Skip to content

Commit

Permalink
Merge pull request #686 from DanielXMoore/684
Browse files Browse the repository at this point in the history
Fix #684: Add support for TypeScript /// directives
  • Loading branch information
STRd6 authored Sep 2, 2023
2 parents 9aece63 + 772a6c1 commit 95dd18d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 13 deletions.
13 changes: 10 additions & 3 deletions source/parser.hera
Original file line number Diff line number Diff line change
Expand Up @@ -6255,8 +6255,8 @@ Shebang
/#![^\r\n]*/ EOL

CivetPrologue
[\t ]* DoubleQuote CivetPrologueContent:content DoubleQuote $SimpleStatementDelimiter EOS? -> content
[\t ]* SingleQuote CivetPrologueContent:content SingleQuote $SimpleStatementDelimiter EOS? -> content
[\t ]* DoubleQuote CivetPrologueContent:content DoubleQuote SimpleStatementDelimiter EOS? -> content
[\t ]* SingleQuote CivetPrologueContent:content SingleQuote SimpleStatementDelimiter EOS? -> content

CivetPrologueContent
"civet" NonIdContinue CivetOption*:options [\s]* ->
Expand Down Expand Up @@ -6290,7 +6290,14 @@ UnknownPrologue
# Can't use $EOS because it will prevent re-writing of coffee style comments
[\t ]* StringLiteral:s $SimpleStatementDelimiter EOS

TripleSlashDirective
# https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html
/\/\/\/[^\r\n]*/ EOS?

DirectivePrologue
PrologueString

PrologueString
CivetPrologue
UnknownPrologue

Expand Down Expand Up @@ -6726,7 +6733,7 @@ Reset
}

Init
Shebang? DirectivePrologue*:directives "" ->
Shebang? TripleSlashDirective* DirectivePrologue*:directives ->
directives.forEach((directive) => {
if (directive.type === "CivetPrologue") {
Object.assign(module.config, directive.config)
Expand Down
21 changes: 21 additions & 0 deletions test/prologues.civet
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{testCase} from ./helper.civet

describe "shebang", ->
testCase """
keeps it at the top
---
#! /usr/bin/env node
---
#! /usr/bin/env node
"""

describe "Triple Slash directives", ->
testCase """
https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html
---
/// <reference path="..." />
x = 1
---
/// <reference path="..." />
x = 1
"""
39 changes: 39 additions & 0 deletions test/regex.civet
Original file line number Diff line number Diff line change
Expand Up @@ -76,169 +76,207 @@ describe "regexp", ->
Math.floor(x / y) // comment
"""

// NOTE: Need the ';' so that the heregex isn't interperted as a https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html
describe "heregex", ->
testCase """
basic
---
;
///abc///
---
;
/abc/
"""

testCase """
flags
---
;
///abc///g
---
;
/abc/g
"""

testCase """
escapes
---
;
///\\u200C///
---
;
/\\u200C/
"""

testCase """
strips whitespace
---
;
/// abc ///
---
;
/abc/
"""

testCase """
escapes single slashes
---
;
/// / ///
---
;
/\\//
"""

testCase """
escaped space
---
;
///\\ ///
---
;
/ /
"""

testCase """
escaped newline
---
;
///\\\n///
---
;
/\\n/
"""

testCase """
space in character class
---
;
///[ ]///
---
;
/[ ]/
"""

testCase """
comment in character class
---
;
///[/*]///
---
;
/[/*]/
"""

testCase """
coffee comment in character class
---
;
///[ # hey ]///
---
;
/[ # hey ]/
"""

testCase """
triple slash inside character class
---
;
///a[///]///
---
;
/a[///]/
"""

testCase """
JS comment
---
;
///
abb // hey
///
---
;
/abb/
"""

testCase '''
substitutions
---
;
///#{a}///
---
;
RegExp(`${a}`)
'''

testCase '''
substitutions escape backticks
---
;
///
`#{a}
///
---
;
RegExp(`\\`${a}`)
'''

testCase '''
allows both kinds of substitutions
---
;
///
${yo}
$ { x }
#{a}
///
---
;
RegExp(`${yo}\\${x}${a}`)
'''

testCase '''
escaped bracket
---
;
///
\\[
///
---
;
/\\[/
'''

testCase '''
escaped bracket with substitution
---
;
///
\\[#{a}]
///
---
;
RegExp(`\\\\[${a}]`)
'''

testCase '''
substitutions keep flags
---
;
///
x#{a}
///g
---
;
RegExp(`x${a}`, "g")
'''

// NOTE: Added escapes for '#' to work around https://github.com/jashkenas/coffeescript/issues/5428
testCase '''
coffee script's heregex
---
;
/// ^
(?:
# Match any character, except those that need special handling below.
Expand All @@ -254,5 +292,6 @@ describe "regexp", ->
)*
///
---
;
/^(?:[^\\\\/\\#\\s]|\\\\[\\s\\S]|\\/(?!\\/\\/)|\\#(?!\\{)|\\s+(?:\\#(?!\\{).*)?)*/
'''
10 changes: 0 additions & 10 deletions test/shebang.civet

This file was deleted.

0 comments on commit 95dd18d

Please sign in to comment.