Skip to content

Commit

Permalink
[[FIX]] Disallow Import declarations below top lvl
Browse files Browse the repository at this point in the history
  • Loading branch information
sstern6 authored and jugglinmike committed Jul 30, 2016
1 parent 039ee2e commit d800e44
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4477,6 +4477,10 @@ var JSHINT = (function() {
}).exps = true;

stmt("import", function() {
if (!state.funct["(scope)"].block.isGlobal()) {
error("E053", state.tokens.curr, "Import");
}

if (!state.inES6()) {
warning("W119", state.tokens.curr, "import", "6");
}
Expand Down Expand Up @@ -4575,7 +4579,7 @@ var JSHINT = (function() {
}

if (!state.funct["(scope)"].block.isGlobal()) {
error("E053", state.tokens.curr);
error("E053", state.tokens.curr, "Export");
ok = false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var errors = {
E050: "Mozilla requires the yield expression to be parenthesized here.",
E051: null,
E052: "Unclosed template literal.",
E053: "Export declaration must be in global scope.",
E053: "{a} declarations are only allowed at the top level of module scope.",
E054: "Class properties must be methods. Expected '(' but instead saw '{a}'.",
E055: "The '{a}' option cannot be set after any executable code.",
E056: "'{a}' was used before it was declared, which is illegal for '{b}' variables.",
Expand Down
47 changes: 37 additions & 10 deletions tests/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6988,22 +6988,49 @@ exports.testES6BlockExports = function (test) {
TestRun(test)
.addError(1, "'broken' is defined but never used.")
.addError(2, "'broken2' is defined but never used.")
.addError(4, "Export declaration must be in global scope.")
.addError(5, "Export declaration must be in global scope.")
.addError(6, "Export declaration must be in global scope.")
.addError(7, "Export declaration must be in global scope.")
.addError(8, "Export declaration must be in global scope.")
.addError(14, "Export declaration must be in global scope.")
.addError(15, "Export declaration must be in global scope.")
.addError(16, "Export declaration must be in global scope.")
.addError(17, "Export declaration must be in global scope.")
.addError(4, "Export declarations are only allowed at the top level of module scope.")
.addError(5, "Export declarations are only allowed at the top level of module scope.")
.addError(6, "Export declarations are only allowed at the top level of module scope.")
.addError(7, "Export declarations are only allowed at the top level of module scope.")
.addError(8, "Export declarations are only allowed at the top level of module scope.")
.addError(14, "Export declarations are only allowed at the top level of module scope.")
.addError(15, "Export declarations are only allowed at the top level of module scope.")
.addError(16, "Export declarations are only allowed at the top level of module scope.")
.addError(17, "Export declarations are only allowed at the top level of module scope.")
.addError(17, "Function declarations should not be placed in blocks. Use a function expression or move the statement to the top of the outer function.")
.addError(18, "Export declaration must be in global scope.")
.addError(18, "Export declarations are only allowed at the top level of module scope.")
.test(code, { esnext: true, unused: true });

test.done();
};

exports.testES6BlockImports = function (test) {
var code = [
"{",
" import x from './m.js';",
"}",
"function limitScope(){",
" import {x} from './m.js';",
"}",
"(function(){",
" import './m.js';",
"}());",
"{",
" import {x as y} from './m.js';",
"}",
"limitScope();"
];

TestRun(test)
.addError(2, "Import declarations are only allowed at the top level of module scope.")
.addError(5, "Import declarations are only allowed at the top level of module scope.")
.addError(8, "Import declarations are only allowed at the top level of module scope.")
.addError(11, "Import declarations are only allowed at the top level of module scope.")
.test(code, { esversion: 6, module: true });

test.done();
};

exports.testStrictDirectiveASI = function (test) {
var options = { strict: true, asi: true, globalstrict: true, predef: ["x"] };

Expand Down

0 comments on commit d800e44

Please sign in to comment.