Skip to content

Commit

Permalink
fix(parser): directive is only for statement consisting entirely of a…
Browse files Browse the repository at this point in the history
… string literal

closes #99
  • Loading branch information
3cp committed Oct 6, 2020
1 parent 7a7fc76 commit 8186dc1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ export function parseDirective(
matchOrInsertSemicolon(parser, context | Context.AllowRegExp);
}

return context & Context.OptionsDirectives
return context & Context.OptionsDirectives && expression.type === 'Literal' && typeof expression.value === 'string'
? finishNode(parser, context, start, line, column, {
type: 'ExpressionStatement',
expression,
Expand Down
75 changes: 74 additions & 1 deletion test/parser/expressions/in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,81 @@ describe('Expressions -In', () => {
name: '__proto'
},
operator: 'in'
}
}
]
}
],
[
'"use strict"',
Context.OptionsRaw | Context.OptionsDirectives,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'Literal',
value: 'use strict',
raw: '"use strict"'
},
directive: 'use strict'
}
]
}
],
[
'"any-string"',
Context.OptionsRaw | Context.OptionsDirectives,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'Literal',
value: 'any-string',
raw: '"any-string"'
},
directive: 'valueOf'
directive: 'any-string'
}
]
}
],
[
'"any-string"',
Context.OptionsRaw,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'Literal',
value: 'any-string',
raw: '"any-string"'
}
}
]
}
],
[
'123',
Context.OptionsRaw | Context.OptionsDirectives,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'Literal',
value: 123,
raw: '123'
}
}
]
}
Expand Down

0 comments on commit 8186dc1

Please sign in to comment.