Blame view
node_modules/eslint/lib/rules/object-shorthand.js
2.54 KB
c39994410
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
/** * @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; } // getters, setters and computed properties are ignored if (node.kind === "get" || node.kind === "set" || node.computed) { return; } if (node.value.type === "FunctionExpression" && !node.value.id && APPLY_TO_METHODS) { // {x: function(){}} should be written as {x() {}} context.report(node, "Expected method shorthand."); } else if (node.value.type === "Identifier" && node.key.name === node.value.name && APPLY_TO_PROPS) { // {x: x} should be written as {x} context.report(node, "Expected property shorthand."); } else if (node.value.type === "Identifier" && node.key.type === "Literal" && node.key.value === node.value.name && APPLY_TO_PROPS) { // {"x": x} should be written as {x} context.report(node, "Expected property shorthand."); } } }; }; module.exports.schema = [ { "enum": ["always", "methods", "properties", "never"] } ]; |