Skip to content
Permalink
Browse files

New: Add metadata to few test rules (fixes #4494)

  • Loading branch information...
ilyavolodin committed Jan 19, 2016
1 parent 82bc2f9 commit 84704740dfe3688cd9ec171fac3d38f288427acc
Showing with 297 additions and 263 deletions.
  1. +1 −1 lib/config/config-validator.js
  2. +9 −6 lib/eslint.js
  3. +11 −7 lib/rule-context.js
  4. +70 −65 lib/rules/accessor-pairs.js
  5. +184 −176 lib/rules/array-bracket-spacing.js
  6. +22 −8 lib/testers/rule-tester.js
@@ -30,7 +30,7 @@ var validators = {
*/
function getRuleOptionsSchema(id) {
var rule = rules.get(id),
schema = rule && rule.schema;
schema = rule && rule.schema || rule && rule.meta && rule.meta.schema;

// Given a tuple of schemas, insert warning level at the beginning
if (Array.isArray(schema)) {
@@ -744,10 +744,11 @@ module.exports = (function() {
options = getRuleOptions(config.rules[key]);

try {
rule = ruleCreator(new RuleContext(
var ruleContext = new RuleContext(
key, api, severity, options,
config.settings, config.parserOptions, config.parser
));
config.settings, config.parserOptions, config.parser, ruleCreator.meta);
rule = ruleCreator.create ? ruleCreator.create(ruleContext) :
ruleCreator(ruleContext);

// add all the node types as listeners
Object.keys(rule).forEach(function(nodeType) {
@@ -851,16 +852,18 @@ module.exports = (function() {
* @param {Object} opts Optional template data which produces a formatted message
* with symbols being replaced by this object's values.
* @param {Object} fix A fix command description.
* @param {Object} meta Metadata of the rule
* @returns {void}
*/
api.report = function(ruleId, severity, node, location, message, opts, fix) {
api.report = function(ruleId, severity, node, location, message, opts, fix, meta) {
if (node) {
assert.strictEqual(typeof node, "object", "Node must be an object");
}

if (typeof location === "string") {
assert.ok(node, "Node must be provided when reporting error if location is not provided");

meta = fix;
fix = opts;
opts = message;
message = location;
@@ -893,8 +896,8 @@ module.exports = (function() {
source: sourceCode.lines[location.line - 1] || ""
};

// ensure there's range and text properties, otherwise it's not a valid fix
if (fix && Array.isArray(fix.range) && (typeof fix.text === "string")) {
// ensure there's range and text properties as well as metadata switch, otherwise it's not a valid fix
if (fix && Array.isArray(fix.range) && (typeof fix.text === "string") && (!meta || !meta.docs || meta.docs.fixable)) {
problem.fix = fix;
}

@@ -66,18 +66,20 @@ var PASSTHROUGHS = [
* @param {string} ruleId The ID of the rule using this object.
* @param {eslint} eslint The eslint object.
* @param {number} severity The configured severity level of the rule.
* @param {array} options The configuration information to be added to the rule.
* @param {object} settings The configuration settings passed from the config file.
* @param {object} parserOptions The parserOptions settings passed from the config file.
* @param {object} parserName The parser setting passed from the config file.
* @param {Array} options The configuration information to be added to the rule.
* @param {Object} settings The configuration settings passed from the config file.
* @param {Object} parserOptions The parserOptions settings passed from the config file.
* @param {Object} parserName The parser setting passed from the config file.
* @param {Object} meta The metadata of the rule
*/
function RuleContext(ruleId, eslint, severity, options, settings, parserOptions, parserName) {
function RuleContext(ruleId, eslint, severity, options, settings, parserOptions, parserName, meta) {
// public.
this.id = ruleId;
this.options = options;
this.settings = settings;
this.parserOptions = parserOptions;
this.parserName = parserName;
this.meta = meta;

// private.
this.eslint = eslint;
@@ -127,7 +129,8 @@ RuleContext.prototype = {
descriptor.loc || descriptor.node.loc.start,
descriptor.message,
descriptor.data,
fix
fix,
this.meta
);

return;
@@ -140,7 +143,8 @@ RuleContext.prototype = {
nodeOrDescriptor,
location,
message,
opts
opts,
this.meta
);
}
};
@@ -66,80 +66,85 @@ function isPropertyDescriptor(node) {
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {
var config = context.options[0] || {};
var checkGetWithoutSet = config.getWithoutSet === true;
var checkSetWithoutGet = config.setWithoutGet !== false;

/**
* Checks a object expression to see if it has setter and getter both present or none.
* @param {ASTNode} node The node to check.
* @returns {void}
* @private
*/
function checkLonelySetGet(node) {
var isSetPresent = false;
var isGetPresent = false;
var isDescriptor = isPropertyDescriptor(node);

for (var i = 0, end = node.properties.length; i < end; i++) {
var property = node.properties[i];

var propToCheck = "";
if (property.kind === "init") {
if (isDescriptor && !property.computed) {
propToCheck = property.key.name;
module.exports = {
meta: {
docs: {
description: "Enforces getter/setter pairs in objects",
category: "Best Practices",
recommended: false
},
schema: [{
"type": "object",
"properties": {
"getWithoutSet": {
"type": "boolean"
},
"setWithoutGet": {
"type": "boolean"
}
},
"additionalProperties": false
}]
},
create: function(context) {
var config = context.options[0] || {};
var checkGetWithoutSet = config.getWithoutSet === true;
var checkSetWithoutGet = config.setWithoutGet !== false;

/**
* Checks a object expression to see if it has setter and getter both present or none.
* @param {ASTNode} node The node to check.
* @returns {void}
* @private
*/
function checkLonelySetGet(node) {
var isSetPresent = false;
var isGetPresent = false;
var isDescriptor = isPropertyDescriptor(node);

for (var i = 0, end = node.properties.length; i < end; i++) {
var property = node.properties[i];

var propToCheck = "";
if (property.kind === "init") {
if (isDescriptor && !property.computed) {
propToCheck = property.key.name;
}
} else {
propToCheck = property.kind;
}
} else {
propToCheck = property.kind;
}

switch (propToCheck) {
case "set":
isSetPresent = true;
break;
switch (propToCheck) {
case "set":
isSetPresent = true;
break;

case "get":
isGetPresent = true;
break;
case "get":
isGetPresent = true;
break;

default:
// Do nothing
}
default:
// Do nothing
}

if (isSetPresent && isGetPresent) {
break;
if (isSetPresent && isGetPresent) {
break;
}
}
}

if (checkSetWithoutGet && isSetPresent && !isGetPresent) {
context.report(node, "Getter is not present");
} else if (checkGetWithoutSet && isGetPresent && !isSetPresent) {
context.report(node, "Setter is not present");
}
}

return {
"ObjectExpression": function(node) {
if (checkSetWithoutGet || checkGetWithoutSet) {
checkLonelySetGet(node);
if (checkSetWithoutGet && isSetPresent && !isGetPresent) {
context.report(node, "Getter is not present");
} else if (checkGetWithoutSet && isGetPresent && !isSetPresent) {
context.report(node, "Setter is not present");
}
}
};

};

module.exports.schema = [
{
"type": "object",
"properties": {
"getWithoutSet": {
"type": "boolean"
},
"setWithoutGet": {
"type": "boolean"
return {
"ObjectExpression": function(node) {
if (checkSetWithoutGet || checkGetWithoutSet) {
checkLonelySetGet(node);
}
}
},
"additionalProperties": false
};
}
];
};
Oops, something went wrong.

0 comments on commit 8470474

Please sign in to comment.
You can’t perform that action at this time.