Resolve root to source directory

This commit is contained in:
Tim Schaub
2016-08-12 09:15:07 -06:00
parent 833d061bc7
commit 0db12e8590
2 changed files with 20 additions and 6 deletions

View File

@@ -3,7 +3,19 @@
const path = require('path');
const util = require('./util');
const sourceRoot = path.join(__dirname, '..', 'src');
function longestCommonPrefix(path1, path2) {
const parts1 = path.resolve(path1).split(path.sep);
const parts2 = path.resolve(path2).split(path.sep);
const common = [];
for (let i = 0, ii = parts1.length; i < ii; ++i) {
if (parts1[i] === parts2[i]) {
common.push(parts1[i]);
} else {
break;
}
}
return common.join(path.sep);
}
exports.rule = {
meta: {
@@ -39,17 +51,19 @@ exports.rule = {
return context.report(expression, 'Expected goog.require() to be called with a string');
}
const filePath = path.relative(sourceRoot, context.getFilename());
const filePath = context.getFilename();
const sourceRoot = path.join(longestCommonPrefix(__dirname, filePath), 'src');
const requirePath = path.relative(sourceRoot, filePath);
let ext;
if (path.basename(filePath) === 'index.js') {
if (path.basename(requirePath) === 'index.js') {
ext = path.sep + 'index.js';
} else {
ext = '.js';
}
const name = arg.value;
const expectedPath = name.split('.').join(path.sep) + ext;
if (expectedPath.toLowerCase() !== filePath.toLowerCase()) {
return context.report(expression, `Expected goog.provide('${name}') to be like ${filePath}`);
if (expectedPath.toLowerCase() !== requirePath.toLowerCase()) {
return context.report(expression, `Expected goog.provide('${name}') to be like ${requirePath}`);
}
}
}