Skip to content

Commit dedfb11

Browse files
kevin940726ljharb
authored andcommittedFeb 4, 2017
add allow glob for rule no-unassigned-import, fix import-js#671
1 parent 28e1623 commit dedfb11

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed
 

‎src/rules/no-unassigned-import.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import isStaticRequire from '../core/staticRequire'
2+
import path from 'path'
3+
import minimatch from 'minimatch'
24

35
function report(context, node) {
46
context.report({
@@ -7,15 +9,40 @@ function report(context, node) {
79
})
810
}
911

12+
function testIsAllow(globs, filename, source) {
13+
if (!Array.isArray(globs)) {
14+
return false // default doens't allow any pattern
15+
}
16+
17+
let filePath
18+
19+
if (source[0] !== '.' && source[0] !== '/') { // a node module
20+
filePath = source
21+
} else {
22+
filePath = path.resolve(path.dirname(filename), source) // get source absolute path
23+
}
24+
25+
return globs.find(glob => (
26+
minimatch(filePath, glob) ||
27+
minimatch(filePath, path.join(process.cwd(), glob))
28+
)) !== undefined
29+
}
30+
1031
function create(context) {
32+
const options = context.options[0] || {}
33+
const filename = context.getFilename()
34+
const isAllow = source => testIsAllow(options.allow, filename, source)
35+
1136
return {
1237
ImportDeclaration(node) {
13-
if (node.specifiers.length === 0) {
38+
if (node.specifiers.length === 0 && !isAllow(node.source.value)) {
1439
report(context, node)
1540
}
1641
},
1742
ExpressionStatement(node) {
18-
if (node.expression.type === 'CallExpression' && isStaticRequire(node.expression)) {
43+
if (node.expression.type === 'CallExpression' &&
44+
isStaticRequire(node.expression) &&
45+
!isAllow(node.expression.arguments[0].value)) {
1946
report(context, node.expression)
2047
}
2148
},
@@ -33,6 +60,7 @@ module.exports = {
3360
'devDependencies': { 'type': ['boolean', 'array'] },
3461
'optionalDependencies': { 'type': ['boolean', 'array'] },
3562
'peerDependencies': { 'type': ['boolean', 'array'] },
63+
'allow': { 'type': 'array' },
3664
},
3765
'additionalProperties': false,
3866
},

‎tests/src/rules/no-unassigned-import.js

+47
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,38 @@ ruleTester.run('no-unassigned-import', rule, {
2929
test({ code: 'require("lodash").foo'}),
3030
test({ code: 'require("lodash").foo()'}),
3131
test({ code: 'require("lodash")()'}),
32+
test({
33+
code: 'import "app.css"',
34+
options: [{ 'allow': ['**/*.css'] }],
35+
}),
36+
test({
37+
code: 'import "app.css";',
38+
options: [{ 'allow': ['*.css'] }],
39+
}),
40+
test({
41+
code: 'import "./app.css"',
42+
options: [{ 'allow': ['**/*.css'] }],
43+
}),
44+
test({
45+
code: 'import "foo/bar"',
46+
options: [{ 'allow': ['foo/**'] }],
47+
}),
48+
test({
49+
code: 'import "foo/bar"',
50+
options: [{ 'allow': ['foo/bar'] }],
51+
}),
52+
test({
53+
code: 'import "../dir/app.css"',
54+
options: [{ 'allow': ['**/*.css'] }],
55+
}),
56+
test({
57+
code: 'import "../dir/app.js"',
58+
options: [{ 'allow': ['**/dir/**'] }],
59+
}),
60+
test({
61+
code: 'require("./app.css")',
62+
options: [{ 'allow': ['**/*.css'] }],
63+
}),
3264
],
3365
invalid: [
3466
test({
@@ -39,5 +71,20 @@ ruleTester.run('no-unassigned-import', rule, {
3971
code: 'require("lodash")',
4072
errors: [error],
4173
}),
74+
test({
75+
code: 'import "./app.css"',
76+
options: [{ 'allow': ['**/*.js'] }],
77+
errors: [error],
78+
}),
79+
test({
80+
code: 'import "./app.css"',
81+
options: [{ 'allow': ['**/dir/**'] }],
82+
errors: [error],
83+
}),
84+
test({
85+
code: 'require("./app.css")',
86+
options: [{ 'allow': ['**/*.js'] }],
87+
errors: [error],
88+
}),
4289
],
4390
})

0 commit comments

Comments
 (0)