-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new rule no-webpack-loader-syntax
#586
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 [loaders](http://webpack.github.io/docs/loaders.html) and their configuration inline in imports 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 using 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). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
## 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}` }, | ||
], | ||
}), | ||
], | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Webpack allows specifying the loaders to use in the imports source string using ...