Skip to content

Commit

Permalink
Support hashbang comments
Browse files Browse the repository at this point in the history
FIX: Properly parse hashbang comments.

Closes codemirror/dev#1236
  • Loading branch information
marijnh committed Aug 23, 2023
1 parent 6bfbf41 commit 7512c95
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const jsHighlight = styleTags({
PropertyDefinition: t.definition(t.propertyName),
PrivatePropertyDefinition: t.definition(t.special(t.propertyName)),
UpdateOp: t.updateOperator,
LineComment: t.lineComment,
"LineComment Hashbang": t.lineComment,
BlockComment: t.blockComment,
Number: t.number,
String: t.string,
Expand Down
4 changes: 3 additions & 1 deletion src/javascript.grammar
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
predicate
}

@top Script { statement+ }
@top Script { Hashbang? statement+ }

@top SingleExpression { expression }

Expand Down Expand Up @@ -609,6 +609,8 @@ questionOp[@name=LogicOp] { "?" }

LineComment { "//" ![\n]* }

Hashbang { "#!" ![\n]* }

divide[@name=ArithOp] { "/" }

@precedence { "/*", LineComment, divide }
Expand Down
9 changes: 9 additions & 0 deletions test/statement.txt
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,12 @@ Script(
ClassDeclaration(class,VariableDefinition,ClassBody(
MethodDeclaration(Star,PropertyDefinition,ParamList,Block))),
ExpressionStatement(ParenthesizedExpression(ObjectExpression(Property(Star,PropertyDefinition,ParamList,Block)))))

# Hashbang

#!/bin/env node
foo()

==>

Script(Hashbang,ExpressionStatement(CallExpression(VariableName,ArgList)))

0 comments on commit 7512c95

Please sign in to comment.