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

Commit bb0df12

Browse files
committed
Add support for ES6 HTML-style comments
Also end comments when a </script> tag is encountered (bad solution, but that's the best we can do with regex right now)
1 parent d61d14e commit bb0df12

File tree

2 files changed

+70
-12
lines changed

2 files changed

+70
-12
lines changed

grammars/javascript.cson

+37-12
Original file line numberDiff line numberDiff line change
@@ -803,15 +803,6 @@
803803
{
804804
'include': '#comments'
805805
}
806-
{
807-
'captures':
808-
'0':
809-
'name': 'punctuation.definition.comment.html.js'
810-
'2':
811-
'name': 'punctuation.definition.comment.html.js'
812-
'match': '(<!--|-->)'
813-
'name': 'comment.block.html.js'
814-
}
815806
{
816807
'match': '(?<!\\.)\\b(class|enum|function|interface)(?!\\s*:)\\b'
817808
'name': 'storage.type.js'
@@ -1629,15 +1620,15 @@
16291620
'include': '#docblock'
16301621
}
16311622
]
1632-
'end': '\\*/'
1623+
'end': '\\*/|(?=</script>)' # Unfortunately, we don't know when we're embedded, so this is as good as it gets
16331624
'name': 'comment.block.documentation.js'
16341625
}
16351626
{
16361627
'begin': '/\\*'
16371628
'captures':
16381629
'0':
16391630
'name': 'punctuation.definition.comment.js'
1640-
'end': '\\*/'
1631+
'end': '\\*/|(?=</script>)' # Unfortunately, we don't know when we're embedded, so this is as good as it gets
16411632
'name': 'comment.block.js'
16421633
}
16431634
{
@@ -1652,9 +1643,43 @@
16521643
'beginCaptures':
16531644
'0':
16541645
'name': 'punctuation.definition.comment.js'
1655-
'end': '\\n'
1646+
'end': '$|(?=</script>)' # Unfortunately, we don't know when we're embedded, so this is as good as it gets
16561647
'name': 'comment.line.double-slash.js'
16571648
}
16581649
]
16591650
}
1651+
{
1652+
'begin': '(^[ \\t]+)?(?=<!--)'
1653+
'beginCaptures':
1654+
'1':
1655+
'name': 'punctuation.whitespace.comment.leading.js'
1656+
'end': '(?!\\G)'
1657+
'patterns': [
1658+
{
1659+
'begin': '<!--'
1660+
'beginCaptures':
1661+
'0':
1662+
'name': 'punctuation.definition.comment.html.js'
1663+
'end': '$|(?=</script>)' # Unfortunately, we don't know when we're embedded, so this is as good as it gets
1664+
'name': 'comment.line.html.js'
1665+
}
1666+
]
1667+
}
1668+
{
1669+
'begin': '(^[ \\t]+)?(?=-->)'
1670+
'beginCaptures':
1671+
'1':
1672+
'name': 'punctuation.whitespace.comment.leading.js'
1673+
'end': '(?!\\G)'
1674+
'patterns': [
1675+
{
1676+
'begin': '-->'
1677+
'beginCaptures':
1678+
'0':
1679+
'name': 'punctuation.definition.comment.html.js'
1680+
'end': '$|(?=</script>)' # Unfortunately, we don't know when we're embedded, so this is as good as it gets
1681+
'name': 'comment.line.html.js'
1682+
}
1683+
]
1684+
}
16601685
]

spec/javascript-spec.coffee

+33
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,39 @@ describe "Javascript grammar", ->
15631563
expect(tokens[7]).toEqual value: ', p2 ', scopes: ['source.js', 'meta.function.js', 'meta.parameters.js', 'comment.block.js']
15641564
expect(tokens[8]).toEqual value: '*/', scopes: ['source.js', 'meta.function.js', 'meta.parameters.js', 'comment.block.js', 'punctuation.definition.comment.js']
15651565

1566+
it "tokenizes HTML-style comments correctly", ->
1567+
{tokens} = grammar.tokenizeLine '<!-- comment'
1568+
expect(tokens[0]).toEqual value: '<!--', scopes: ['source.js', 'comment.line.html.js', 'punctuation.definition.comment.html.js']
1569+
expect(tokens[1]).toEqual value: ' comment', scopes: ['source.js', 'comment.line.html.js']
1570+
1571+
{tokens} = grammar.tokenizeLine '--> comment'
1572+
expect(tokens[0]).toEqual value: '-->', scopes: ['source.js', 'comment.line.html.js', 'punctuation.definition.comment.html.js']
1573+
expect(tokens[1]).toEqual value: ' comment', scopes: ['source.js', 'comment.line.html.js']
1574+
1575+
it "stops comments when a </script> tag is encountered", ->
1576+
# HTML doesn't count comments if they're followed by a </script> tag. Unfortunately we have
1577+
# no idea if we're embedded or not, so we err on the side of caution and always assume that we are :/
1578+
1579+
{tokens} = grammar.tokenizeLine '/* </script>'
1580+
expect(tokens[0]).toEqual value: '/*', scopes: ['source.js', 'comment.block.js', 'punctuation.definition.comment.js']
1581+
expect(tokens[1]).not.toEqual value: ' </script>', scopes: ['source.js', 'comment.block.js']
1582+
1583+
{tokens} = grammar.tokenizeLine '/** </script>'
1584+
expect(tokens[0]).toEqual value: '/**', scopes: ['source.js', 'comment.block.documentation.js', 'punctuation.definition.comment.js']
1585+
expect(tokens[1]).not.toEqual value: ' </script>', scopes: ['source.js', 'comment.block.documentation.js']
1586+
1587+
{tokens} = grammar.tokenizeLine '// </script>'
1588+
expect(tokens[0]).toEqual value: '//', scopes: ['source.js', 'comment.line.double-slash.js', 'punctuation.definition.comment.js']
1589+
expect(tokens[1]).not.toEqual value: ' </script>', scopes: ['source.js', 'comment.line.double-slash.js']
1590+
1591+
{tokens} = grammar.tokenizeLine '<!-- </script>'
1592+
expect(tokens[0]).toEqual value: '<!--', scopes: ['source.js', 'comment.line.html.js', 'punctuation.definition.comment.html.js']
1593+
expect(tokens[1]).not.toEqual value: ' </script>', scopes: ['source.js', 'comment.line.html.js']
1594+
1595+
{tokens} = grammar.tokenizeLine '--> </script>'
1596+
expect(tokens[0]).toEqual value: '-->', scopes: ['source.js', 'comment.line.html.js', 'punctuation.definition.comment.html.js']
1597+
expect(tokens[1]).not.toEqual value: ' </script>', scopes: ['source.js', 'comment.line.html.js']
1598+
15661599
describe "console", ->
15671600
it "tokenizes the console keyword", ->
15681601
{tokens} = grammar.tokenizeLine('console;')

0 commit comments

Comments
 (0)