Files
openlayers/rules/valid-requires.js
2016-08-08 09:58:12 -06:00

39 lines
1.2 KiB
JavaScript

'use strict';
const util = require('./util');
exports.rule = {
meta: {
docs: {
description: 'require that all goog.require() have a valid arg and appear at the top level'
}
},
create: function(context) {
return {
CallExpression: function(expression) {
if (util.isRequireExpression(expression)) {
const ancestors = context.getAncestors();
const parent = ancestors[ancestors.length - 1];
if (parent.type !== 'ExpressionStatement') {
return context.report(expression, 'Expected goog.require() to in an expression statement');
}
if (ancestors.length !== 2) {
return context.report(expression, 'Expected goog.require() 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');
}
}
}
};
}
};