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

Commit

Permalink
Merge pull request #441 from DamnedScholar/master
Browse files Browse the repository at this point in the history
Added template matching following innerHTML
  • Loading branch information
Wliu authored Nov 3, 2016
2 parents 3228dac + 96c3bda commit 3da89a1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 12 deletions.
34 changes: 34 additions & 0 deletions grammars/javascript.cson
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,40 @@
}
]
}
{
'begin': '(?<=innerHTML)\\s*(\\+?=)\\s*(?=`)'
'beginCaptures':
'1':
'name': 'keyword.operator.assignment.js'
'end': '(?<=`)'
'endCaptures':
'0':
'name': 'punctuation.definition.string.end.js'
'contentName': 'string.quoted.template.html.js'
'patterns': [
{
'begin': '`'
'beginCaptures':
'0':
'name': 'punctuation.definition.string.begin.js'
'end': '`'
'endCaptures':
'0':
'name': 'punctuation.definition.string.end.js'
'patterns': [
{
'include': '#string_escapes'
}
{
'include': '#interpolated_js'
}
{
'include': 'text.html.basic'
}
]
}
]
}
{
'begin': '(Relay\\.QL)\\s*(`)'
'beginCaptures':
Expand Down
41 changes: 29 additions & 12 deletions spec/javascript-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -603,20 +603,37 @@ describe "JavaScript grammar", ->
describe "ES6 tagged HTML string templates", ->
it "tokenizes them as strings", ->
{tokens} = grammar.tokenizeLine('html`hey <b>${name}</b>`')
expect(tokens[0]).toEqual value: 'html', scopes: [ 'source.js', 'string.quoted.template.html.js', 'entity.name.function.js' ]
expect(tokens[1]).toEqual value: '`', scopes: [ 'source.js', 'string.quoted.template.html.js', 'punctuation.definition.string.begin.js' ]
expect(tokens[0]).toEqual value: 'html', scopes: ['source.js', 'string.quoted.template.html.js', 'entity.name.function.js']
expect(tokens[1]).toEqual value: '`', scopes: ['source.js', 'string.quoted.template.html.js', 'punctuation.definition.string.begin.js']
expect(tokens[2]).toEqual value: 'hey <b>', scopes: ['source.js', 'string.quoted.template.html.js']
expect(tokens[3]).toEqual value: '${', scopes: ['source.js', 'string.quoted.template.html.js', 'source.js.embedded.source', 'punctuation.section.embedded.js']
expect(tokens[4]).toEqual value: 'name', scopes: ['source.js', 'string.quoted.template.html.js', 'source.js.embedded.source']
expect(tokens[5]).toEqual value: '}', scopes: ['source.js', 'string.quoted.template.html.js', 'source.js.embedded.source', 'punctuation.section.embedded.js']
expect(tokens[6]).toEqual value: '</b>', scopes: ['source.js', 'string.quoted.template.html.js']
expect(tokens[7]).toEqual value: '`', scopes: ['source.js', 'string.quoted.template.html.js', 'punctuation.definition.string.end.js']

describe "innerHTML attribute declarations with string template tags", ->
it "tokenizes them as strings", ->
{tokens} = grammar.tokenizeLine('text.innerHTML = `hey <b>${name}</b>`')
expect(tokens[0]).toEqual value: 'text', scopes: ['source.js', 'variable.other.object.js']
expect(tokens[1]).toEqual value: '.', scopes: ['source.js', 'meta.delimiter.property.period.js']
expect(tokens[2]).toEqual value: 'innerHTML', scopes: ['source.js', 'variable.other.property.js']
expect(tokens[3]).toEqual value: ' ', scopes: ['source.js']
expect(tokens[4]).toEqual value: '=', scopes: ['source.js', 'keyword.operator.assignment.js']
expect(tokens[5]).toEqual value: ' ', scopes: ['source.js']
expect(tokens[6]).toEqual value: '`', scopes: ['source.js', 'string.quoted.template.html.js', 'punctuation.definition.string.begin.js']
expect(tokens[7]).toEqual value: 'hey <b>', scopes: ['source.js', 'string.quoted.template.html.js']
expect(tokens[8]).toEqual value: '${', scopes: ['source.js', 'string.quoted.template.html.js', 'source.js.embedded.source', 'punctuation.section.embedded.js']
expect(tokens[9]).toEqual value: 'name', scopes: ['source.js', 'string.quoted.template.html.js', 'source.js.embedded.source']
expect(tokens[10]).toEqual value: '}', scopes: ['source.js', 'string.quoted.template.html.js', 'source.js.embedded.source', 'punctuation.section.embedded.js']
expect(tokens[11]).toEqual value: '</b>', scopes: ['source.js', 'string.quoted.template.html.js']
expect(tokens[12]).toEqual value: '`', scopes: ['source.js', 'string.quoted.template.html.js', 'punctuation.definition.string.end.js']

describe "ES6 tagged HTML string templates with expanded function name", ->
it "tokenizes them as strings", ->
{tokens} = grammar.tokenizeLine('escapeHTML`hey <b>${name}</b>`')
expect(tokens[0]).toEqual value: 'escapeHTML', scopes: [ 'source.js', 'string.quoted.template.html.js', 'entity.name.function.js' ]
expect(tokens[1]).toEqual value: '`', scopes: [ 'source.js', 'string.quoted.template.html.js', 'punctuation.definition.string.begin.js' ]
expect(tokens[0]).toEqual value: 'escapeHTML', scopes: ['source.js', 'string.quoted.template.html.js', 'entity.name.function.js']
expect(tokens[1]).toEqual value: '`', scopes: ['source.js', 'string.quoted.template.html.js', 'punctuation.definition.string.begin.js']
expect(tokens[2]).toEqual value: 'hey <b>', scopes: ['source.js', 'string.quoted.template.html.js']
expect(tokens[3]).toEqual value: '${', scopes: ['source.js', 'string.quoted.template.html.js', 'source.js.embedded.source', 'punctuation.section.embedded.js']
expect(tokens[4]).toEqual value: 'name', scopes: ['source.js', 'string.quoted.template.html.js', 'source.js.embedded.source']
Expand All @@ -627,9 +644,9 @@ describe "JavaScript grammar", ->
describe "ES6 tagged HTML string templates with expanded function name and white space", ->
it "tokenizes them as strings", ->
{tokens} = grammar.tokenizeLine('escapeHTML `hey <b>${name}</b>`')
expect(tokens[0]).toEqual value: 'escapeHTML', scopes: [ 'source.js', 'string.quoted.template.html.js', 'entity.name.function.js' ]
expect(tokens[1]).toEqual value: ' ', scopes: [ 'source.js', 'string.quoted.template.html.js' ]
expect(tokens[2]).toEqual value: '`', scopes: [ 'source.js', 'string.quoted.template.html.js', 'punctuation.definition.string.begin.js' ]
expect(tokens[0]).toEqual value: 'escapeHTML', scopes: ['source.js', 'string.quoted.template.html.js', 'entity.name.function.js']
expect(tokens[1]).toEqual value: ' ', scopes: ['source.js', 'string.quoted.template.html.js']
expect(tokens[2]).toEqual value: '`', scopes: ['source.js', 'string.quoted.template.html.js', 'punctuation.definition.string.begin.js']
expect(tokens[3]).toEqual value: 'hey <b>', scopes: ['source.js', 'string.quoted.template.html.js']
expect(tokens[4]).toEqual value: '${', scopes: ['source.js', 'string.quoted.template.html.js', 'source.js.embedded.source', 'punctuation.section.embedded.js']
expect(tokens[5]).toEqual value: 'name', scopes: ['source.js', 'string.quoted.template.html.js', 'source.js.embedded.source']
Expand All @@ -640,16 +657,16 @@ describe "JavaScript grammar", ->
describe "ES6 tagged Relay.QL string templates", ->
it "tokenizes them as strings", ->
{tokens} = grammar.tokenizeLine('Relay.QL`fragment on Foo { id }`')
expect(tokens[0]).toEqual value: 'Relay.QL', scopes: [ 'source.js', 'string.quoted.template.graphql.js', 'entity.name.function.js' ]
expect(tokens[1]).toEqual value: '`', scopes: [ 'source.js', 'string.quoted.template.graphql.js', 'punctuation.definition.string.begin.js' ]
expect(tokens[0]).toEqual value: 'Relay.QL', scopes: ['source.js', 'string.quoted.template.graphql.js', 'entity.name.function.js']
expect(tokens[1]).toEqual value: '`', scopes: ['source.js', 'string.quoted.template.graphql.js', 'punctuation.definition.string.begin.js']
expect(tokens[2]).toEqual value: 'fragment on Foo { id }', scopes: ['source.js', 'string.quoted.template.graphql.js']
expect(tokens[3]).toEqual value: '`', scopes: ['source.js', 'string.quoted.template.graphql.js', 'punctuation.definition.string.end.js']

describe "ES6 tagged Relay.QL string templates with interpolation", ->
it "tokenizes them as strings", ->
{tokens} = grammar.tokenizeLine('Relay.QL`fragment on Foo { ${myFragment} }`')
expect(tokens[0]).toEqual value: 'Relay.QL', scopes: [ 'source.js', 'string.quoted.template.graphql.js', 'entity.name.function.js' ]
expect(tokens[1]).toEqual value: '`', scopes: [ 'source.js', 'string.quoted.template.graphql.js', 'punctuation.definition.string.begin.js' ]
expect(tokens[0]).toEqual value: 'Relay.QL', scopes: ['source.js', 'string.quoted.template.graphql.js', 'entity.name.function.js']
expect(tokens[1]).toEqual value: '`', scopes: ['source.js', 'string.quoted.template.graphql.js', 'punctuation.definition.string.begin.js']
expect(tokens[2]).toEqual value: 'fragment on Foo { ', scopes: ['source.js', 'string.quoted.template.graphql.js']
expect(tokens[3]).toEqual value: '${', scopes: ['source.js', 'string.quoted.template.graphql.js', 'source.js.embedded.source', 'punctuation.section.embedded.js']
expect(tokens[4]).toEqual value: 'myFragment', scopes: ['source.js', 'string.quoted.template.graphql.js', 'source.js.embedded.source']
Expand Down Expand Up @@ -1657,7 +1674,7 @@ describe "JavaScript grammar", ->
expect(tokens[5]).toEqual value: 'x', scopes: ['source.js', 'meta.method-call.js', 'meta.arguments.js', scope]
expect(tokens[6]).toEqual value: delim, scopes: ['source.js', 'meta.method-call.js', 'meta.arguments.js', scope, 'punctuation.definition.string.end.js']
expect(tokens[8]).toEqual value: '+', scopes: ['source.js', 'meta.method-call.js', 'meta.arguments.js', 'keyword.operator.js']
expect(tokens[9]).toEqual value: ' y ', scopes: ['source.js', 'meta.method-call.js', 'meta.arguments.js' ]
expect(tokens[9]).toEqual value: ' y ', scopes: ['source.js', 'meta.method-call.js', 'meta.arguments.js']
expect(tokens[10]).toEqual value: '+', scopes: ['source.js', 'meta.method-call.js', 'meta.arguments.js', 'keyword.operator.js']
expect(tokens[12]).toEqual value: delim, scopes: ['source.js', 'meta.method-call.js', 'meta.arguments.js', scope, 'punctuation.definition.string.begin.js']
expect(tokens[13]).toEqual value: ':function()', scopes: ['source.js', 'meta.method-call.js', 'meta.arguments.js', scope]
Expand Down

0 comments on commit 3da89a1

Please sign in to comment.