Skip to content

Commit

Permalink
Issue #1504: ES6 module exports should not count as unused
Browse files Browse the repository at this point in the history
  • Loading branch information
valueof committed Feb 21, 2014
1 parent 02ea236 commit 9e581e9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3565,7 +3565,7 @@ stmt("export", function () {
if (state.tokens.next.value === "{") {
advance("{");
for (;;) {
identifier();
exported[identifier()] = true;

if (state.tokens.next.value === ",") {
advance(",");
Expand All @@ -3582,20 +3582,25 @@ stmt("export", function () {

if (state.tokens.next.id === "var") {
advance("var");
exported[state.tokens.next.value] = true;
syntax["var"].fud.call(syntax["var"].fud);
} else if (state.tokens.next.id === "let") {
advance("let");
exported[state.tokens.next.value] = true;
syntax["let"].fud.call(syntax["let"].fud);
} else if (state.tokens.next.id === "const") {
advance("const");
exported[state.tokens.next.value] = true;
syntax["const"].fud.call(syntax["const"].fud);
} else if (state.tokens.next.id === "function") {
this.block = true;
advance("function");
exported[state.tokens.next.value] = true;
syntax["function"].fud();
} else if (state.tokens.next.id === "class") {
this.block = true;
advance("class");
exported[state.tokens.next.value] = true;
syntax["class"].fud();
} else {
warn("E024", { token: state.tokens.next, args: [state.tokens.next.value] });
Expand Down
49 changes: 48 additions & 1 deletion tests/unit/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,53 @@ exports.testES6Modules = function (test) {
test.done();
};

exports.testES6ModulesNamedExportsAffectUnused = function (test) {
// Named Exports should count as used
var src1 = [
"var a = {",
" foo: 'foo',",
" bar: 'bar'",
"};",
"var x = 23;",
"var z = 42;",
"export { a, x };",
"export var b = { baz: 'baz' };",
"export function boo() { return z; }",
"export class MyClass { }"
];

TestRun(test)
.test(src1, {
esnext: true,
unused: true
});

test.done();
};

exports.testES6ModulesDefaultExportsAffectUnused = function (test) {
// Default Exports should count as used
var src1 = [
"var a = {",
" foo: 'foo',",
" bar: 'bar'",
"};",
"var x = 23;",
"var z = 42;",
"export default { a: a, x: x };",
"export default function boo() { return x + z; }",
"export default class MyClass { }"
];

TestRun(test)
.test(src1, {
esnext: true,
unused: true
});

test.done();
};

exports.testPotentialVariableLeak = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/leak.js", "utf8");

Expand Down Expand Up @@ -726,4 +773,4 @@ exports.testArrayPrototypeExtensions = function (test) {
jshint.run("var x = 123;\nlet y = 456;\nconst z = 123;");
delete Array.prototype.undefinedPrototypeProperty;
test.done();
};
};

0 comments on commit 9e581e9

Please sign in to comment.