mirror of
https://github.com/nodejs/node.git
synced 2025-05-19 02:36:32 +00:00

PR-URL: https://github.com/iojs/io.js/pull/1539 Fixes: https://github.com/iojs/io.js/issues/1253 Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com> Reviewed-By: Johan Bergström <bugs@bergstroem.nu> Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
64 lines
2.2 KiB
JavaScript
64 lines
2.2 KiB
JavaScript
/**
|
|
* @fileoverview Rule to enforce concise object methods and properties.
|
|
* @author Jamund Ferguson
|
|
* @copyright 2015 Jamund Ferguson. All rights reserved.
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
var OPTIONS = {
|
|
always: "always",
|
|
never: "never",
|
|
methods: "methods",
|
|
properties: "properties"
|
|
};
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Rule Definition
|
|
//------------------------------------------------------------------------------
|
|
|
|
module.exports = function(context) {
|
|
|
|
var APPLY = context.options[0] || OPTIONS.always;
|
|
var APPLY_TO_METHODS = APPLY === OPTIONS.methods || APPLY === OPTIONS.always;
|
|
var APPLY_TO_PROPS = APPLY === OPTIONS.properties || APPLY === OPTIONS.always;
|
|
var APPLY_NEVER = APPLY === OPTIONS.never;
|
|
|
|
//--------------------------------------------------------------------------
|
|
// Public
|
|
//--------------------------------------------------------------------------
|
|
|
|
return {
|
|
"Property": function(node) {
|
|
var isConciseProperty = node.method || node.shorthand,
|
|
type;
|
|
|
|
// if we're "never" and concise we should warn now
|
|
if (APPLY_NEVER && isConciseProperty) {
|
|
type = node.method ? "method" : "property";
|
|
context.report(node, "Expected longform " + type + " syntax.");
|
|
}
|
|
|
|
// at this point if we're concise or if we're "never" we can leave
|
|
if (APPLY_NEVER || isConciseProperty) {
|
|
return;
|
|
}
|
|
|
|
if (node.value.type === "ArrowFunctionExpression" && APPLY_TO_METHODS) {
|
|
|
|
// {x: ()=>{}} should be written as {x() {}}
|
|
context.report(node, "Expected method shorthand.");
|
|
} else if (node.value.type === "FunctionExpression" && APPLY_TO_METHODS) {
|
|
|
|
// {x: function(){}} should be written as {x() {}}
|
|
context.report(node, "Expected method shorthand.");
|
|
} else if (node.key.name === node.value.name && APPLY_TO_PROPS) {
|
|
|
|
// {x: x} should be written as {x}
|
|
context.report(node, "Expected property shorthand.");
|
|
}
|
|
}
|
|
};
|
|
|
|
};
|