This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 237
Fix tokenizing of various keywords within ternary expression #228
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,33 @@ describe "Javascript grammar", -> | |
| {tokens} = grammar.tokenizeLine('with') | ||
| expect(tokens[0]).toEqual value: 'with', scopes: ['source.js', 'keyword.control.js'] | ||
|
|
||
| map = | ||
| super: 'variable.language.js' | ||
| this: 'variable.language.js' | ||
| null: 'constant.language.null.js' | ||
| true: 'constant.language.boolean.true.js' | ||
| false: 'constant.language.boolean.false.js' | ||
| debugger: 'keyword.other.js' | ||
| exports: 'support.variable.js' | ||
| __filename: 'support.variable.js' | ||
|
|
||
| for keyword, scope of map | ||
| do (keyword, scope) -> | ||
| it "does not tokenize `#{keyword}` when it is an object key", -> | ||
| {tokens} = grammar.tokenizeLine("#{keyword}: 1") | ||
| expect(tokens[0]).toEqual value: keyword, scopes: ['source.js'] | ||
| expect(tokens[1]).toEqual value: ':', scopes: ['source.js', 'keyword.operator.js'] | ||
|
|
||
| it "tokenizes `#{keyword}` in the middle of ternary expressions", -> | ||
| {tokens} = grammar.tokenizeLine("a ? #{keyword} : b") | ||
| expect(tokens[2]).toEqual value: ' ', scopes: ['source.js'] | ||
| expect(tokens[3]).toEqual value: keyword, scopes: ['source.js', scope] | ||
|
|
||
|
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. Just to be comprehensive can you also add a test for 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. Sure, good call |
||
| it "tokenizes `#{keyword}` at the end of ternary expressions", -> | ||
| {tokens} = grammar.tokenizeLine("a ? b : #{keyword}") | ||
| expect(tokens[4]).toEqual value: ' ', scopes: ['source.js'] | ||
| expect(tokens[5]).toEqual value: keyword, scopes: ['source.js', scope] | ||
|
|
||
| describe "built-in globals", -> | ||
| it "tokenizes them as support classes", -> | ||
| {tokens} = grammar.tokenizeLine('window') | ||
|
|
@@ -336,6 +363,19 @@ describe "Javascript grammar", -> | |
| expect(tokens[4]).toEqual value: 'systemLanguage', scopes: ['source.js', 'support.constant.js'] | ||
| expect(tokens[5]).toEqual value: ';', scopes: ['source.js', 'punctuation.terminator.statement.js'] | ||
|
|
||
| it "does not tokenize constants when they are object keys", -> | ||
| {tokens} = grammar.tokenizeLine('FOO: 1') | ||
| expect(tokens[0]).toEqual value: 'FOO', scopes: ['source.js'] | ||
| expect(tokens[1]).toEqual value: ':', scopes: ['source.js', 'keyword.operator.js'] | ||
|
|
||
| it "tokenizes constants in the middle of ternary expressions", -> | ||
| {tokens} = grammar.tokenizeLine('a ? FOO : b') | ||
| expect(tokens[3]).toEqual value: 'FOO', scopes: ['source.js', 'constant.other.js'] | ||
|
|
||
|
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. Same thing here, a test for |
||
| it "tokenizes constants at the end of ternary expressions", -> | ||
| {tokens} = grammar.tokenizeLine('a ? b : FOO') | ||
| expect(tokens[5]).toEqual value: 'FOO', scopes: ['source.js', 'constant.other.js'] | ||
|
|
||
| describe "ES6 string templates", -> | ||
| it "tokenizes them as strings", -> | ||
| {tokens} = grammar.tokenizeLine('`hey ${name}`') | ||
|
|
@@ -399,6 +439,19 @@ describe "Javascript grammar", -> | |
| expect(tokens[0]).toEqual value: 'yield', scopes: ['source.js', 'meta.control.yield.js', 'keyword.control.js'] | ||
| expect(tokens[2]).toEqual value: '*', scopes: ['source.js', 'meta.control.yield.js', 'storage.modifier.js'] | ||
|
|
||
| it "does not tokenize yield when it is an object key", -> | ||
| {tokens} = grammar.tokenizeLine('yield: 1') | ||
| expect(tokens[0]).toEqual value: 'yield', scopes: ['source.js'] | ||
| expect(tokens[1]).toEqual value: ':', scopes: ['source.js', 'keyword.operator.js'] | ||
|
|
||
| it "tokenizes yield in the middle of ternary expressions", -> | ||
| {tokens} = grammar.tokenizeLine('a ? yield : b') | ||
| expect(tokens[3]).toEqual value: 'yield', scopes: ['source.js', 'meta.control.yield.js', 'keyword.control.js'] | ||
|
|
||
| it "tokenizes yield at the end of ternary expressions", -> | ||
| {tokens} = grammar.tokenizeLine('a ? b : yield') | ||
| expect(tokens[5]).toEqual value: 'yield', scopes: ['source.js', 'meta.control.yield.js', 'keyword.control.js'] | ||
|
|
||
| describe "default: in a switch statement", -> | ||
| it "tokenizes it as a keyword", -> | ||
| {tokens} = grammar.tokenizeLine('default: ') | ||
|
|
||
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 make it so the test itself isn't looped, only the contents within the test? So
If necessary you can keep
mapout of the tests to avoid duplication.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 like how the loop generates multiple tests because when tokenizing of one keyword breaks, only its test will fail and not the entire block. This was useful for me in development when I wrote these tests first and changed one by one regular expression to get them passing.
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 you've actually convinced me to start writing new tests this way 😄!