Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Allow $dollar$ signs in identifiers #272

Merged
merged 3 commits into from
Nov 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions grammars/javascript.cson
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@
]
}
{
'begin': '\\b(?:(async)(?:\\s+))?(function\\*?)\\s*((\\*)|(?:(?:\\s+)(\\*?[a-zA-Z_$]\\w*)))?\\s*(\\()'
'begin': '\\b(?:(async)(?:\\s+))?(function\\*?)\\s*((\\*)|(?:(?:\\s+)(\\*?[a-zA-Z_$][\\w$]*)))?\\s*(\\()'
'beginCaptures':
'1':
'name': 'storage.modifier.js'
Expand Down Expand Up @@ -513,7 +513,17 @@
'include': '#methods'
}
{
'match': '\\b(class)(?:(?:\\s+(extends)\\s+(\\w+(?:\\.\\w*)?))|(?:(?:\\s+(\\w+(?:\\.\\w*)?))(?:\\s+(extends)\\s+(\\w+(?:\\.\\w*)?))?))'
'match': '''(?x)
\\b(class)
(?:
(?:\\s+(extends)\\s+([a-zA-Z_$][\\w$]*))
|
(?:
(?:\\s+([a-zA-Z_$][\\w$]*))
(?:\\s+(extends)\\s+([a-zA-Z_$][\\w$]*))?
)
)
'''
Copy link
Contributor Author

Choose a reason for hiding this comment

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

btw, should I rebase #254?

Copy link
Contributor

Choose a reason for hiding this comment

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

We haven't decided whether or not to proceed with that one yet. If we do, I'll ask you to :).

Also, can you remove the spaces in this regex? I think they attract too much attention to themselves for some reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

'captures':
'1':
'name': 'storage.type.class.js'
Expand Down Expand Up @@ -1072,7 +1082,7 @@
'patterns': [
{
'comment': '.methodCall(arg1, "arg2", [...])'
'begin': '(\\.)(\\w+)\\s*(\\()'
'begin': '(\\.)([a-zA-Z_$][\\w$]*)\\s*(\\()'
'beginCaptures':
'1':
'name': 'meta.delimiter.method.period.js'
Expand All @@ -1096,11 +1106,13 @@
'patterns': [
{
'comment': 'functionCall(arg1, "arg2", [...])'
'begin': '\\b(\\w+)\\s*(\\()'
'begin': '(?:(\\d\\w*)|([a-zA-Z_$][\\w$]*))\\s*(\\()'
'beginCaptures':
'1':
'name': 'entity.name.function.js'
'name': 'invalid.illegal.js'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without invalid.illegal it would be ...(?:\\$+)?\\b[a-zA-Z_][\\w$]*)...

'2':
'name': 'entity.name.function.js'
'3':
'name': 'punctuation.definition.arguments.begin.js'
'end': '\\)'
'endCaptures':
Expand All @@ -1118,7 +1130,7 @@
'patterns': [
{
'comment': '`obj` in `obj.prop`, `obj.methodCall()`'
'match': '(?:(\\b[a-zA-Z_$]\\w*)|(\\d\\w*))(?=\\s*\\.\\s*[a-zA-Z_$]\\w*)'
'match': '(?:([a-zA-Z_$][\\w$]*)|(\\d\\w*))(?=\\s*\\.\\s*[a-zA-Z_$]\\w*)'
'captures':
'1':
'name': 'variable.other.object.js'
Expand All @@ -1130,7 +1142,7 @@
'patterns': [
{
'comment': '`prop` in `obj.prop`, `func().prop`'
'match': '(\\.)\\s*(?:(\\b\\d\\w*)|(\\b[A-Z][A-Z0-9_]*)\\b|(\\w*))'
'match': '(\\.)\\s*(?:(\\b\\d\\w*)|([A-Z][A-Z0-9_$]*\\b\\$*)|([\\w$]*))'
'captures':
'1':
'name': 'meta.delimiter.property.period.js'
Expand Down
64 changes: 64 additions & 0 deletions spec/javascript-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -482,19 +482,37 @@ describe "Javascript grammar", ->
expect(tokens[0]).toEqual value: 'class', scopes: ['source.js', 'meta.class.js', 'storage.type.class.js']
expect(tokens[2]).toEqual value: 'MyClass', scopes: ['source.js', 'meta.class.js', 'entity.name.type.js']

{tokens} = grammar.tokenizeLine('class $abc$')
expect(tokens[2]).toEqual value: '$abc$', scopes: ['source.js', 'meta.class.js', 'entity.name.type.js']

{tokens} = grammar.tokenizeLine('class $$')
expect(tokens[2]).toEqual value: '$$', scopes: ['source.js', 'meta.class.js', 'entity.name.type.js']

it "tokenizes class...extends", ->
{tokens} = grammar.tokenizeLine('class MyClass extends SomeClass')
expect(tokens[0]).toEqual value: 'class', scopes: ['source.js', 'meta.class.js', 'storage.type.class.js']
expect(tokens[2]).toEqual value: 'MyClass', scopes: ['source.js', 'meta.class.js', 'entity.name.type.js']
expect(tokens[4]).toEqual value: 'extends', scopes: ['source.js', 'meta.class.js', 'storage.modifier.js']
expect(tokens[6]).toEqual value: 'SomeClass', scopes: ['source.js', 'meta.class.js', 'entity.name.type.js']

{tokens} = grammar.tokenizeLine('class MyClass extends $abc$')
expect(tokens[6]).toEqual value: '$abc$', scopes: ['source.js', 'meta.class.js', 'entity.name.type.js']

{tokens} = grammar.tokenizeLine('class MyClass extends $$')
expect(tokens[6]).toEqual value: '$$', scopes: ['source.js', 'meta.class.js', 'entity.name.type.js']

it "tokenizes anonymous class", ->
{tokens} = grammar.tokenizeLine('class extends SomeClass')
expect(tokens[0]).toEqual value: 'class', scopes: ['source.js', 'meta.class.js', 'storage.type.class.js']
expect(tokens[2]).toEqual value: 'extends', scopes: ['source.js', 'meta.class.js', 'storage.modifier.js']
expect(tokens[4]).toEqual value: 'SomeClass', scopes: ['source.js', 'meta.class.js', 'entity.name.type.js']

{tokens} = grammar.tokenizeLine('class extends $abc$')
expect(tokens[4]).toEqual value: '$abc$', scopes: ['source.js', 'meta.class.js', 'entity.name.type.js']

{tokens} = grammar.tokenizeLine('class extends $$')
expect(tokens[4]).toEqual value: '$$', scopes: ['source.js', 'meta.class.js', 'entity.name.type.js']

it "tokenizes constructors", ->
{tokens} = grammar.tokenizeLine('constructor(a, b)')
expect(tokens[0]).toEqual value: 'constructor', scopes: ['source.js', 'entity.name.function.constructor.js']
Expand Down Expand Up @@ -789,6 +807,18 @@ describe "Javascript grammar", ->
expect(tokens[0]).toEqual value: 'default', scopes: ['source.js', 'keyword.control.js']

describe "non-anonymous functions", ->
it "tokenizes regular functions", ->
{tokens} = grammar.tokenizeLine('function nonAnonymous(){}')
expect(tokens[0]).toEqual value: 'function', scopes: ['source.js', 'meta.function.js', 'storage.type.function.js']
expect(tokens[2]).toEqual value: 'nonAnonymous', scopes: ['source.js', 'meta.function.js', 'entity.name.function.js']
expect(tokens[3]).toEqual value: '(', scopes: ['source.js', 'meta.function.js', 'punctuation.definition.parameters.begin.js']

{tokens} = grammar.tokenizeLine('function $abc$(){}')
expect(tokens[2]).toEqual value: '$abc$', scopes: ['source.js', 'meta.function.js', 'entity.name.function.js']

{tokens} = grammar.tokenizeLine('function $$(){}')
expect(tokens[2]).toEqual value: '$$', scopes: ['source.js', 'meta.function.js', 'entity.name.function.js']

it "tokenizes methods", ->
{tokens} = grammar.tokenizeLine('Foo.method = function nonAnonymous(')
expect(tokens[0]).toEqual value: 'Foo', scopes: ['source.js', 'meta.function.js', 'support.class.js']
Expand Down Expand Up @@ -908,6 +938,16 @@ describe "Javascript grammar", ->
expect(tokens[0]).toEqual value: 'super', scopes: ['source.js', 'variable.language.js']

describe "objects", ->
it "tokenizes them", ->
{tokens} = grammar.tokenizeLine('obj.prop')
expect(tokens[0]).toEqual value: 'obj', scopes: ['source.js', 'variable.other.object.js']

{tokens} = grammar.tokenizeLine('$abc$.prop')
expect(tokens[0]).toEqual value: '$abc$', scopes: ['source.js', 'variable.other.object.js']

{tokens} = grammar.tokenizeLine('$$.prop')
expect(tokens[0]).toEqual value: '$$', scopes: ['source.js', 'variable.other.object.js']

it "tokenizes illegal objects", ->
{tokens} = grammar.tokenizeLine('1.prop')
expect(tokens[0]).toEqual value: '1', scopes: ['source.js', 'invalid.illegal.js']
Expand Down Expand Up @@ -945,6 +985,12 @@ describe "Javascript grammar", ->
expect(tokens[15]).toEqual value: '}', scopes: ['source.js', 'meta.function-call.js', 'meta.brace.curly.js']
expect(tokens[16]).toEqual value: ')', scopes: ['source.js', 'meta.function-call.js', 'punctuation.definition.arguments.end.js']

{tokens} = grammar.tokenizeLine('$abc$()')
expect(tokens[0]).toEqual value: '$abc$', scopes: ['source.js', 'meta.function-call.js', 'entity.name.function.js']

{tokens} = grammar.tokenizeLine('$$()')
expect(tokens[0]).toEqual value: '$$', scopes: ['source.js', 'meta.function-call.js', 'entity.name.function.js']

it "tokenizes function calls when they are arguments", ->
{tokens} = grammar.tokenizeLine('a(b(c))')
expect(tokens[0]).toEqual value: 'a', scopes: ['source.js', 'meta.function-call.js', 'entity.name.function.js']
Expand All @@ -955,6 +1001,12 @@ describe "Javascript grammar", ->
expect(tokens[5]).toEqual value: ')', scopes: ['source.js', 'meta.function-call.js', 'meta.function-call.js', 'punctuation.definition.arguments.end.js']
expect(tokens[6]).toEqual value: ')', scopes: ['source.js', 'meta.function-call.js', 'punctuation.definition.arguments.end.js']

it "tokenizes illegal function calls", ->
{tokens} = grammar.tokenizeLine('0illegal()')
expect(tokens[0]).toEqual value: '0illegal', scopes: ['source.js', 'meta.function-call.js', 'invalid.illegal.js']
expect(tokens[1]).toEqual value: '(', scopes: ['source.js', 'meta.function-call.js', 'punctuation.definition.arguments.begin.js']
expect(tokens[2]).toEqual value: ')', scopes: ['source.js', 'meta.function-call.js', 'punctuation.definition.arguments.end.js']

it "tokenizes illegal arguments", ->
{tokens} = grammar.tokenizeLine('a(1a)')
expect(tokens[0]).toEqual value: 'a', scopes: ['source.js', 'meta.function-call.js', 'entity.name.function.js']
Expand Down Expand Up @@ -1004,6 +1056,12 @@ describe "Javascript grammar", ->
expect(tokens[6]).toEqual value: '1', scopes: ['source.js', 'meta.method-call.js', 'constant.numeric.js']
expect(tokens[7]).toEqual value: ')', scopes: ['source.js', 'meta.method-call.js', 'punctuation.definition.arguments.end.js']

{tokens} = grammar.tokenizeLine('a.$abc$()')
expect(tokens[2]).toEqual value: '$abc$', scopes: ['source.js', 'meta.method-call.js', 'entity.name.function.js']

{tokens} = grammar.tokenizeLine('a.$$()')
expect(tokens[2]).toEqual value: '$$', scopes: ['source.js', 'meta.method-call.js', 'entity.name.function.js']

{tokens} = grammar.tokenizeLine('gulp.src("./*.js")')
expect(tokens[0]).toEqual value: 'gulp', scopes: ['source.js', 'variable.other.object.js']
expect(tokens[1]).toEqual value: '.', scopes: ['source.js', 'meta.method-call.js', 'meta.delimiter.method.period.js']
Expand Down Expand Up @@ -1049,6 +1107,12 @@ describe "Javascript grammar", ->
expect(tokens[1]).toEqual value: '.', scopes: ['source.js', 'meta.delimiter.property.period.js']
expect(tokens[2]).toEqual value: 'Property', scopes: ['source.js', 'variable.other.property.js']

{tokens} = grammar.tokenizeLine('obj.$abc$')
expect(tokens[2]).toEqual value: '$abc$', scopes: ['source.js', 'variable.other.property.js']

{tokens} = grammar.tokenizeLine('obj.$$')
expect(tokens[2]).toEqual value: '$$', scopes: ['source.js', 'variable.other.property.js']

{tokens} = grammar.tokenizeLine('a().b')
expect(tokens[0]).toEqual value: 'a', scopes: ['source.js', 'meta.function-call.js', 'entity.name.function.js']
expect(tokens[1]).toEqual value: '(', scopes: ['source.js', 'meta.function-call.js', 'punctuation.definition.arguments.begin.js']
Expand Down