/
import-index.js
34 lines (30 loc) · 896 Bytes
/
import-index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict';
const getDocumentationUrl = require('./utils/get-documentation-url');
const regexp = /^(@.*?\/.*?|[./]+?.*?)\/(\.|(?:index(?:\.js)?))?$/;
const isImportingIndex = value => regexp.test(value);
const normalize = value => value.replace(regexp, '$1');
const importIndex = (context, node, argument) => {
if (argument && isImportingIndex(argument.value)) {
context.report({
node,
message: 'Do not reference the index file directly.',
fix: fixer => fixer.replaceText(argument, `'${normalize(argument.value)}'`)
});
}
};
const create = context => {
return {
'CallExpression[callee.name="require"]': node => importIndex(context, node, node.arguments[0]),
ImportDeclaration: node => importIndex(context, node, node.source)
};
};
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
url: getDocumentationUrl(__filename)
},
fixable: 'code'
}
};