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

Commit

Permalink
Refactor duplicate error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Sep 26, 2016
1 parent 6800abc commit 5152905
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,21 +909,21 @@ pp.checkExport = function (node, checkNames, isDefault) {
// Check for duplicate exports
if (isDefault) {
// Default exports
this.checkDuplicateExports(node, "default", isDefault);
this.checkDuplicateExports(node, "default");
} 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);
this.checkDuplicateExports(specifier, name);
}
} else if (node.declaration) {
// Exported declarations
if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
this.checkDuplicateExports(node, node.declaration.id.name, isDefault);
this.checkDuplicateExports(node, node.declaration.id.name);
} else if (node.declaration.type === "VariableDeclaration") {
for (let declaration of node.declaration.declarations) {
this.checkDeclaration(declaration.id, isDefault);
this.checkDeclaration(declaration.id);
}
}
}
Expand All @@ -938,31 +938,31 @@ pp.checkExport = function (node, checkNames, isDefault) {
}
};

pp.checkDeclaration = function(node, isDefault) {
pp.checkDeclaration = function(node) {
if (node.type === "ObjectPattern") {
for (let prop of node.properties) {
this.checkDeclaration(prop, isDefault);
this.checkDeclaration(prop);
}
} else if (node.type === "ArrayPattern") {
for (let elem of node.elements) {
this.checkDeclaration(elem, isDefault);
this.checkDeclaration(elem);
}
} else if (node.type === "ObjectProperty") {
this.checkDeclaration(node.value, isDefault);
this.checkDeclaration(node.value);
} else if (node.type === "Identifier") {
this.checkDuplicateExports(node, node.name, isDefault);
this.checkDuplicateExports(node, node.name);
}
};

pp.checkDuplicateExports = function(node, name, isDefault) {
pp.checkDuplicateExports = function(node, name) {
if (this.state.exportedIdentifiers.indexOf(name) > -1) {
this.raiseDuplicateExportError(node, name, isDefault);
this.raiseDuplicateExportError(node, name);
}
this.state.exportedIdentifiers.push(name);
};

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

0 comments on commit 5152905

Please sign in to comment.