Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: check assertions per import statement #228

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
23bde67
refactor: check assertions per import statement
nayeemrmn Feb 12, 2023
0af4881
fmt
nayeemrmn Feb 12, 2023
af1009e
review
nayeemrmn Feb 12, 2023
e964794
Merge remote-tracking branch 'upstream/main' into import-errors
nayeemrmn Feb 12, 2023
d9d9c70
add comment for is_known_none()
nayeemrmn Feb 12, 2023
e8e71af
move dynamic import skip to validate()
nayeemrmn Feb 13, 2023
33cd066
use follow_dynamic
nayeemrmn Feb 13, 2023
8b55dff
better serialization for import assertions
nayeemrmn Feb 13, 2023
bae5aa5
populate module_metadata in fill()
nayeemrmn Feb 13, 2023
734fba4
remove unnecessary types / trait impls
nayeemrmn Feb 13, 2023
a3042b9
change ModuleGraphError::UnsupportedMediaType to unnamed tuple fields
nayeemrmn Feb 13, 2023
c625c53
collapse ModuleGraphError::ModuleError()
nayeemrmn Feb 13, 2023
7901e8c
Merge remote-tracking branch 'upstream/main' into import-errors
nayeemrmn Feb 14, 2023
fc487c5
remove ModuleKind::Asserted
nayeemrmn Feb 14, 2023
908cd6d
add ImportKind
nayeemrmn Feb 14, 2023
6750bb0
skip serializing ImportKind::Es
nayeemrmn Feb 14, 2023
604376c
fix: use same behaviour if has resolver or not
dsherret Feb 14, 2023
be06539
Move invalid local import checks to validate
dsherret Feb 15, 2023
0e8b76a
Test.
dsherret Feb 15, 2023
1ad47ec
Wasm
dsherret Feb 15, 2023
4db3546
Merge my work on main
dsherret Feb 15, 2023
637de8e
Merge branch 'main' into import-errors
dsherret Feb 15, 2023
b3a1998
Update wasm
dsherret Feb 15, 2023
ccb15a9
Merge remote-tracking branch 'upstream/main' into import-errors
nayeemrmn Feb 16, 2023
e386130
only enforce assertions on es runtime imports
nayeemrmn Feb 16, 2023
bfb545e
Merge remote-tracking branch 'upstream/main' into import-errors
nayeemrmn Feb 22, 2023
edbf9a3
Merge remote-tracking branch 'upstream/main' into import-errors
nayeemrmn Mar 1, 2023
6e68504
Merge remote-tracking branch 'upstream/main' into import-errors
nayeemrmn Mar 5, 2023
0171ce5
Merge remote-tracking branch 'upstream/main' into import-errors
nayeemrmn Mar 6, 2023
47ff14d
Merge remote-tracking branch 'upstream/main' into import-errors
nayeemrmn Mar 6, 2023
792196e
Merge remote-tracking branch 'upstream/main' into import-errors
nayeemrmn Apr 4, 2023
205b21e
don't serialize assertions, maybe unstable
nayeemrmn Apr 4, 2023
4b65693
restore Dependency::assertionType for legacy
nayeemrmn Apr 4, 2023
03f670c
fixup! restore Dependency::assertionType for legacy
nayeemrmn Apr 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

199 changes: 199 additions & 0 deletions js/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,21 @@ Deno.test({
},
},
},
"imports": [
{
"range": {
"end": {
"character": 27,
"line": 0,
},
"start": {
"character": 19,
"line": 0,
},
},
"specifier": "./b.js",
},
],
},
],
"kind": "esm",
Expand Down Expand Up @@ -282,6 +297,21 @@ Deno.test({
},
},
},
"imports": [
{
"range": {
"end": {
"character": 32,
"line": 0,
},
"start": {
"character": 19,
"line": 0,
},
},
"specifier": "./deno.json",
},
],
"assertionType": "json",
},
],
Expand Down Expand Up @@ -377,6 +407,21 @@ Deno.test({
},
},
},
"imports": [
{
"range": {
"end": {
"character": 27,
"line": 0,
},
"start": {
"character": 19,
"line": 0,
},
},
"specifier": "./b.js",
},
],
},
],
"kind": "esm",
Expand Down Expand Up @@ -473,6 +518,21 @@ Deno.test({
},
},
},
"imports": [
{
"range": {
"end": {
"character": 32,
"line": 0,
},
"start": {
"character": 20,
"line": 0,
},
},
"specifier": "builtin:fs",
},
],
},
{
"specifier": "https://example.com/bundle",
Expand All @@ -489,6 +549,21 @@ Deno.test({
},
},
},
"imports": [
{
"range": {
"end": {
"character": 62,
"line": 1,
},
"start": {
"character": 34,
"line": 1,
},
},
"specifier": "https://example.com/bundle",
},
],
},
],
"kind": "esm",
Expand Down Expand Up @@ -560,6 +635,21 @@ Deno.test({
"end": { "line": 2, "character": 34 },
},
},
"imports": [
{
"range": {
"end": {
"character": 34,
"line": 2,
},
"start": {
"character": 26,
"line": 2,
},
},
"specifier": "./a.ts",
},
],
}, {
"specifier": "./b.ts",
"code": {
Expand All @@ -569,6 +659,21 @@ Deno.test({
"end": { "line": 3, "character": 35 },
},
},
"imports": [
{
"range": {
"end": {
"character": 35,
"line": 3,
},
"start": {
"character": 27,
"line": 3,
},
},
"specifier": "./b.ts",
},
],
}, {
"specifier": "./c.ts",
"code": {
Expand All @@ -578,6 +683,21 @@ Deno.test({
"end": { "line": 4, "character": 34 },
},
},
"imports": [
{
"range": {
"end": {
"character": 34,
"line": 4,
},
"start": {
"character": 26,
"line": 4,
},
},
"specifier": "./c.ts",
},
],
}, {
"specifier": "./d.ts",
"code": {
Expand All @@ -588,6 +708,22 @@ Deno.test({
},
},
"isDynamic": true,
"imports": [
{
"isDynamic": true,
"range": {
"end": {
"character": 39,
"line": 5,
},
"start": {
"character": 31,
"line": 5,
},
},
"specifier": "./d.ts",
},
],
}],
"typesDependency": {
"specifier": "./test01.d.ts",
Expand Down Expand Up @@ -723,6 +859,21 @@ Deno.test({
},
},
},
"imports": [
{
"range": {
"end": {
"character": 32,
"line": 1,
},
"start": {
"character": 22,
"line": 1,
},
},
"specifier": "./a.json",
},
],
"assertionType": "json",
},
{
Expand All @@ -741,6 +892,22 @@ Deno.test({
},
},
"isDynamic": true,
"imports": [
{
"isDynamic": true,
"range": {
"end": {
"character": 31,
"line": 2,
},
"start": {
"character": 21,
"line": 2,
},
},
"specifier": "./b.json",
},
],
"assertionType": "json",
},
],
Expand Down Expand Up @@ -780,6 +947,22 @@ Deno.test({
},
},
},
"imports": [
{
"kind": "tsReferencePath",
"range": {
"end": {
"character": 38,
"line": 1,
},
"start": {
"character": 28,
"line": 1,
},
},
"specifier": "./a.d.ts",
},
],
},
{
"specifier": "./b.d.ts",
Expand All @@ -796,6 +979,22 @@ Deno.test({
},
},
},
"imports": [
{
"kind": "tsReferenceTypes",
"range": {
"end": {
"character": 39,
"line": 2,
},
"start": {
"character": 29,
"line": 2,
},
},
"specifier": "./b.d.ts",
},
],
},
],
"kind": "esm",
Expand Down
30 changes: 22 additions & 8 deletions js/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,32 @@ export interface TypesDependencyJson {
dependency: ResolvedDependency;
}

/** The kind of module.
*
* For asserted modules, the value of the `asserted` property is set to the
* `type` value of the assertion.
*
* Dependency analysis is not performed for asserted or Script modules
* currently. Synthetic modules were injected into the graph with their own
* dependencies provided. */
/** The kind of module. */
export type ModuleKind =
| "asserted"
| "esm"
| "npm"
| "external";

export type ImportKind =
| "tsType"
| "tsReferencePath"
| "tsReferenceTypes"
| "jsxImportSource"
| "jsDoc";

export type ImportAssertions = "unknown" | Record<string, string>;

export interface ImportJson {
specifier: string;
/** Absent if standard ES import/export. */
kind?: ImportKind;
range: RangeJson;
isDynamic?: true;
assertions?: ImportAssertions;
errors?: string[];
}

export interface DependencyJson {
/** The string specifier that was used for the dependency. */
specifier: string;
Expand All @@ -117,6 +129,8 @@ export interface DependencyJson {
/** A flag indicating if the dependency was dynamic. (e.g.
* `await import("mod.ts")`) */
isDynamic?: true;
imports?: ImportJson[];
// TODO(nayeemrmn): Legacy field, remove for 2.0. Asserts are per-import.
assertionType?: string;
}

Expand Down
13 changes: 13 additions & 0 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl DependencyKind {
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ImportAssertion {
/// The value of this assertion could not be statically analyzed.
Unknown,
Expand All @@ -147,6 +148,7 @@ impl ImportAssertion {
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ImportAssertions {
/// There was no import assertions object literal.
None,
Expand Down Expand Up @@ -188,6 +190,17 @@ impl ImportAssertions {
_ => None,
}
}

/// Check if a assertion key is known to be absent (not set to a value and not
/// undeterminable). This is significant because it is the precise case where
/// `type: "javascript"` is asserted.
pub fn is_known_none(&self, key: &str) -> bool {
nayeemrmn marked this conversation as resolved.
Show resolved Hide resolved
match self {
ImportAssertions::None => true,
ImportAssertions::Known(map) => !map.contains_key(key),
ImportAssertions::Unknown => false,
}
}
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
Expand Down
Loading