-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new rule
no-webpack-loader-syntax
(#586)
* Add new rule `no-webpack-loader-syntax` Webpack allows specifying loaders and their configuration inline in imports using a non-standard import syntax. This rule allows disabling this non-standard syntax in imports. * Fix up no-webpack-loader-syntax docs * Add no-webpack-loader-syntax to README
- Loading branch information
Showing
6 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# no-webpack-loader-syntax | ||
|
||
Forbid Webpack loader syntax in imports. | ||
|
||
[Webpack](http://webpack.github.io) allows specifying the [loaders](http://webpack.github.io/docs/loaders.html) to use in the import source string using a special syntax like this: | ||
```js | ||
var moduleWithOneLoader = require("my-loader!./my-awesome-module"); | ||
``` | ||
|
||
This syntax is non-standard, so it couples the code to Webpack. The recommended way to specify Webpack loader configuration is in a [Webpack configuration file](http://webpack.github.io/docs/loaders.html#loaders-by-config). | ||
|
||
## Rule Details | ||
|
||
### Fail | ||
|
||
```js | ||
import myModule from 'my-loader!my-module'; | ||
import theme from 'style!css!./theme.css'; | ||
|
||
var myModule = require('my-loader!./my-module'); | ||
var theme = require('style!css!./theme.css'); | ||
``` | ||
|
||
### Pass | ||
|
||
```js | ||
import myModule from 'my-module'; | ||
import theme from './theme.css'; | ||
|
||
var myModule = require('my-module'); | ||
var theme = require('./theme.css'); | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you have a project that doesn't use Webpack you can safely disable this rule. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import isStaticRequire from '../core/staticRequire' | ||
|
||
function reportIfNonStandard(context, node, name) { | ||
if (name.indexOf('!') !== -1) { | ||
context.report(node, `Unexpected '!' in '${name}'. ` + | ||
'Do not use import syntax to configure webpack loaders.' | ||
) | ||
} | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
docs: {}, | ||
}, | ||
|
||
create: function (context) { | ||
return { | ||
ImportDeclaration: function handleImports(node) { | ||
reportIfNonStandard(context, node, node.source.value) | ||
}, | ||
CallExpression: function handleRequires(node) { | ||
if (isStaticRequire(node)) { | ||
reportIfNonStandard(context, node, node.arguments[0].value) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { test } from '../utils' | ||
|
||
import { RuleTester } from 'eslint' | ||
|
||
const ruleTester = new RuleTester() | ||
, rule = require('rules/no-webpack-loader-syntax') | ||
|
||
const message = 'Do not use import syntax to configure webpack loaders.' | ||
|
||
ruleTester.run('no-webpack-loader-syntax', rule, { | ||
valid: [ | ||
test({ code: 'import _ from "lodash"'}), | ||
test({ code: 'import find from "lodash.find"'}), | ||
test({ code: 'import foo from "./foo.css"'}), | ||
test({ code: 'import data from "@scope/my-package/data.json"'}), | ||
test({ code: 'var _ = require("lodash")'}), | ||
test({ code: 'var find = require("lodash.find")'}), | ||
test({ code: 'var foo = require("./foo")'}), | ||
test({ code: 'var foo = require("../foo")'}), | ||
test({ code: 'var foo = require("foo")'}), | ||
test({ code: 'var foo = require("./")'}), | ||
test({ code: 'var foo = require("@scope/foo")'}), | ||
], | ||
invalid: [ | ||
test({ | ||
code: 'import _ from "babel!lodash"', | ||
errors: [ | ||
{ message: `Unexpected '!' in 'babel!lodash'. ${message}` }, | ||
], | ||
}), | ||
test({ | ||
code: 'import find from "-babel-loader!lodash.find"', | ||
errors: [ | ||
{ message: `Unexpected '!' in '-babel-loader!lodash.find'. ${message}` }, | ||
], | ||
}), | ||
test({ | ||
code: 'import foo from "style!css!./foo.css"', | ||
errors: [ | ||
{ message: `Unexpected '!' in 'style!css!./foo.css'. ${message}` }, | ||
], | ||
}), | ||
test({ | ||
code: 'import data from "json!@scope/my-package/data.json"', | ||
errors: [ | ||
{ message: `Unexpected '!' in 'json!@scope/my-package/data.json'. ${message}` }, | ||
], | ||
}), | ||
test({ | ||
code: 'var _ = require("babel!lodash")', | ||
errors: [ | ||
{ message: `Unexpected '!' in 'babel!lodash'. ${message}` }, | ||
], | ||
}), | ||
test({ | ||
code: 'var find = require("-babel-loader!lodash.find")', | ||
errors: [ | ||
{ message: `Unexpected '!' in '-babel-loader!lodash.find'. ${message}` }, | ||
], | ||
}), | ||
test({ | ||
code: 'var foo = require("style!css!./foo.css")', | ||
errors: [ | ||
{ message: `Unexpected '!' in 'style!css!./foo.css'. ${message}` }, | ||
], | ||
}), | ||
test({ | ||
code: 'var data = require("json!@scope/my-package/data.json")', | ||
errors: [ | ||
{ message: `Unexpected '!' in 'json!@scope/my-package/data.json'. ${message}` }, | ||
], | ||
}), | ||
], | ||
}) |