Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Commit

Permalink
Disallow duplicate named exports (#107)
Browse files Browse the repository at this point in the history
fixes #69
  • Loading branch information
kaicataldo authored and hzoo committed Sep 22, 2016
1 parent 4e1fbd4 commit 650e333
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 6 deletions.
44 changes: 41 additions & 3 deletions src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ pp.parseExport = function (node) {
}
node.declaration = expr;
if (needsSemi) this.semicolon();
this.checkExport(node);
this.checkExport(node, true, true);
return this.finishNode(node, "ExportDefaultDeclaration");
} else if (this.state.type.keyword || this.shouldParseExportDeclaration()) {
node.specifiers = [];
Expand All @@ -855,7 +855,7 @@ pp.parseExport = function (node) {
node.specifiers = this.parseExportSpecifiers();
this.parseExportFrom(node);
}
this.checkExport(node);
this.checkExport(node, true);
return this.finishNode(node, "ExportNamedDeclaration");
};

Expand Down Expand Up @@ -903,7 +903,31 @@ pp.shouldParseExportDeclaration = function () {
return this.isContextual("async");
};

pp.checkExport = function (node) {
pp.checkExport = function (node, checkNames, isDefault) {
if (checkNames) {
// Check for duplicate exports
if (isDefault) {
// Default exports
this.checkDuplicateExports(node, "default", isDefault);
} else if (node.specifiers && node.specifiers.length) {
// Named exports
for (let specifier of node.specifiers) {
const name = specifier.exported.name;
if (name === "default") isDefault = true;
this.checkDuplicateExports(specifier, name, isDefault);
}
} else if (node.declaration) {
// Exported declarations
if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
this.checkDuplicateExports(node, node.declaration.id.name, isDefault);
} else if (node.declaration.type === "VariableDeclaration") {
for (let declaration of node.declaration.declarations) {
this.checkDuplicateExports(declaration, declaration.id.name, isDefault);
}
}
}
}

if (this.state.decorators.length) {
let isClass = node.declaration && (node.declaration.type === "ClassDeclaration" || node.declaration.type === "ClassExpression");
if (!node.declaration || !isClass) {
Expand All @@ -913,6 +937,20 @@ pp.checkExport = function (node) {
}
};

pp.checkDuplicateExports = function(node, name, isDefault) {
if (this.state.exportedIdentifiers[name]) {
this.raiseDuplicateExportError(node, name, isDefault);
}
this.state.exportedIdentifiers[name] = true;
};

pp.raiseDuplicateExportError = function(node, name, isDefault) {
this.raise(node.start, isDefault ?
"Only one default export allowed per module." :
`\`${name}\` has already been exported. Exported identifiers must be unique.`
);
};

// Parses a comma-separated list of module exports.

pp.parseExportSpecifiers = function () {
Expand Down
6 changes: 6 additions & 0 deletions src/tokenizer/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export default class State {
this.containsEsc = this.containsOctal = false;
this.octalPosition = null;

this.exportedIdentifiers = {};

return this;
}

Expand Down Expand Up @@ -119,6 +121,10 @@ export default class State {
containsOctal: boolean;
octalPosition: ?number;

// Names of exports store. `default` is stored as a name for both
// `export default foo;` and `export { foo as default };`.
exportedIdentifiers: {[id:string]: boolean};

curPosition() {
return new Position(this.curLine, this.pos - this.lineStart);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default function() {};
export { foo as default };
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Only one default export allowed per module. (2:9)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default {};
export default function() {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Only one default export allowed per module. (2:0)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Foo };
export class Foo {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"throws": "`Foo` has already been exported. Exported identifiers must be unique. (2:0)"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { foo };
export function foo() {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "`foo` has already been exported. Exported identifiers must be unique. (2:0)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { foo };
export const foo = bar;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "`foo` has already been exported. Exported identifiers must be unique. (2:13)"
}
2 changes: 2 additions & 0 deletions test/fixtures/es2015/modules/duplicate-named-export/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { foo };
export { bar as foo };
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "`foo` has already been exported. Exported identifiers must be unique. (2:9)"
}
2 changes: 1 addition & 1 deletion test/fixtures/flow/type-exports/interface/actual.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export interface foo { p: number };
export interface foo<T> { p: T };
export interface bar<T> { p: T };
4 changes: 2 additions & 2 deletions test/fixtures/flow/type-exports/interface/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
"column": 20
}
},
"name": "foo"
"name": "bar"
},
"typeParameters": {
"type": "TypeParameterDeclaration",
Expand Down Expand Up @@ -346,4 +346,4 @@
],
"directives": []
}
}
}

0 comments on commit 650e333

Please sign in to comment.