Skip to content

Commit

Permalink
fix(parser): fixed issue with module code not in strict mode in a few…
Browse files Browse the repository at this point in the history
… cases
  • Loading branch information
KFlash committed Jun 27, 2019
1 parent 5ce81b1 commit c6d24b6
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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": "1.0.0",
"version": "1.0.1",
"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
4 changes: 3 additions & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const enum Errors {
NewlineAfterThrow,
StrictWith,
IllegalReturn,
InvalidForLHSBinding,
InvalidNewTarget,
InvalidEscapedKeyword,
MissingPrivateName,
Expand Down Expand Up @@ -265,9 +266,10 @@ export const errorMessages: {
[Errors.NewlineAfterThrow]: 'Illegal newline after throw',
[Errors.StrictWith]: 'Strict mode code may not include a with statement',
[Errors.IllegalReturn]: 'Illegal return statement',
[Errors.InvalidForLHSBinding]: 'The for-header left hand side binding declaration is not destructible',
[Errors.InvalidNewTarget]: 'new.target only allowed within functions',
[Errors.InvalidEscapedKeyword]: 'Keywords must be written literally, without embedded escapes',
[Errors.MissingPrivateName]: "'#' not followed by identifier",
[Errors.MissingPrivateName]: "''#' not followed by identifier",
[Errors.InvalidStrictStatic]: '`Static` is a reserved word in strict mode',
[Errors.FutureReservedWordInStrictModeNotId]:
'The use of a future reserved word for an identifier is invalid. The identifier name is reserved in strict mode',
Expand Down
6 changes: 4 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export interface Options {
export function parseSource(source: string, options: Options | void, context: Context): ESTree.Program {
let sourceFile = '';
if (options != null) {
if (options.module) context |= Context.Module;
if (options.module) context |= Context.Module | Context.Strict;
if (options.next) context |= Context.OptionsNext;
if (options.loc) context |= Context.OptionsLoc;
if (options.ranges) context |= Context.OptionsRanges;
Expand Down Expand Up @@ -4298,7 +4298,9 @@ export function parseFunctionDeclaration(

// A function behaves as a lexical binding, except in a script scope, or in any function scope.
const mode =
context & Context.TopLevel && (context & Context.Module) === 0 ? BindingType.Variable : BindingType.Let;
((context & Context.InGlobal) === 0 || (context & Context.Module) === 0) && context & Context.TopLevel
? BindingType.Variable
: BindingType.Let;

addFunctionName(parser, context, scope, parser.tokenValue, mode, 1);

Expand Down
6 changes: 5 additions & 1 deletion test/parser/lexical/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ describe('Lexical - Function', () => {
['(function() { { function foo() {} function* foo() {} } })()', Context.OptionsLexical],
['(function() { { async function foo() {} async function foo() {} } })()', Context.OptionsLexical],
['let x; { var x; }', Context.OptionsLexical],
['{ var x; } let x;', Context.OptionsLexical]
['{ var x; } let x;', Context.OptionsLexical],
['"use strict"; function foo(bar, bar){}', Context.OptionsLexical],
['function foo(bar, bar){}', Context.OptionsLexical | Context.Module | Context.Strict]
]);

for (const arg of [
Expand Down Expand Up @@ -210,6 +212,8 @@ describe('Lexical - Function', () => {
function* x() { yield 1; }
{ function* x() { yield 2 } }
})();`,
`function a() {}
function a() {}`,
`(function() {
var y;
async function x() { y = 1; }
Expand Down

0 comments on commit c6d24b6

Please sign in to comment.