Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

Commit

Permalink
Add allowImportExportEverywhere parserOption (#327) (#329)
Browse files Browse the repository at this point in the history
* Add allowImportExportEverywhere parserOption (#327)

* Added "allowImportExportEverywhere option (#327)" test.
  • Loading branch information
rhettlivingston authored and hzoo committed Jun 22, 2016
1 parent e1d03fa commit b0734fa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,16 @@ Check out the [ESLint docs](http://eslint.org/docs/rules/) for all possible rule
### Configuration

`sourceType` can be set to `'module'`(default) or `'script'` if your code isn't using ECMAScript modules.
`allowImportExportEverywhere` can be set to true to allow import and export declarations to appear anywhere a statement is allowed if your build environment supports that. By default, import and export declarations can only appear at a program's top level.

**.eslintrc**

```json
{
"parser": "babel-eslint",
"parserOptions": {
"sourceType": "module"
"sourceType": "module",
"allowImportExportEverywhere": false
}
}
```
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ exports.parse = function (code, options) {
options = options || {};
eslintOptions.ecmaVersion = options.ecmaVersion = options.ecmaVersion || 6;
eslintOptions.sourceType = options.sourceType = options.sourceType || "module";
eslintOptions.allowImportExportEverywhere = options.allowImportExportEverywhere = options.allowImportExportEverywhere || false;
if (options.sourceType === "module") {
eslintOptions.globalReturn = false;
} else {
Expand All @@ -385,7 +386,7 @@ exports.parseNoPatch = function (code, options) {
var opts = {
sourceType: options.sourceType,
strictMode: true,
allowImportExportEverywhere: false, // consistent with espree
allowImportExportEverywhere: options.allowImportExportEverywhere, // consistent with espree
allowReturnOutsideFunction: true,
allowSuperOutsideMethod: true,
plugins: [
Expand Down
16 changes: 16 additions & 0 deletions test/non-regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,22 @@ describe("verify", function () {
);
});

it("allowImportExportEverywhere option (#327)", function () {
verifyAndAssertMessages([
"if (true) { import Foo from 'foo'; }",
"function foo() { import Bar from 'bar'; }",
"switch (a) { case 1: import FooBar from 'foobar'; }"
].join("\n"),
{},
[],
"module",
{
env: {},
parserOptions: { ecmaVersion: 6, sourceType: "module", allowImportExportEverywhere: true }
}
);
});

it("with does not crash parsing in script mode (strict off) #171", function () {
verifyAndAssertMessages(
"with (arguments) { length; }",
Expand Down

0 comments on commit b0734fa

Please sign in to comment.