-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Support 'import.meta' #23327
Merged
Merged
Support 'import.meta' #23327
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8923eec
Added tests for 'import.meta'.
DanielRosenwasser 9b8670c
Accepted baselines.
DanielRosenwasser 9e2bbb6
Basic parsing/emitting support for 'import.meta'.
DanielRosenwasser f0081f0
Basic checking for 'import.meta'.
DanielRosenwasser 910c5d3
Accepted baselines.
DanielRosenwasser 7c0f249
Added semicolon.
DanielRosenwasser 56f4b2e
Provide an error when using 'import.meta' without setting 'esnext'.
DanielRosenwasser 4f497e6
Added tests around ES5, assigning to 'import.meta' and properties, gl…
DanielRosenwasser 1ea269a
Accepted baselines.
DanielRosenwasser 0feefab
'modulekind' -> 'moduleKind'
DanielRosenwasser a55febd
Make 'ImportMeta' more minimal.
DanielRosenwasser 28f8d75
Accepted baselines.
DanielRosenwasser 9672116
Merge remote-tracking branch 'origin/master' into importDotMeta
DanielRosenwasser bc0d3e1
Look for top-level imports/exports before diving into the tree.
DanielRosenwasser 161535b
Check for both 'module' and 'target'.
DanielRosenwasser 12a3e39
Specify ESNext module in test.
DanielRosenwasser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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 |
---|---|---|
|
@@ -540,7 +540,7 @@ namespace ts { | |
const newSourceFile = IncrementalParser.updateSourceFile(sourceFile, newText, textChangeRange, aggressiveChecks); | ||
// Because new source file node is created, it may not have the flag PossiblyContainDynamicImport. This is the case if there is no new edit to add dynamic import. | ||
// We will manually port the flag to the new source file. | ||
newSourceFile.flags |= (sourceFile.flags & NodeFlags.PossiblyContainsDynamicImport); | ||
newSourceFile.flags |= (sourceFile.flags & NodeFlags.PermanentlySetIncrementalFlags); | ||
return newSourceFile; | ||
} | ||
|
||
|
@@ -2625,6 +2625,20 @@ namespace ts { | |
return token() === SyntaxKind.OpenParenToken || token() === SyntaxKind.LessThanToken; | ||
} | ||
|
||
function nextTokenIsDot() { | ||
return nextToken() === SyntaxKind.DotToken; | ||
} | ||
|
||
function nextTokenIsOpenParenOrLessThanOrDot() { | ||
switch (nextToken()) { | ||
case SyntaxKind.OpenParenToken: | ||
case SyntaxKind.LessThanToken: | ||
case SyntaxKind.DotToken: | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
function parseTypeLiteral(): TypeLiteralNode { | ||
const node = <TypeLiteralNode>createNode(SyntaxKind.TypeLiteral); | ||
node.members = parseObjectTypeMembers(); | ||
|
@@ -3093,7 +3107,7 @@ namespace ts { | |
case SyntaxKind.Identifier: | ||
return true; | ||
case SyntaxKind.ImportKeyword: | ||
return lookAhead(nextTokenIsOpenParenOrLessThan); | ||
return lookAhead(nextTokenIsOpenParenOrLessThanOrDot); | ||
default: | ||
return isIdentifier(); | ||
} | ||
|
@@ -3955,14 +3969,31 @@ namespace ts { | |
// 3)we have a MemberExpression which either completes the LeftHandSideExpression, | ||
// or starts the beginning of the first four CallExpression productions. | ||
let expression: MemberExpression; | ||
if (token() === SyntaxKind.ImportKeyword && lookAhead(nextTokenIsOpenParenOrLessThan)) { | ||
// We don't want to eagerly consume all import keyword as import call expression so we look a head to find "(" | ||
// For example: | ||
// var foo3 = require("subfolder | ||
// import * as foo1 from "module-from-node | ||
// We want this import to be a statement rather than import call expression | ||
sourceFile.flags |= NodeFlags.PossiblyContainsDynamicImport; | ||
expression = parseTokenNode<PrimaryExpression>(); | ||
if (token() === SyntaxKind.ImportKeyword) { | ||
if (lookAhead(nextTokenIsOpenParenOrLessThan)) { | ||
// We don't want to eagerly consume all import keyword as import call expression so we look ahead to find "(" | ||
// For example: | ||
// var foo3 = require("subfolder | ||
// import * as foo1 from "module-from-node | ||
// We want this import to be a statement rather than import call expression | ||
sourceFile.flags |= NodeFlags.PossiblyContainsDynamicImport; | ||
expression = parseTokenNode<PrimaryExpression>(); | ||
} | ||
else if (lookAhead(nextTokenIsDot)) { | ||
// This is an 'import.*' metaproperty (i.e. 'import.meta') | ||
const fullStart = scanner.getStartPos(); | ||
nextToken(); // advance past the 'import' | ||
nextToken(); // advance past the dot | ||
const node = createNode(SyntaxKind.MetaProperty, fullStart) as MetaProperty; | ||
node.keywordToken = SyntaxKind.ImportKeyword; | ||
node.name = parseIdentifierName(); | ||
expression = finishNode(node); | ||
|
||
sourceFile.flags |= NodeFlags.PossiblyContainsImportMeta; | ||
} | ||
else { | ||
expression = parseMemberExpressionOrHigher(); | ||
} | ||
} | ||
else { | ||
expression = token() === SyntaxKind.SuperKeyword ? parseSuperExpression() : parseMemberExpressionOrHigher(); | ||
|
@@ -4523,7 +4554,7 @@ namespace ts { | |
case SyntaxKind.FunctionKeyword: | ||
return parseFunctionExpression(); | ||
case SyntaxKind.NewKeyword: | ||
return parseNewExpression(); | ||
return parseNewExpressionOrNewDotTarget(); | ||
case SyntaxKind.SlashToken: | ||
case SyntaxKind.SlashEqualsToken: | ||
if (reScanSlashToken() === SyntaxKind.RegularExpressionLiteral) { | ||
|
@@ -4674,7 +4705,7 @@ namespace ts { | |
return isIdentifier() ? parseIdentifier() : undefined; | ||
} | ||
|
||
function parseNewExpression(): NewExpression | MetaProperty { | ||
function parseNewExpressionOrNewDotTarget(): NewExpression | MetaProperty { | ||
const fullStart = scanner.getStartPos(); | ||
parseExpected(SyntaxKind.NewKeyword); | ||
if (parseOptional(SyntaxKind.DotToken)) { | ||
|
@@ -5122,7 +5153,7 @@ namespace ts { | |
return true; | ||
|
||
case SyntaxKind.ImportKeyword: | ||
return isStartOfDeclaration() || lookAhead(nextTokenIsOpenParenOrLessThan); | ||
return isStartOfDeclaration() || lookAhead(nextTokenIsOpenParenOrLessThanOrDot); | ||
|
||
case SyntaxKind.ConstKeyword: | ||
case SyntaxKind.ExportKeyword: | ||
|
@@ -6108,14 +6139,29 @@ namespace ts { | |
} | ||
|
||
function setExternalModuleIndicator(sourceFile: SourceFile) { | ||
sourceFile.externalModuleIndicator = forEach(sourceFile.statements, node => | ||
hasModifier(node, ModifierFlags.Export) | ||
|| node.kind === SyntaxKind.ImportEqualsDeclaration && (<ImportEqualsDeclaration>node).moduleReference.kind === SyntaxKind.ExternalModuleReference | ||
|| node.kind === SyntaxKind.ImportDeclaration | ||
|| node.kind === SyntaxKind.ExportAssignment | ||
|| node.kind === SyntaxKind.ExportDeclaration | ||
// Usually we'd like to avoid a full tree walk, but it's possible | ||
// that we have a deeper external module indicator (e.g. `import.meta`, | ||
// and possibly nested import statements in the future). | ||
// Ideally the first few statements will be an import/export anyway. | ||
sourceFile.externalModuleIndicator = | ||
!(sourceFile.flags & NodeFlags.PossiblyContainsImportMeta) ? | ||
forEach(sourceFile.statements, isAnExternalModuleIndicatorNode) : | ||
walkTreeForExternalModuleIndicators(sourceFile); | ||
} | ||
|
||
function isAnExternalModuleIndicatorNode(node: Node) { | ||
return hasModifier(node, ModifierFlags.Export) | ||
|| node.kind === SyntaxKind.ImportEqualsDeclaration && (<ImportEqualsDeclaration>node).moduleReference.kind === SyntaxKind.ExternalModuleReference | ||
|| node.kind === SyntaxKind.ImportDeclaration | ||
|| node.kind === SyntaxKind.ExportAssignment | ||
|| node.kind === SyntaxKind.ExportDeclaration | ||
|| isMetaProperty(node) && node.keywordToken === SyntaxKind.ImportKeyword && node.name.escapedText === "meta" | ||
? node | ||
: undefined); | ||
: undefined; | ||
} | ||
|
||
function walkTreeForExternalModuleIndicators(node: Node): Node { | ||
return isAnExternalModuleIndicatorNode(node) ? node : forEachChild(node, walkTreeForExternalModuleIndicators); | ||
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. This can accidentally pick up an import/export from an module augmentation as the current file's declare module "foo" {
import * as blah from "blah"
}
import.meta |
||
} | ||
|
||
const enum ParsingContext { | ||
|
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
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.
i think this should be: