Skip to content

Commit

Permalink
[[FIX]] Support more cases of ES6 module usage
Browse files Browse the repository at this point in the history
The ES6 module parsing is incomplete currently. This adds more cases.

Fixes #2118
Fixes #2143
  • Loading branch information
leebyron committed Feb 3, 2015
1 parent 33612f8 commit 776ed69
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
32 changes: 31 additions & 1 deletion src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4242,7 +4242,22 @@ var JSHINT = (function() {
// ImportClause :: ImportedDefaultBinding
this.name = identifier();
addlabel(this.name, { type: "unused", token: state.tokens.curr });
} else if (state.tokens.next.id === "*") {
if (state.tokens.next.value === ",") {
// ImportClause :: ImportedDefaultBinding , NameSpaceImport
// ImportClause :: ImportedDefaultBinding , NamedImports
advance(",");
// At this point, we intentionally fall through to continue matching
// either NameSpaceImport or NamedImports.
// Discussion:
// https://github.com/jshint/jshint/pull/2144#discussion_r23978406
} else {
advance("from");
advance("(string)");
return this;
}
}

if (state.tokens.next.id === "*") {
// ImportClause :: NameSpaceImport
advance("*");
advance("as");
Expand All @@ -4251,6 +4266,7 @@ var JSHINT = (function() {
addlabel(this.name, { type: "unused", token: state.tokens.curr });
}
} else {
// ImportClause :: NamedImports
advance("{");
for (;;) {
if (state.tokens.next.value === "}") {
Expand Down Expand Up @@ -4301,13 +4317,16 @@ var JSHINT = (function() {
}

if (state.tokens.next.value === "*") {
// ExportDeclaration :: export * FromClause
advance("*");
advance("from");
advance("(string)");
return this;
}

if (state.tokens.next.type === "default") {
// ExportDeclaration :: export default HoistableDeclaration
// ExportDeclaration :: export default ClassDeclaration
state.nameStack.set(state.tokens.next);
advance("default");
if (state.tokens.next.id === "function" || state.tokens.next.id === "class") {
Expand All @@ -4319,6 +4338,7 @@ var JSHINT = (function() {
}

if (state.tokens.next.value === "{") {
// ExportDeclaration :: export ExportClause
advance("{");
for (;;) {
var id;
Expand All @@ -4337,31 +4357,41 @@ var JSHINT = (function() {
break;
}
}
if (state.tokens.next.value === "from") {
// ExportDeclaration :: export ExportClause FromClause
advance("from");
advance("(string)");
}
return this;
}

if (state.tokens.next.id === "var") {
// ExportDeclaration :: export VariableStatement
advance("var");
exported[state.tokens.next.value] = ok;
state.tokens.next.exported = true;
state.syntax["var"].fud.call(state.syntax["var"].fud);
} else if (state.tokens.next.id === "let") {
// ExportDeclaration :: export VariableStatement
advance("let");
exported[state.tokens.next.value] = ok;
state.tokens.next.exported = true;
state.syntax["let"].fud.call(state.syntax["let"].fud);
} else if (state.tokens.next.id === "const") {
// ExportDeclaration :: export VariableStatement
advance("const");
exported[state.tokens.next.value] = ok;
state.tokens.next.exported = true;
state.syntax["const"].fud.call(state.syntax["const"].fud);
} else if (state.tokens.next.id === "function") {
// ExportDeclaration :: export Declaration
this.block = true;
advance("function");
exported[state.tokens.next.value] = ok;
state.tokens.next.exported = true;
state.syntax["function"].fud();
} else if (state.tokens.next.id === "class") {
// ExportDeclaration :: export Declaration
this.block = true;
advance("class");
exported[state.tokens.next.value] = ok;
Expand Down
22 changes: 12 additions & 10 deletions tests/unit/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,19 +679,21 @@ exports.testES6Modules = function (test) {
.addError(7, "'import' is only available in ES6 (use esnext option).")
.addError(8, "'import' is only available in ES6 (use esnext option).")
.addError(9, "'import' is only available in ES6 (use esnext option).")
.addError(20, "'export' is only available in ES6 (use esnext option).")
.addError(24, "'export' is only available in ES6 (use esnext option).")
.addError(28, "'export' is only available in ES6 (use esnext option).")
.addError(29, "'export' is only available in ES6 (use esnext option).")
.addError(33, "'export' is only available in ES6 (use esnext option).")
.addError(37, "'export' is only available in ES6 (use esnext option).")
.addError(41, "'export' is only available in ES6 (use esnext option).")
.addError(10, "'import' is only available in ES6 (use esnext option).")
.addError(11, "'import' is only available in ES6 (use esnext option).")
.addError(22, "'export' is only available in ES6 (use esnext option).")
.addError(26, "'export' is only available in ES6 (use esnext option).")
.addError(30, "'export' is only available in ES6 (use esnext option).")
.addError(31, "'export' is only available in ES6 (use esnext option).")
.addError(35, "'export' is only available in ES6 (use esnext option).")
.addError(39, "'export' is only available in ES6 (use esnext option).")
.addError(43, "'export' is only available in ES6 (use esnext option).")
.addError(44, "'class' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz).")
.addError(45, "'export' is only available in ES6 (use esnext option).")
.addError(46, "'class' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz).")
.addError(47, "'export' is only available in ES6 (use esnext option).")
.addError(47, "'class' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz).")
.addError(46, "'export' is only available in ES6 (use esnext option).")
.addError(45, "'class' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz).")
.addError(44, "'export' is only available in ES6 (use esnext option).")
.addError(43, "'class' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz).")
.test(src, {});

var src2 = [
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/fixtures/es6-import-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { default as _ } from "underscore";
import {} from "ember";
import "ember";
import * as ember from "ember";
import _, * as ember from "ember";
import _, { default as ember } from "ember";

$.ajax();
emGet("foo");
Expand All @@ -26,7 +28,7 @@ export default function() {
}

export { foo };
export { foo, bar };
export { foo, bar } from "source";

// gettin' fancy

Expand Down

0 comments on commit 776ed69

Please sign in to comment.