Skip to content

Store import nodes in SourceFile.imports instead of StringLiteral nodes#22495

Merged
4 commits merged into
masterfrom
imports
Mar 16, 2018
Merged

Store import nodes in SourceFile.imports instead of StringLiteral nodes#22495
4 commits merged into
masterfrom
imports

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost commented Mar 12, 2018

Fixes #20562
Previously SourceFile.imports was a list of StringLiterals and we had to climb parents to get to the importer node, which is error-prone as node.parent is just a Node. Now we store the import nodes directly and climb down if we need the StringLiteral.

@ghost ghost requested a review from sheetalkamat March 12, 2018 20:43
@ghost ghost force-pushed the imports branch from f290146 to d634010 Compare March 12, 2018 21:23
@ghost ghost force-pushed the imports branch from d634010 to 15fbdee Compare March 12, 2018 22:01
@ghost
Copy link
Copy Markdown
Author

ghost commented Mar 15, 2018

@sheetalkamat @DanielRosenwasser @aozgaa Could someone review this? Underlying issue is marked 2.8.1.

@sheetalkamat
Copy link
Copy Markdown
Member

I prefer that we just handle the scenario of getting import declaration from string literal correctly rather than changing the storage from module specifiers to import declarations

@RyanCavanaugh
Copy link
Copy Markdown
Member

No tests?

@ghost ghost force-pushed the imports branch from 84f0612 to 09826f9 Compare March 15, 2018 20:40
@ghost
Copy link
Copy Markdown
Author

ghost commented Mar 15, 2018

This was a telemetry issue.. I haven't been able to figure out a case that actually fails at runtime.

@ghost
Copy link
Copy Markdown
Author

ghost commented Mar 15, 2018

@sheetalkamat Please re-review

Comment thread src/compiler/types.ts

/* @internal */
export type AnyValidImportOrReExport =
| (ImportDeclaration | ExportDeclaration) & { moduleSpecifier: StringLiteral }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be incorrect from https://github.com/Microsoft/TypeScript/pull/22495/files#diff-08a3cc4f1f9a51dbb468c2810f5229d3R1607 where moduleSpecifier is not present ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be, added it there.

Comment thread src/compiler/utilities.ts
switch (node.parent.kind) {
case SyntaxKind.ImportDeclaration:
case SyntaxKind.ExportDeclaration:
return node.parent as AnyValidImportOrReExport;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this is where we might crash as moduleSpecifier is absent on import declaration at: https://github.com/Microsoft/TypeScript/pull/22495/files#diff-08a3cc4f1f9a51dbb468c2810f5229d3R1607 ?

Comment thread src/compiler/program.ts Outdated
imports = append(imports, node.arguments[0]);
}
// we have to check the argument list has length of 1. We will still have to process these even though we have parsing error.
else if (isImportCall(node) && node.arguments.length === 1 && node.arguments[0].kind === SyntaxKind.StringLiteral) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check instead isStringLiteralLike(node.arguments[0]) instead of checking just string literal ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isRequireCall checks for StringLiteralLike, updated the parameter name

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No i meant

// we have to check the argument list has length of 1. We will still have to process these even though we have parsing error.
                 else if (isImportCall(node) && node.arguments.length === 1 && node.arguments[0].kind === SyntaxKind.StringLiteral) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sure. Now we can get better typings for:

const foo = await import(`foo`);

@ghost ghost merged commit 28ff6b6 into master Mar 16, 2018
@ghost ghost deleted the imports branch March 16, 2018 21:01
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants