-
Notifications
You must be signed in to change notification settings - Fork 15
/
require-json-ext.js
58 lines (52 loc) · 1.4 KB
/
require-json-ext.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
var path = require('path');
var helpers = require('./_helpers');
var jsonExtRe = /\.json$/;
module.exports = function(context) {
var target = context.getFilename();
var resolveOpts = {
basedir: path.dirname(target),
extensions: ['.js', '.json', '.node'],
};
function validate(node) {
var id = helpers.getModuleId(node);
if (jsonExtRe.test(id)) return;
var resolved = helpers.resolveSync(id, resolveOpts);
if (jsonExtRe.test(resolved)) {
var basename = path.basename(id);
var idNode = helpers.getIdNode(node);
context.report({
node: idNode,
data: {basename: basename},
message: '"{{basename}}" missing ".json" extension.',
fix: function(fixer) {
return fixer.insertTextBeforeRange([idNode.range[1] - 1], '.json');
},
});
}
}
return {
CallExpression: function(node) {
if (helpers.isRequireCall(node) ||
helpers.isRequireResolveCall(node)) {
validate(node);
}
},
ImportDeclaration: function(node) {
if (helpers.isImport(node)) {
validate(node);
}
},
ExportAllDeclaration: function(node) {
if (helpers.isExportFrom(node)) {
validate(node);
}
},
ExportNamedDeclaration: function(node) {
if (helpers.isExportFrom(node)) {
validate(node);
}
},
};
};
module.exports.schema = [];