Skip to content

Commit

Permalink
fix(parser): fixed issue with a directive preceding an 'use strict' d…
Browse files Browse the repository at this point in the history
…irective containing an OctalEscapeSequence
  • Loading branch information
KFlash committed Jun 4, 2019
1 parent 7ffdea3 commit 84bd498
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "meriyah",
"version": "0.2.5",
"version": "0.2.7",
"description": "A 100% compliant, self-hosted javascript parser with high focus on both performance and stability",
"main": "dist/meriyah.umd.js",
"module": "dist/meriyah.esm.js",
Expand Down
6 changes: 3 additions & 3 deletions src/lexer/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export function skipHashBang(parser: ParserState): void {
let index = parser.index;
if (index === parser.end) return;
if (parser.nextCP === Chars.ByteOrderMark) {
parser.nextCP = parser.source.charCodeAt(++index);
parser.index = index;
parser.index = ++index;
parser.nextCP = parser.source.charCodeAt(index);
}

if (index < parser.end && parser.source.charCodeAt(index) === Chars.Hash) {
if (index < parser.end && parser.nextCP === Chars.Hash) {
index++;
if (index < parser.end && parser.source.charCodeAt(index) === Chars.Exclamation) {
parser.index = index + 1;
Expand Down
3 changes: 1 addition & 2 deletions src/lexer/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ export function isExoticECMAScriptWhitespace(code: number): boolean {
*/
return (
code === Chars.NonBreakingSpace ||
code === Chars.ZeroWidthNoBreakSpace ||
code === Chars.NextLine ||
code === Chars.Ogham ||
(code >= Chars.EnQuad && code <= Chars.ZeroWidthSpace) ||
code === Chars.NarrowNoBreakSpace ||
code === Chars.ZeroWidthJoiner ||
code === Chars.ZeroWidthNonJoiner ||
code === Chars.MathematicalSpace ||
code === Chars.IdeographicSpace ||
code === Chars.ByteOrderMark
Expand Down
6 changes: 4 additions & 2 deletions src/lexer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,10 +505,12 @@ export function scanSingleToken(parser: ParserState, context: Context, state: Sc
parser.line++;
continue;
}
if (isIDStart(parser.nextCP) || consumeMultiUnitCodePoint(parser, parser.nextCP)) {

if (isIDStart(first) || consumeMultiUnitCodePoint(parser, first)) {
return scanIdentifier(parser, context);
}
if (isExoticECMAScriptWhitespace(parser.nextCP)) {

if (isExoticECMAScriptWhitespace(first)) {
nextCodePoint(parser);
continue;
}
Expand Down
7 changes: 5 additions & 2 deletions src/lexer/string.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ParserState, Context } from '../common';
import { ParserState, Context, Flags } from '../common';
import { Token } from '../token';
import { Chars } from '../chars';
import { report, Errors } from '../errors';
Expand Down Expand Up @@ -115,7 +115,6 @@ export function parseEscape(parser: ParserState, context: Context, first: number
code = (code << 3) | (next - Chars.Zero);
index++;
column++;

if (index < parser.end) {
const next = parser.source.charCodeAt(index);

Expand All @@ -132,6 +131,8 @@ export function parseEscape(parser: ParserState, context: Context, first: number
}
}

parser.flags |= Flags.Octals;

return code;
}

Expand All @@ -156,6 +157,8 @@ export function parseEscape(parser: ParserState, context: Context, first: number
}
}

parser.flags |= Flags.Octals;

return code;
}

Expand Down
3 changes: 3 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2645,6 +2645,9 @@ export function parseFunctionBody(
if (parser.flags & Flags.SimpleParameterList) {
reportAt(parser, parser.index, parser.line, parser.tokenIndex, Errors.IllegalUseStrict);
}
if (parser.flags & Flags.Octals) {
reportAt(parser, parser.index, parser.line, parser.tokenIndex, Errors.StrictOctalLiteral);
}
}
}
body.push(parseDirective(parser, context, expr, token, tokenIndex));
Expand Down
32 changes: 21 additions & 11 deletions test/lexer/whitespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('Lexer - Whitespace', () => {
column: 1
});

pass('skips white spacee', {
pass('skips white space', {
source: '\u0009\u000B\u000C\u0020\u00A0\u000A\u000D\u2028\u2029',
hasNext: false,
value: '',
Expand All @@ -96,6 +96,16 @@ describe('Lexer - Whitespace', () => {
column: 0
});

pass('skips paragraphseparator', {
source: '\u2029',
hasNext: false,
value: '',
newLine: true,
line: 1,
index: 1,
column: 0
});

pass('skips white space', {
source: '\true',
hasNext: false,
Expand Down Expand Up @@ -486,16 +496,6 @@ describe('Lexer - Whitespace', () => {
column: 20
});

pass('skips exotic whitespace', {
source: '\u200D\u200C',
hasNext: false,
newLine: false,
value: '',
line: 1,
index: 2,
column: 2
});

pass('skips single line comment with identifier and newline', {
source: '// foo\n',
hasNext: false,
Expand Down Expand Up @@ -688,6 +688,16 @@ describe('Lexer - Whitespace', () => {
column: 1
});

pass('skips simple exotic whitespace', {
source: '\xA0',
hasNext: true,
newLine: false,
value: '',
line: 3,
index: 1,
column: 1
});

pass('skips complex exotic whitespace', {
source: '\t\x0B\x0C\xA0\u1680\u2000\u200A\u202F\u205F\u3000',
hasNext: true,
Expand Down
46 changes: 46 additions & 0 deletions test/parser/miscellaneous/failure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,52 @@ describe('Miscellaneous - Failure', () => {
'async (break) => {"use strict";}',
'async (break) => {}',
'(foo, break) => {}',
'function a(){ "use strict"; function a(a=yield){}}',
'class A {set a(yield){}}',
'();',
'[([a])] = 12;',
'(a, ...b);',
'var e = [a -= 12] = 5',
'function l() { "\\12"; "use strict" }',
'function l() { "\\00002"; "use strict" }',
'function l() { "\\712"; "use strict" }',
'function l() { "\\12"; "use strict" }',
'[ a -= 12 ] = 12;',
'(...a)',
'(a, ...b)',
'(((...a)))',
'0++',
'0--',
'let let;',
'({a: 0} = 0);',
'({get a(){}} = 0)',
'({a}) = 0;',
'({a: 0} = 0);',
'({a} += 0);',
'[...{a: 0}] = 0;',
'({a: 0} = 0);',
'for(({a: 0}) in 0);',
'for({a: 0} of 0);',
'for(([0]) in 0);',
'\\u0000',
'for(const let = 0;;);',
'{ const a; }',
'function f(){ const a; }',
'for(const a = 0;;) label: function f(){}',
'for(;;) labelA: labelB: labelC: function f(){}',
'for(;;) labelA: labelB: labelC: function f(){}',
'for(let let in 0);',
'continue;',
'label: continue label;',
'class A { f(eval){} };',
'class A { *f(eval){} }',
'function f(a){ super.b }',
'for(let let;;);',
'(a,);',
'(((a, ...b)))',
'({ a = 0 });',
'class A extends B { constructor() { !{constructor() { super(); }}; } }',
'class A extends B { constructor() { !{get constructor() { super(); }}; } }',
'async eval => {"use strict";}',
'async (eval) => {"use strict";}',
'arguments => {"use strict";}',
Expand Down

0 comments on commit 84bd498

Please sign in to comment.