-
Notifications
You must be signed in to change notification settings - Fork 763
Create tokens with the right data #2308
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10781,6 +10781,8 @@ type SourceFile struct { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tokenCacheMu sync.Mutex | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tokenCache map[core.TextRange]*Node | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tokenFactoryMu sync.Mutex | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tokenFactory *NodeFactory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
jakebailey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| declarationMapMu sync.Mutex | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| declarationMap map[string][]*Node | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -10942,6 +10944,16 @@ func (node *SourceFile) GetOrCreateToken( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pos int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parent *Node, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) *TokenNode { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return node.GetOrCreateTokenEx(kind, pos, end, parent, TokenFlagsNone) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (node *SourceFile) GetOrCreateTokenEx( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
jakebailey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| kind Kind, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pos int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parent *Node, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flags TokenFlags, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) *TokenNode { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| node.tokenCacheMu.Lock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defer node.tokenCacheMu.Unlock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -10959,13 +10971,49 @@ func (node *SourceFile) GetOrCreateToken( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| token := newNode(kind, &Token{}, NodeFactoryHooks{}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| token := createToken(kind, node, pos, end, flags) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| token.Loc = loc | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| token.Parent = parent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| node.tokenCache[loc] = token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // `kind` should be a token kind. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func createToken(kind Kind, file *SourceFile, pos, end int, flags TokenFlags) *Node { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| file.tokenFactoryMu.Lock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defer file.tokenFactoryMu.Unlock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if file.tokenFactory == nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| file.tokenFactory = NewNodeFactory(NodeFactoryHooks{}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| text := file.text[pos:end] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| switch kind { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case KindNumericLiteral: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return file.tokenFactory.NewNumericLiteral(text) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case KindBigIntLiteral: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return file.tokenFactory.NewBigIntLiteral(text) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case KindStringLiteral: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return file.tokenFactory.NewStringLiteral(text) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case KindJsxText, KindJsxTextAllWhiteSpaces: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return file.tokenFactory.NewJsxText(text, kind == KindJsxTextAllWhiteSpaces) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case KindRegularExpressionLiteral: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return file.tokenFactory.NewRegularExpressionLiteral(text) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case KindNoSubstitutionTemplateLiteral: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return file.tokenFactory.NewNoSubstitutionTemplateLiteral(text) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return file.tokenFactory.NewNumericLiteral(text) | |
| case KindBigIntLiteral: | |
| return file.tokenFactory.NewBigIntLiteral(text) | |
| case KindStringLiteral: | |
| return file.tokenFactory.NewStringLiteral(text) | |
| case KindJsxText, KindJsxTextAllWhiteSpaces: | |
| return file.tokenFactory.NewJsxText(text, kind == KindJsxTextAllWhiteSpaces) | |
| case KindRegularExpressionLiteral: | |
| return file.tokenFactory.NewRegularExpressionLiteral(text) | |
| case KindNoSubstitutionTemplateLiteral: | |
| return file.tokenFactory.NewNoSubstitutionTemplateLiteral(text) | |
| node := file.tokenFactory.NewNumericLiteral(text) | |
| node.AsNumericLiteral().TokenFlags = flags | |
| return node | |
| case KindBigIntLiteral: | |
| node := file.tokenFactory.NewBigIntLiteral(text) | |
| node.AsBigIntLiteral().TokenFlags = flags | |
| return node | |
| case KindStringLiteral: | |
| node := file.tokenFactory.NewStringLiteral(text) | |
| node.AsStringLiteral().TokenFlags = flags | |
| return node | |
| case KindJsxText, KindJsxTextAllWhiteSpaces: | |
| node := file.tokenFactory.NewJsxText(text, kind == KindJsxTextAllWhiteSpaces) | |
| node.AsJsxText().TokenFlags = flags | |
| return node | |
| case KindRegularExpressionLiteral: | |
| node := file.tokenFactory.NewRegularExpressionLiteral(text) | |
| node.AsRegularExpressionLiteral().TokenFlags = flags | |
| return node | |
| case KindNoSubstitutionTemplateLiteral: | |
| node := file.tokenFactory.NewNoSubstitutionTemplateLiteral(text) | |
| node.AsNoSubstitutionTemplateLiteral().TemplateFlags = flags | |
| return node |
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.
This is unfortunately correct, and it's very annoying e.g. NewStringLiteral doesn't take the flags because I keep forgetting about it.
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.
Should it?
This code is also sort of the same as parseLiteralExpression which does sort of make it seem like this is all sort of error prone, yeah.
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 it should in general, as there's code in the LS that uses token flags, and code in the printer that also uses token flags, but there's tons of existing places where we don't set flags right now. I'll try and refactor that in another PR tomorrow.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package fourslash_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/microsoft/typescript-go/internal/fourslash" | ||
| . "github.com/microsoft/typescript-go/internal/fourslash/tests/util" | ||
| "github.com/microsoft/typescript-go/internal/testutil" | ||
| ) | ||
|
|
||
| func TestCompletionsUnterminatedLiteral(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| const content = `// @noLib: true | ||
| function foo(a"/*1*/` | ||
| f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
| defer done() | ||
| f.VerifyCompletions(t, "1", &fourslash.CompletionsExpectedList{ | ||
| ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{ | ||
| CommitCharacters: &DefaultCommitCharacters, | ||
| }, | ||
| Items: &fourslash.CompletionsExpectedItems{}, | ||
| }) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.