Create rule enforcing that goog.provide() is like the path

This commit is contained in:
Tim Schaub
2016-08-11 22:15:06 -06:00
parent 4fffa190c3
commit 73c8059a0a
5 changed files with 70 additions and 0 deletions

View File

@@ -102,6 +102,7 @@
"openlayers-internal/no-unused-requires": 2,
"openlayers-internal/one-provide": 1,
"openlayers-internal/requires-first": 2,
"openlayers-internal/valid-provide": 2,
"openlayers-internal/valid-requires": 2
}
},

View File

@@ -7,6 +7,7 @@ module.exports = {
'no-unused-requires': require('./no-unused-requires').rule,
'one-provide': require('./one-provide').rule,
'requires-first': require('./requires-first').rule,
'valid-provide': require('./valid-provide').rule,
'valid-requires': require('./valid-requires').rule
}
};

58
rules/valid-provide.js Normal file
View File

@@ -0,0 +1,58 @@
'use strict';
const path = require('path');
const util = require('./util');
const sourceRoot = path.join(__dirname, '..', 'src');
exports.rule = {
meta: {
docs: {
description: 'require the first goog.provide() has an arg named like the file path'
}
},
create: function(context) {
let gotFirst = false;
return {
CallExpression: function(expression) {
if (gotFirst) {
return;
}
if (util.isProvideExpression(expression)) {
gotFirst = true;
const parent = expression.parent;
if (parent.type !== 'ExpressionStatement') {
return context.report(expression, 'Expected goog.provide() to in an expression statement');
}
if (parent.parent.type !== 'Program') {
return context.report(expression, 'Expected goog.provide() to be at the top level');
}
if (expression.arguments.length !== 1) {
return context.report(expression, 'Expected one argument for goog.require()');
}
const arg = expression.arguments[0];
if (arg.type !== 'Literal' || !arg.value || typeof arg.value !== 'string') {
return context.report(expression, 'Expected goog.require() to be called with a string');
}
const filePath = path.relative(sourceRoot, context.getFilename());
let ext;
if (path.basename(filePath) === '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}`);
}
}
}
};
}
};

5
test/.eslintrc Normal file
View File

@@ -0,0 +1,5 @@
{
"rules": {
"openlayers-internal/valid-provide": 0
}
}

5
test_rendering/.eslintrc Normal file
View File

@@ -0,0 +1,5 @@
{
"rules": {
"openlayers-internal/valid-provide": 0
}
}