Resolve root to source directory
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "eslint-plugin-openlayers-internal",
|
"name": "eslint-plugin-openlayers-internal",
|
||||||
"version": "2.0.1",
|
"version": "2.1.0",
|
||||||
"description": "Custom ESLint rules for the OpenLayers project",
|
"description": "Custom ESLint rules for the OpenLayers project",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|||||||
@@ -3,7 +3,19 @@
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const util = require('./util');
|
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 = {
|
exports.rule = {
|
||||||
meta: {
|
meta: {
|
||||||
@@ -39,17 +51,19 @@ exports.rule = {
|
|||||||
return context.report(expression, 'Expected goog.require() to be called with a string');
|
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;
|
let ext;
|
||||||
if (path.basename(filePath) === 'index.js') {
|
if (path.basename(requirePath) === 'index.js') {
|
||||||
ext = path.sep + 'index.js';
|
ext = path.sep + 'index.js';
|
||||||
} else {
|
} else {
|
||||||
ext = '.js';
|
ext = '.js';
|
||||||
}
|
}
|
||||||
const name = arg.value;
|
const name = arg.value;
|
||||||
const expectedPath = name.split('.').join(path.sep) + ext;
|
const expectedPath = name.split('.').join(path.sep) + ext;
|
||||||
if (expectedPath.toLowerCase() !== filePath.toLowerCase()) {
|
if (expectedPath.toLowerCase() !== requirePath.toLowerCase()) {
|
||||||
return context.report(expression, `Expected goog.provide('${name}') to be like ${filePath}`);
|
return context.report(expression, `Expected goog.provide('${name}') to be like ${requirePath}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user