-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcase-sensitive.js
150 lines (130 loc) · 4.02 KB
/
case-sensitive.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict';
var commondir = require('commondir');
var fs = require('fs');
var path = require('path');
var resolve = require('resolve');
var helpers = require('./_helpers');
var nodeExts = /\.(js|json|node)$/;
var _readdirCache = helpers.oneTickCache();
function readdirSync(dirname) {
var readdirCache = _readdirCache();
if (!(dirname in readdirCache)) {
try {
readdirCache[dirname] = fs.readdirSync(dirname);
} catch (err) {
readdirCache[dirname] = null;
}
}
return readdirCache[dirname];
}
// turns "/a/b/c.js" into ["/a", "/a/b", "/a/b/c.js"]
function pathSteps(pathString) {
return pathString
.split(path.sep)
.map(function(part, i, parts) {
return parts.slice(0, i + 1).join(path.sep);
})
.filter(Boolean);
}
// if more than one possible suggestion is found, return none.
function getCaseSuggestion(needle, haystack) {
var found = false;
var lneedle = needle.toLowerCase();
for (var i = 0; i < haystack.length; i++) {
if (lneedle === haystack[i].toLowerCase()) {
if (found) return false;
found = haystack[i];
}
}
return found;
}
module.exports = function(context) {
var target = context.getFilename();
var resolveOpts = {
basedir: path.dirname(target),
extensions: ['.js', '.json', '.node'],
};
if (context.options[0] && context.options[0].paths) {
resolveOpts.paths = context.options[0].paths.map(function(single_path) {
return path.resolve(single_path);
});
}
function validate(node) {
var id = helpers.getModuleId(node);
var resolved = helpers.resolveSync(id, resolveOpts, context);
if (!resolved || resolve.isCore(resolved)) return;
var prefix = commondir([target, resolved]);
pathSteps(resolved)
.filter(function(step) {
// remove the steps outside of our request
return step.indexOf(prefix) !== -1;
})
.forEach(function(step, i, steps) {
var basename = path.basename(step);
var dirname = path.dirname(step);
var dirlist = readdirSync(dirname);
// we don't have permission?
if (!dirlist) return;
// compare the directory listing to the requested path. this works
// because "resolve" resolves by concating the path segments from the
// input, so the resolved path will have the incorrect case:
if (dirlist.indexOf(basename) !== -1) return;
var shouldRemoveExt =
i === steps.length - 1 && // last step
nodeExts.test(basename) && // expected
!nodeExts.test(id); // actual
var suggestion = getCaseSuggestion(basename, dirlist);
var incorrect = shouldRemoveExt
? basename.replace(nodeExts, '')
: basename;
var correct = shouldRemoveExt && suggestion
? suggestion.replace(nodeExts, '')
: suggestion;
var idNode = helpers.getIdNode(node);
if (correct) {
context.report({
node: idNode,
data: {incorrect: incorrect, correct: correct},
message: 'Case mismatch in "{{incorrect}}", expected "{{correct}}".',
});
} else {
context.report({
node: idNode,
data: {incorrect: incorrect},
message: 'Case mismatch in "{{incorrect}}".',
});
}
});
}
return {
CallExpression: function(node) {
if (helpers.isRequireCall(node) ||
helpers.isRequireResolveCall(node)) {
validate(node);
}
},
ImportDeclaration: function(node) {
if (helpers.isImport(node) || helpers.isImportType(node)) {
validate(node);
}
},
ExportAllDeclaration: function(node) {
if (helpers.isExportFrom(node) || helpers.isExportTypeFrom(node)) {
validate(node);
}
},
ExportNamedDeclaration: function(node) {
if (helpers.isExportFrom(node) || helpers.isExportTypeFrom(node)) {
validate(node);
}
},
};
};
module.exports.schema = {
paths: {
type: 'array',
items: {
type: 'string',
},
},
};