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

Add support for ES6 HTML-style comments #371

Closed
wants to merge 2 commits into from
Closed
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
48 changes: 38 additions & 10 deletions grammars/javascript.cson
Original file line number Diff line number Diff line change
Expand Up @@ -827,15 +827,6 @@
{
'include': '#comments'
}
{
'match': '(<!--|-->)'
'captures':
'0':
'name': 'punctuation.definition.comment.html.js'
'2':
'name': 'punctuation.definition.comment.html.js'
'name': 'comment.block.html.js'
}
{
'match': '(?<!\\.)\\b(class|enum|function|interface)(?!\\s*:)\\b'
'name': 'storage.type.js'
Expand Down Expand Up @@ -1971,12 +1962,15 @@
'include': '#docblock'
}
]
'end': '\\*/|(?=</script>)' # Unfortunately, we don't know when we're embedded, so this is as good as it gets
'name': 'comment.block.documentation.js'
}
{
'begin': '/\\*'
'beginCaptures':
'0':
'name': 'punctuation.definition.comment.js'
'end': '\\*/|(?=</script>)' # Unfortunately, we don't know when we're embedded, so this is as good as it gets
'end': '\\*/'
'endCaptures':
'0':
Expand All @@ -1995,11 +1989,45 @@
'beginCaptures':
'0':
'name': 'punctuation.definition.comment.js'
'end': '\\n'
'end': '$|(?=</script>)' # Unfortunately, we don't know when we're embedded, so this is as good as it gets
'name': 'comment.line.double-slash.js'
}
]
}
{
'begin': '(^[ \\t]+)?(?=<!--)'
'beginCaptures':
'1':
'name': 'punctuation.whitespace.comment.leading.js'
'end': '(?!\\G)'
'patterns': [
{
'begin': '<!--'
'beginCaptures':
'0':
'name': 'punctuation.definition.comment.html.js'
'end': '$|(?=</script>)' # Unfortunately, we don't know when we're embedded, so this is as good as it gets
'name': 'comment.line.html.js'
}
]
}
{
'begin': '(^[ \\t]+)?(?=-->)'
'beginCaptures':
'1':
'name': 'punctuation.whitespace.comment.leading.js'
'end': '(?!\\G)'
'patterns': [
{
'begin': '-->'
'beginCaptures':
'0':
'name': 'punctuation.definition.comment.html.js'
'end': '$|(?=</script>)' # Unfortunately, we don't know when we're embedded, so this is as good as it gets
'name': 'comment.line.html.js'
}
]
}
]
'switch_statement':
'patterns': [
Expand Down
33 changes: 33 additions & 0 deletions spec/javascript-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,39 @@ describe "Javascript grammar", ->
expect(tokens[7]).toEqual value: ', p2 ', scopes: ['source.js', 'meta.function.js', 'meta.parameters.js', 'comment.block.js']
expect(tokens[8]).toEqual value: '*/', scopes: ['source.js', 'meta.function.js', 'meta.parameters.js', 'comment.block.js', 'punctuation.definition.comment.js']

it "tokenizes HTML-style comments correctly", ->
{tokens} = grammar.tokenizeLine '<!-- comment'
expect(tokens[0]).toEqual value: '<!--', scopes: ['source.js', 'comment.line.html.js', 'punctuation.definition.comment.html.js']
expect(tokens[1]).toEqual value: ' comment', scopes: ['source.js', 'comment.line.html.js']

{tokens} = grammar.tokenizeLine '--> comment'
expect(tokens[0]).toEqual value: '-->', scopes: ['source.js', 'comment.line.html.js', 'punctuation.definition.comment.html.js']
expect(tokens[1]).toEqual value: ' comment', scopes: ['source.js', 'comment.line.html.js']

it "stops comments when a </script> tag is encountered", ->
# HTML doesn't count comments if they're followed by a </script> tag. Unfortunately we have
# no idea if we're embedded or not, so we err on the side of caution and always assume that we are :/

{tokens} = grammar.tokenizeLine '/* </script>'
expect(tokens[0]).toEqual value: '/*', scopes: ['source.js', 'comment.block.js', 'punctuation.definition.comment.js']
expect(tokens[1]).not.toEqual value: ' </script>', scopes: ['source.js', 'comment.block.js']

{tokens} = grammar.tokenizeLine '/** </script>'
expect(tokens[0]).toEqual value: '/**', scopes: ['source.js', 'comment.block.documentation.js', 'punctuation.definition.comment.js']
expect(tokens[1]).not.toEqual value: ' </script>', scopes: ['source.js', 'comment.block.documentation.js']

{tokens} = grammar.tokenizeLine '// </script>'
expect(tokens[0]).toEqual value: '//', scopes: ['source.js', 'comment.line.double-slash.js', 'punctuation.definition.comment.js']
expect(tokens[1]).not.toEqual value: ' </script>', scopes: ['source.js', 'comment.line.double-slash.js']

{tokens} = grammar.tokenizeLine '<!-- </script>'
expect(tokens[0]).toEqual value: '<!--', scopes: ['source.js', 'comment.line.html.js', 'punctuation.definition.comment.html.js']
expect(tokens[1]).not.toEqual value: ' </script>', scopes: ['source.js', 'comment.line.html.js']

{tokens} = grammar.tokenizeLine '--> </script>'
expect(tokens[0]).toEqual value: '-->', scopes: ['source.js', 'comment.line.html.js', 'punctuation.definition.comment.html.js']
expect(tokens[1]).not.toEqual value: ' </script>', scopes: ['source.js', 'comment.line.html.js']

describe "console", ->
it "tokenizes the console keyword", ->
{tokens} = grammar.tokenizeLine('console;')
Expand Down