Skip to content

Commit

Permalink
fix(parser): improved error reporting for duplicate bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed Jul 29, 2019
1 parent 5e41577 commit 0483d25
Show file tree
Hide file tree
Showing 3 changed files with 8 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.

10 changes: 6 additions & 4 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export interface ScopeState {
/** Scope error interface */
export interface ScopeError {
type: Errors;
params: string[];
index: number;
line: number;
column: number;
Expand Down Expand Up @@ -527,10 +528,11 @@ export function createArrowHeadParsingScope(parser: ParserState, context: Contex
* @param parser Parser state
* @param type Errors type
*/
export function recordScopeError(parser: ParserState, type: Errors): ScopeError {
export function recordScopeError(parser: ParserState, type: Errors, ...params: string[]): ScopeError {
const { index, line, column } = parser;
return {
type,
params,
index,
line,
column
Expand Down Expand Up @@ -612,7 +614,7 @@ export function addBlockName(

if (value && (value & BindingKind.Empty) === 0) {
if (kind & BindingKind.ArgumentList) {
scope.scopeError = recordScopeError(parser, Errors.Unexpected);
scope.scopeError = recordScopeError(parser, Errors.DuplicateBinding, name);
} else if (
context & Context.OptionsWebCompat &&
value & BindingKind.FunctionLexical &&
Expand All @@ -632,7 +634,7 @@ export function addBlockName(

if (scope.type & ScopeKind.ArrowParams && value && (value & BindingKind.Empty) === 0) {
if (kind & BindingKind.ArgumentList) {
scope.scopeError = recordScopeError(parser, Errors.Unexpected);
scope.scopeError = recordScopeError(parser, Errors.DuplicateBinding, name);
}
}

Expand Down Expand Up @@ -678,7 +680,7 @@ export function addVarName(
}
if (currentScope === scope) {
if (value & BindingKind.ArgumentList && kind & BindingKind.ArgumentList) {
currentScope.scopeError = recordScopeError(parser, Errors.Unexpected);
currentScope.scopeError = recordScopeError(parser, Errors.DuplicateBinding, name);
}
}
if (value & (BindingKind.CatchIdentifier | BindingKind.CatchPattern)) {
Expand Down
2 changes: 1 addition & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ export function report(parser: ParserState, type: Errors, ...params: string[]):
}

export function reportScopeError(scope: any): never {
throw new ParseError(scope.index, scope.line, scope.column, scope.type);
throw new ParseError(scope.index, scope.line, scope.column, scope.type, scope.params);
}

/**
Expand Down

0 comments on commit 0483d25

Please sign in to comment.