Skip to content

Commit 43b73e0

Browse files
committed
test case correctness of all require statements
1 parent a527828 commit 43b73e0

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
"read-last-lines": "^1.1.0",
145145
"requirejs": "^2.3.1",
146146
"through2": "^2.0.3",
147+
"true-case-path": "^1.0.2",
147148
"watchify": "^3.9.0",
148149
"xml2js": "^0.4.16"
149150
}

tasks/test_syntax.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var glob = require('glob');
66
var madge = require('madge');
77
var readLastLines = require('read-last-lines');
88
var eslint = require('eslint');
9+
var trueCasePath = require('true-case-path');
910

1011
var constants = require('./util/constants');
1112
var srcGlob = path.join(constants.pathToSrc, '**/*.js');
@@ -55,6 +56,8 @@ function assertJasmineSuites() {
5556
* - check for header comment
5657
* - check that we don't have any features that break in IE
5758
* - check that we don't use getComputedStyle unexpectedly
59+
* - check that require statements use lowercase (to match assertFileNames)
60+
* or match the case of the source file
5861
*/
5962
function assertSrcContents() {
6063
var licenseSrc = constants.licenseSrc;
@@ -70,6 +73,9 @@ function assertSrcContents() {
7073
// Forbidden in IE in any context
7174
var IE_BLACK_LIST = ['classList'];
7275

76+
// require'd built-in modules
77+
var BUILTINS = ['events'];
78+
7379
var getComputedStyleCnt = 0;
7480

7581
glob(combineGlobs([srcGlob, libGlob]), function(err, files) {
@@ -100,7 +106,28 @@ function assertSrcContents() {
100106
}
101107
else if(node.type === 'Identifier' && node.source() === 'getComputedStyle') {
102108
if(node.parent.source() !== 'window.getComputedStyle') {
103-
logs.push(file + ': getComputedStyle must be called as a `window` property.');
109+
logs.push(file + ' : getComputedStyle must be called as a `window` property.');
110+
}
111+
}
112+
else if(node.type === 'CallExpression' && node.callee.name === 'require') {
113+
var pathNode = node.arguments[0];
114+
var pathStr = pathNode.value;
115+
if(pathNode.type !== 'Literal') {
116+
logs.push(file + ' : You may only `require` literals.');
117+
}
118+
else if(BUILTINS.indexOf(pathStr) === -1) {
119+
// node version 8.9.0+ can use require.resolve(request, {paths: [...]})
120+
// and avoid this explicit conversion to the current location
121+
if(pathStr.charAt(0) === '.') {
122+
pathStr = path.relative(__dirname, path.join(path.dirname(file), pathStr));
123+
}
124+
var fullPath = require.resolve(pathStr);
125+
var casedPath = trueCasePath(fullPath);
126+
127+
if(fullPath !== trueCasePath(fullPath)) {
128+
logs.push(file + ' : `require` path is not case-correct:\n' +
129+
fullPath + ' -> ' + casedPath);
130+
}
104131
}
105132
}
106133
});

0 commit comments

Comments
 (0)