Check for duplicate named exports in exported destructuring assignments (#144)

* Check for duplicate named exports in exported destructuring assignments

* Refactor duplicate error reporting

* Remove unnecessary check
This commit is contained in:
Kai Cataldo 2016-10-04 09:06:26 -04:00 committed by Daniel Tschinder
parent 7c2302a02f
commit 76e69277d7
31 changed files with 2302 additions and 83 deletions

View File

@ -909,23 +909,19 @@ 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, specifier.exported.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) {
if (declaration.id.name) {
this.checkDuplicateExports(declaration, declaration.id.name, isDefault);
}
this.checkDeclaration(declaration.id);
}
}
}
@ -940,15 +936,31 @@ pp.checkExport = function (node, checkNames, isDefault) {
}
};
pp.checkDuplicateExports = function(node, name, isDefault) {
pp.checkDeclaration = function(node) {
if (node.type === "ObjectPattern") {
for (let prop of node.properties) {
this.checkDeclaration(prop);
}
} else if (node.type === "ArrayPattern") {
for (let elem of node.elements) {
this.checkDeclaration(elem);
}
} else if (node.type === "ObjectProperty") {
this.checkDeclaration(node.value);
} else if (node.type === "Identifier") {
this.checkDuplicateExports(node, node.name);
}
};
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.`
);

View File

@ -1,2 +1,11 @@
export const { rhythm } = typography;
export const { TypographyStyle } = typography;
export const { foo } = bar;
export const { foo: foo2 } = bar;
export const { foo: { baz } } = bar;
export const { foo: { baz: { qux } } } = bar;
export const { foo: { baz: { qux2 } }, foo3 } = bar;
export const [foo4] = bar;
export const [[baz2]] = bar;
export const [{ foo: [baz3] }, { foo2: [baz4] }] = bar;
export const { foo: { baz: { qux3 } }, foo2: { baz2: [qux4]} } = bar;
export const { foo: { baz: { qux5 } }, foo2: { baz2: [{qux6}]} } = bar;
export const { Foo } = bar;

View File

@ -0,0 +1,2 @@
export function foo() {};
export const { a: [{foo}] } = bar;

View File

@ -0,0 +1,3 @@
{
"throws": "`foo` has already been exported. Exported identifiers must be unique. (2:20)"
}

View File

@ -0,0 +1,2 @@
export function foo4() {};
export const [{ a: [{ foo }], b: { foo2: [{ foo3: foo4 }] } }] = bar;

View File

@ -0,0 +1,3 @@
{
"throws": "`foo4` has already been exported. Exported identifiers must be unique. (2:50)"
}

View File

@ -0,0 +1,2 @@
export function foo4() {};
export const { a: [{ foo }], b: { foo2: [{ foo3: foo4 }] } } = bar;

View File

@ -0,0 +1,3 @@
{
"throws": "`foo4` has already been exported. Exported identifiers must be unique. (2:49)"
}

View File

@ -0,0 +1,2 @@
export function foo4() {};
export const { a: [{ foo4: foo }], b, c: { foo2: [{ foo3: foo4 }] } } = bar;

View File

@ -0,0 +1,3 @@
{
"throws": "`foo4` has already been exported. Exported identifiers must be unique. (2:58)"
}

View File

@ -0,0 +1,3 @@
export const foo = 1;
export const { foo2: foo } = bar;

View File

@ -0,0 +1,3 @@
{
"throws": "`foo` has already been exported. Exported identifiers must be unique. (2:21)"
}

View File

@ -0,0 +1,3 @@
export const { foo: foo2 } = bar;
export const foo2 = 1;

View File

@ -0,0 +1,3 @@
{
"throws": "`foo2` has already been exported. Exported identifiers must be unique. (2:13)"
}

View File

@ -0,0 +1,2 @@
export function foo() {};
export const { foo } = bar;

View File

@ -0,0 +1,3 @@
{
"throws": "`foo` has already been exported. Exported identifiers must be unique. (2:15)"
}

View File

@ -0,0 +1,2 @@
export const { foo } = bar;
export function foo() {};

View File

@ -0,0 +1,3 @@
{
"throws": "`foo` has already been exported. Exported identifiers must be unique. (2:0)"
}

View File

@ -0,0 +1,2 @@
export function foo() {};
export const [foo] = bar;

View File

@ -0,0 +1,3 @@
{
"throws": "`foo` has already been exported. Exported identifiers must be unique. (2:14)"
}

View File

@ -0,0 +1,2 @@
export const [foo] = bar;
export function foo() {};

View File

@ -0,0 +1,3 @@
{
"throws": "`foo` has already been exported. Exported identifiers must be unique. (2:0)"
}

View File

@ -0,0 +1,2 @@
export const { foo } = bar;
export const [foo] = bar2;

View File

@ -0,0 +1,3 @@
{
"throws": "`foo` has already been exported. Exported identifiers must be unique. (2:14)"
}

View File

@ -0,0 +1,2 @@
export const [foo] = bar;
export const { foo } = bar2;

View File

@ -0,0 +1,3 @@
{
"throws": "`foo` has already been exported. Exported identifiers must be unique. (2:15)"
}

View File

@ -0,0 +1,2 @@
export class Foo {};
export const { Foo } = bar;

View File

@ -0,0 +1,3 @@
{
"throws": "`Foo` has already been exported. Exported identifiers must be unique. (2:15)"
}

View File

@ -0,0 +1,2 @@
export class Foo {};
export const [Foo] = bar;

View File

@ -0,0 +1,3 @@
{
"throws": "`Foo` has already been exported. Exported identifiers must be unique. (2:14)"
}