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
Parse import reflection #14926
Merged
Merged
Parse import reflection #14926
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3a45f53
chore: improve token typings
JLHwung 7031a3c
parse import reflection
JLHwung e5780ec
print import reflection
JLHwung 4a53088
add syntax plugin
JLHwung 9f48d7d
add syntax plugin to standalone
JLHwung bacf8d2
update AST to module: boolean
JLHwung 178538d
fix: module is not a reserved JS keyword
JLHwung 2cc6536
fix: print ImportDeclarations' inner comments
JLHwung 05fe1e2
cleanup
JLHwung d63f44b
Update packages/babel-parser/src/parse-error/standard-errors.ts
JLHwung f363a56
Disable import assertions after import module
JLHwung 98c8a28
Update flow/ts integration errors
JLHwung 470f301
avoid indent when printing import's inner comments
JLHwung 9467184
add more test cases
JLHwung 9f14643
update generated files
JLHwung 47683a3
fix: print inner comments before type/module
JLHwung File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/babel-generator/test/fixtures/comments/import-reflection-inner-comment/input.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* 0 */import /* 1 */module /* 2 */from /* 3 */from /* 4 */"./module.wasm"/* 5 */; |
3 changes: 3 additions & 0 deletions
3
packages/babel-generator/test/fixtures/comments/import-reflection-inner-comment/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["importReflection"] | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-generator/test/fixtures/comments/import-reflection-inner-comment/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* 0 */import /* 1 */module /* 2 */from /* 3 */ from /* 4 */"./module.wasm" /* 5 */; |
1 change: 1 addition & 0 deletions
1
packages/babel-generator/test/fixtures/import-reflection/basic/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import module foo from "./module.wasm"; |
1 change: 1 addition & 0 deletions
1
packages/babel-generator/test/fixtures/import-reflection/basic/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import module foo from "./module.wasm"; |
3 changes: 3 additions & 0 deletions
3
packages/babel-generator/test/fixtures/import-reflection/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["importReflection"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2612,6 +2612,24 @@ export default abstract class StatementParser extends ExpressionParser { | |
return false; | ||
} | ||
|
||
checkImportReflection(node: Undone<N.ImportDeclaration>) { | ||
if (node.module) { | ||
if ( | ||
node.specifiers.length !== 1 || | ||
node.specifiers[0].type !== "ImportDefaultSpecifier" | ||
) { | ||
this.raise(Errors.ImportReflectionNotBinding, { | ||
at: node.specifiers[0].loc.start, | ||
}); | ||
} | ||
if (node.assertions?.length > 0) { | ||
this.raise(Errors.ImportReflectionHasAssertion, { | ||
at: node.specifiers[0].loc.start, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
checkJSONModuleImport( | ||
node: Undone< | ||
N.ExportAllDeclaration | N.ExportNamedDeclaration | N.ImportDeclaration | ||
|
@@ -2645,13 +2663,47 @@ export default abstract class StatementParser extends ExpressionParser { | |
} | ||
} | ||
|
||
parseMaybeImportReflection(node: Undone<N.ImportDeclaration>) { | ||
let isImportReflection = false; | ||
if (this.isContextual(tt._module)) { | ||
const lookahead = this.lookahead(); | ||
if (tokenIsIdentifier(lookahead.type)) { | ||
if (lookahead.type !== tt._from) { | ||
// import module x | ||
isImportReflection = true; | ||
} else { | ||
const nextNextTokenFirstChar = this.input.charCodeAt( | ||
this.nextTokenStartSince(lookahead.end), | ||
); | ||
if (nextNextTokenFirstChar === charCodes.lowercaseF) { | ||
// import module from from ... | ||
isImportReflection = true; | ||
} | ||
} | ||
} else { | ||
// import module { x } ... | ||
// This is invalid, we will continue parsing and throw | ||
// a recoverable error later | ||
isImportReflection = true; | ||
Comment on lines
+2684
to
+2687
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add a test for |
||
} | ||
} | ||
if (isImportReflection) { | ||
this.expectPlugin("importReflection"); | ||
this.next(); // eat tt._module; | ||
node.module = true; | ||
} else if (this.hasPlugin("importReflection")) { | ||
node.module = false; | ||
} | ||
} | ||
|
||
// Parses import declaration. | ||
// https://tc39.es/ecma262/#prod-ImportDeclaration | ||
|
||
parseImport(this: Parser, node: Undone<N.ImportDeclaration>): N.AnyImport { | ||
// import '...' | ||
node.specifiers = []; | ||
if (!this.match(tt.string)) { | ||
this.parseMaybeImportReflection(node); | ||
// check if we have a default import like | ||
// import React from "react"; | ||
const hasDefault = this.maybeParseDefaultImportSpecifier(node); | ||
|
@@ -2684,6 +2736,7 @@ export default abstract class StatementParser extends ExpressionParser { | |
node.attributes = attributes; | ||
} | ||
} | ||
this.checkImportReflection(node); | ||
this.checkJSONModuleImport(node); | ||
|
||
this.semicolon(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/babel-parser/test/fixtures/experimental/_no-plugin/import-reflection/input.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import module foo from "./module.wasm"; |
3 changes: 3 additions & 0 deletions
3
packages/babel-parser/test/fixtures/experimental/_no-plugin/import-reflection/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"throws": "This experimental syntax requires enabling the parser plugin: \"importReflection\". (1:7)" | ||
} |
1 change: 1 addition & 0 deletions
1
...es/babel-parser/test/fixtures/experimental/import-reflection/invalid-assertions/input.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import module foo from "./module.json" assert { type: "json" } |
4 changes: 4 additions & 0 deletions
4
...babel-parser/test/fixtures/experimental/import-reflection/invalid-assertions/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"plugins": ["importReflection", "importAssertions"], | ||
"sourceType": "module" | ||
} |
61 changes: 61 additions & 0 deletions
61
.../babel-parser/test/fixtures/experimental/import-reflection/invalid-assertions/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":62,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":62,"index":62}}, | ||
"errors": [ | ||
"SyntaxError: `import module x` cannot have assertions. (1:14)" | ||
], | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":62,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":62,"index":62}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "ImportDeclaration", | ||
"start":0,"end":62,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":62,"index":62}}, | ||
"specifiers": [ | ||
{ | ||
"type": "ImportDefaultSpecifier", | ||
"start":14,"end":17,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":17,"index":17}}, | ||
"local": { | ||
"type": "Identifier", | ||
"start":14,"end":17,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":17,"index":17},"identifierName":"foo"}, | ||
"name": "foo" | ||
} | ||
} | ||
], | ||
"module": true, | ||
"source": { | ||
"type": "StringLiteral", | ||
"start":23,"end":38,"loc":{"start":{"line":1,"column":23,"index":23},"end":{"line":1,"column":38,"index":38}}, | ||
"extra": { | ||
"rawValue": "./module.json", | ||
"raw": "\"./module.json\"" | ||
}, | ||
"value": "./module.json" | ||
}, | ||
"assertions": [ | ||
{ | ||
"type": "ImportAttribute", | ||
"start":48,"end":60,"loc":{"start":{"line":1,"column":48,"index":48},"end":{"line":1,"column":60,"index":60}}, | ||
"key": { | ||
"type": "Identifier", | ||
"start":48,"end":52,"loc":{"start":{"line":1,"column":48,"index":48},"end":{"line":1,"column":52,"index":52},"identifierName":"type"}, | ||
"name": "type" | ||
}, | ||
"value": { | ||
"type": "StringLiteral", | ||
"start":54,"end":60,"loc":{"start":{"line":1,"column":54,"index":54},"end":{"line":1,"column":60,"index":60}}, | ||
"extra": { | ||
"rawValue": "json", | ||
"raw": "\"json\"" | ||
}, | ||
"value": "json" | ||
} | ||
} | ||
] | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...arser/test/fixtures/experimental/import-reflection/invalid-default-named-import/input.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import module foo, {bar} from "./module.wasm"; |
55 changes: 55 additions & 0 deletions
55
...ser/test/fixtures/experimental/import-reflection/invalid-default-named-import/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":46,"index":46}}, | ||
"errors": [ | ||
"SyntaxError: Only `import module x from \"./module\"` is valid. (1:14)" | ||
], | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":46,"index":46}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "ImportDeclaration", | ||
"start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":46,"index":46}}, | ||
"specifiers": [ | ||
{ | ||
"type": "ImportDefaultSpecifier", | ||
"start":14,"end":17,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":17,"index":17}}, | ||
"local": { | ||
"type": "Identifier", | ||
"start":14,"end":17,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":17,"index":17},"identifierName":"foo"}, | ||
"name": "foo" | ||
} | ||
}, | ||
{ | ||
"type": "ImportSpecifier", | ||
"start":20,"end":23,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":23,"index":23}}, | ||
"imported": { | ||
"type": "Identifier", | ||
"start":20,"end":23,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":23,"index":23},"identifierName":"bar"}, | ||
"name": "bar" | ||
}, | ||
"local": { | ||
"type": "Identifier", | ||
"start":20,"end":23,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":23,"index":23},"identifierName":"bar"}, | ||
"name": "bar" | ||
} | ||
} | ||
], | ||
"module": true, | ||
"source": { | ||
"type": "StringLiteral", | ||
"start":30,"end":45,"loc":{"start":{"line":1,"column":30,"index":30},"end":{"line":1,"column":45,"index":45}}, | ||
"extra": { | ||
"rawValue": "./module.wasm", | ||
"raw": "\"./module.wasm\"" | ||
}, | ||
"value": "./module.wasm" | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...s/babel-parser/test/fixtures/experimental/import-reflection/invalid-export-from/input.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export module x from "./module.wasm"; |
8 changes: 8 additions & 0 deletions
8
...abel-parser/test/fixtures/experimental/import-reflection/invalid-export-from/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"plugins": [ | ||
"importReflection", | ||
"exportDefaultFrom" | ||
], | ||
"sourceType": "module", | ||
"throws": "Unexpected token, expected \"{\" (1:7)" | ||
} |
1 change: 1 addition & 0 deletions
1
...-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-1/input.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import type module foo from "./module.wasm"; |
8 changes: 8 additions & 0 deletions
8
...rser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-1/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"plugins": [ | ||
"importReflection", | ||
"flow" | ||
], | ||
"sourceType": "module", | ||
"throws": "Unexpected token, expected \"from\" (1:19)" | ||
} |
1 change: 1 addition & 0 deletions
1
...-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-2/input.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import module type foo from "./module.wasm"; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test for
import module from foo
, just to verify that it errors even iffoo
starts withf
?