-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Better template literals support in checker #32064
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
Merged
sandersn
merged 10 commits into
microsoft:master
from
IllusionMH:better-template-literals-support-in-checker
Sep 27, 2019
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c7d496c
Support template literals in enum declarations
IllusionMH e906e39
Support template literals in const enum access
IllusionMH 79ce806
Support template literals in swith with typeof narrowing
IllusionMH cc0a28c
Support template literals in element access discriminant
IllusionMH 8e80e4b
Support template literals in ambient module declaration
IllusionMH 378032a
Unify symbols for template literals in computed properties
IllusionMH c941ae3
Unify expression position checks for template literals
IllusionMH a97f8f6
Support template literals in rename and find all references
IllusionMH 11086c9
Mark computed properties with template literals as write access
IllusionMH 169566a
Inline startsWithQuote
sandersn 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
28 changes: 28 additions & 0 deletions
28
tests/baselines/reference/ambientModuleWithTemplateLiterals.js
This file contains hidden or 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,28 @@ | ||
//// [ambientModuleWithTemplateLiterals.ts] | ||
declare module Foo { | ||
enum Bar { | ||
a = `1`, | ||
b = '2', | ||
c = '3' | ||
} | ||
|
||
export const a = 'string'; | ||
export const b = `template`; | ||
|
||
export const c = Bar.a; | ||
export const d = Bar['b']; | ||
export const e = Bar[`c`]; | ||
} | ||
|
||
Foo.a; | ||
Foo.b; | ||
Foo.c; | ||
Foo.d; | ||
Foo.e; | ||
|
||
//// [ambientModuleWithTemplateLiterals.js] | ||
Foo.a; | ||
Foo.b; | ||
Foo.c; | ||
Foo.d; | ||
Foo.e; |
65 changes: 65 additions & 0 deletions
65
tests/baselines/reference/ambientModuleWithTemplateLiterals.symbols
This file contains hidden or 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,65 @@ | ||
=== tests/cases/compiler/ambientModuleWithTemplateLiterals.ts === | ||
declare module Foo { | ||
>Foo : Symbol(Foo, Decl(ambientModuleWithTemplateLiterals.ts, 0, 0)) | ||
|
||
enum Bar { | ||
>Bar : Symbol(Bar, Decl(ambientModuleWithTemplateLiterals.ts, 0, 20)) | ||
|
||
a = `1`, | ||
>a : Symbol(Bar.a, Decl(ambientModuleWithTemplateLiterals.ts, 1, 14)) | ||
|
||
b = '2', | ||
>b : Symbol(Bar.b, Decl(ambientModuleWithTemplateLiterals.ts, 2, 16)) | ||
|
||
c = '3' | ||
>c : Symbol(Bar.c, Decl(ambientModuleWithTemplateLiterals.ts, 3, 16)) | ||
} | ||
|
||
export const a = 'string'; | ||
>a : Symbol(a, Decl(ambientModuleWithTemplateLiterals.ts, 7, 16)) | ||
|
||
export const b = `template`; | ||
>b : Symbol(b, Decl(ambientModuleWithTemplateLiterals.ts, 8, 16)) | ||
|
||
export const c = Bar.a; | ||
>c : Symbol(c, Decl(ambientModuleWithTemplateLiterals.ts, 10, 16)) | ||
>Bar.a : Symbol(Bar.a, Decl(ambientModuleWithTemplateLiterals.ts, 1, 14)) | ||
>Bar : Symbol(Bar, Decl(ambientModuleWithTemplateLiterals.ts, 0, 20)) | ||
>a : Symbol(Bar.a, Decl(ambientModuleWithTemplateLiterals.ts, 1, 14)) | ||
|
||
export const d = Bar['b']; | ||
>d : Symbol(d, Decl(ambientModuleWithTemplateLiterals.ts, 11, 16)) | ||
>Bar : Symbol(Bar, Decl(ambientModuleWithTemplateLiterals.ts, 0, 20)) | ||
>'b' : Symbol(Bar.b, Decl(ambientModuleWithTemplateLiterals.ts, 2, 16)) | ||
|
||
export const e = Bar[`c`]; | ||
>e : Symbol(e, Decl(ambientModuleWithTemplateLiterals.ts, 12, 16)) | ||
>Bar : Symbol(Bar, Decl(ambientModuleWithTemplateLiterals.ts, 0, 20)) | ||
>`c` : Symbol(Bar.c, Decl(ambientModuleWithTemplateLiterals.ts, 3, 16)) | ||
} | ||
|
||
Foo.a; | ||
>Foo.a : Symbol(Foo.a, Decl(ambientModuleWithTemplateLiterals.ts, 7, 16)) | ||
>Foo : Symbol(Foo, Decl(ambientModuleWithTemplateLiterals.ts, 0, 0)) | ||
>a : Symbol(Foo.a, Decl(ambientModuleWithTemplateLiterals.ts, 7, 16)) | ||
|
||
Foo.b; | ||
>Foo.b : Symbol(Foo.b, Decl(ambientModuleWithTemplateLiterals.ts, 8, 16)) | ||
>Foo : Symbol(Foo, Decl(ambientModuleWithTemplateLiterals.ts, 0, 0)) | ||
>b : Symbol(Foo.b, Decl(ambientModuleWithTemplateLiterals.ts, 8, 16)) | ||
|
||
Foo.c; | ||
>Foo.c : Symbol(Foo.c, Decl(ambientModuleWithTemplateLiterals.ts, 10, 16)) | ||
>Foo : Symbol(Foo, Decl(ambientModuleWithTemplateLiterals.ts, 0, 0)) | ||
>c : Symbol(Foo.c, Decl(ambientModuleWithTemplateLiterals.ts, 10, 16)) | ||
|
||
Foo.d; | ||
>Foo.d : Symbol(Foo.d, Decl(ambientModuleWithTemplateLiterals.ts, 11, 16)) | ||
>Foo : Symbol(Foo, Decl(ambientModuleWithTemplateLiterals.ts, 0, 0)) | ||
>d : Symbol(Foo.d, Decl(ambientModuleWithTemplateLiterals.ts, 11, 16)) | ||
|
||
Foo.e; | ||
>Foo.e : Symbol(Foo.e, Decl(ambientModuleWithTemplateLiterals.ts, 12, 16)) | ||
>Foo : Symbol(Foo, Decl(ambientModuleWithTemplateLiterals.ts, 0, 0)) | ||
>e : Symbol(Foo.e, Decl(ambientModuleWithTemplateLiterals.ts, 12, 16)) | ||
|
72 changes: 72 additions & 0 deletions
72
tests/baselines/reference/ambientModuleWithTemplateLiterals.types
This file contains hidden or 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,72 @@ | ||
=== tests/cases/compiler/ambientModuleWithTemplateLiterals.ts === | ||
declare module Foo { | ||
>Foo : typeof Foo | ||
|
||
enum Bar { | ||
>Bar : Bar | ||
|
||
a = `1`, | ||
>a : Bar.a | ||
>`1` : "1" | ||
|
||
b = '2', | ||
>b : Bar.b | ||
>'2' : "2" | ||
|
||
c = '3' | ||
>c : Bar.c | ||
>'3' : "3" | ||
} | ||
|
||
export const a = 'string'; | ||
>a : "string" | ||
>'string' : "string" | ||
|
||
export const b = `template`; | ||
>b : "template" | ||
>`template` : "template" | ||
|
||
export const c = Bar.a; | ||
>c : Bar.a | ||
>Bar.a : Bar.a | ||
>Bar : typeof Bar | ||
>a : Bar.a | ||
|
||
export const d = Bar['b']; | ||
>d : Bar.b | ||
>Bar['b'] : Bar.b | ||
>Bar : typeof Bar | ||
>'b' : "b" | ||
|
||
export const e = Bar[`c`]; | ||
>e : Bar.c | ||
>Bar[`c`] : Bar.c | ||
>Bar : typeof Bar | ||
>`c` : "c" | ||
} | ||
|
||
Foo.a; | ||
>Foo.a : "string" | ||
>Foo : typeof Foo | ||
>a : "string" | ||
|
||
Foo.b; | ||
>Foo.b : "template" | ||
>Foo : typeof Foo | ||
>b : "template" | ||
|
||
Foo.c; | ||
>Foo.c : Foo.Bar.a | ||
>Foo : typeof Foo | ||
>c : Foo.Bar.a | ||
|
||
Foo.d; | ||
>Foo.d : Foo.Bar.b | ||
>Foo : typeof Foo | ||
>d : Foo.Bar.b | ||
|
||
Foo.e; | ||
>Foo.e : Foo.Bar.c | ||
>Foo : typeof Foo | ||
>e : Foo.Bar.c | ||
|
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.
Just curious, what was this one needed for?
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.
Template literals and string literals with extended Unicode escapes will be first processed with es2015 transformer which will replace them with
Synthesized
string literals that don't have reference to parent element or to original node.Under the hood
getTextOfNode(node.argumentExpression)
will callgetSourceFileOfNode
onSynthesized
string literal which will returnundefined
(becausenode.parent
isundefined
) and thengetSourceTextOfNodeFromSourceFile
that will try to accesssourceFile.text
and exception will be thrown.On the other hand
ElementAccessExpression
even if will be replaced withSynthesized
node, it will have link to original node. ThereforeoriginalNode.argumentExpression
will have proper parent references up to source file and text will be retrieved correctly.