object-shorthand.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /**
  2. * @fileoverview Rule to enforce concise object methods and properties.
  3. * @author Jamund Ferguson
  4. */
  5. "use strict";
  6. const OPTIONS = {
  7. always: "always",
  8. never: "never",
  9. methods: "methods",
  10. properties: "properties",
  11. consistent: "consistent",
  12. consistentAsNeeded: "consistent-as-needed"
  13. };
  14. //------------------------------------------------------------------------------
  15. // Requirements
  16. //------------------------------------------------------------------------------
  17. const astUtils = require("./utils/ast-utils");
  18. //------------------------------------------------------------------------------
  19. // Rule Definition
  20. //------------------------------------------------------------------------------
  21. module.exports = {
  22. meta: {
  23. type: "suggestion",
  24. docs: {
  25. description: "require or disallow method and property shorthand syntax for object literals",
  26. category: "ECMAScript 6",
  27. recommended: false,
  28. url: "https://eslint.org/docs/rules/object-shorthand"
  29. },
  30. fixable: "code",
  31. schema: {
  32. anyOf: [
  33. {
  34. type: "array",
  35. items: [
  36. {
  37. enum: ["always", "methods", "properties", "never", "consistent", "consistent-as-needed"]
  38. }
  39. ],
  40. minItems: 0,
  41. maxItems: 1
  42. },
  43. {
  44. type: "array",
  45. items: [
  46. {
  47. enum: ["always", "methods", "properties"]
  48. },
  49. {
  50. type: "object",
  51. properties: {
  52. avoidQuotes: {
  53. type: "boolean"
  54. }
  55. },
  56. additionalProperties: false
  57. }
  58. ],
  59. minItems: 0,
  60. maxItems: 2
  61. },
  62. {
  63. type: "array",
  64. items: [
  65. {
  66. enum: ["always", "methods"]
  67. },
  68. {
  69. type: "object",
  70. properties: {
  71. ignoreConstructors: {
  72. type: "boolean"
  73. },
  74. avoidQuotes: {
  75. type: "boolean"
  76. },
  77. avoidExplicitReturnArrows: {
  78. type: "boolean"
  79. }
  80. },
  81. additionalProperties: false
  82. }
  83. ],
  84. minItems: 0,
  85. maxItems: 2
  86. }
  87. ]
  88. },
  89. messages: {
  90. expectedAllPropertiesShorthanded: "Expected shorthand for all properties.",
  91. expectedLiteralMethodLongform: "Expected longform method syntax for string literal keys.",
  92. expectedPropertyShorthand: "Expected property shorthand.",
  93. expectedPropertyLongform: "Expected longform property syntax.",
  94. expectedMethodShorthand: "Expected method shorthand.",
  95. expectedMethodLongform: "Expected longform method syntax.",
  96. unexpectedMix: "Unexpected mix of shorthand and non-shorthand properties."
  97. }
  98. },
  99. create(context) {
  100. const APPLY = context.options[0] || OPTIONS.always;
  101. const APPLY_TO_METHODS = APPLY === OPTIONS.methods || APPLY === OPTIONS.always;
  102. const APPLY_TO_PROPS = APPLY === OPTIONS.properties || APPLY === OPTIONS.always;
  103. const APPLY_NEVER = APPLY === OPTIONS.never;
  104. const APPLY_CONSISTENT = APPLY === OPTIONS.consistent;
  105. const APPLY_CONSISTENT_AS_NEEDED = APPLY === OPTIONS.consistentAsNeeded;
  106. const PARAMS = context.options[1] || {};
  107. const IGNORE_CONSTRUCTORS = PARAMS.ignoreConstructors;
  108. const AVOID_QUOTES = PARAMS.avoidQuotes;
  109. const AVOID_EXPLICIT_RETURN_ARROWS = !!PARAMS.avoidExplicitReturnArrows;
  110. const sourceCode = context.getSourceCode();
  111. //--------------------------------------------------------------------------
  112. // Helpers
  113. //--------------------------------------------------------------------------
  114. const CTOR_PREFIX_REGEX = /[^_$0-9]/u;
  115. /**
  116. * Determines if the first character of the name is a capital letter.
  117. * @param {string} name The name of the node to evaluate.
  118. * @returns {boolean} True if the first character of the property name is a capital letter, false if not.
  119. * @private
  120. */
  121. function isConstructor(name) {
  122. const match = CTOR_PREFIX_REGEX.exec(name);
  123. // Not a constructor if name has no characters apart from '_', '$' and digits e.g. '_', '$$', '_8'
  124. if (!match) {
  125. return false;
  126. }
  127. const firstChar = name.charAt(match.index);
  128. return firstChar === firstChar.toUpperCase();
  129. }
  130. /**
  131. * Determines if the property can have a shorthand form.
  132. * @param {ASTNode} property Property AST node
  133. * @returns {boolean} True if the property can have a shorthand form
  134. * @private
  135. *
  136. */
  137. function canHaveShorthand(property) {
  138. return (property.kind !== "set" && property.kind !== "get" && property.type !== "SpreadElement" && property.type !== "SpreadProperty" && property.type !== "ExperimentalSpreadProperty");
  139. }
  140. /**
  141. * Checks whether a node is a string literal.
  142. * @param {ASTNode} node Any AST node.
  143. * @returns {boolean} `true` if it is a string literal.
  144. */
  145. function isStringLiteral(node) {
  146. return node.type === "Literal" && typeof node.value === "string";
  147. }
  148. /**
  149. * Determines if the property is a shorthand or not.
  150. * @param {ASTNode} property Property AST node
  151. * @returns {boolean} True if the property is considered shorthand, false if not.
  152. * @private
  153. *
  154. */
  155. function isShorthand(property) {
  156. // property.method is true when `{a(){}}`.
  157. return (property.shorthand || property.method);
  158. }
  159. /**
  160. * Determines if the property's key and method or value are named equally.
  161. * @param {ASTNode} property Property AST node
  162. * @returns {boolean} True if the key and value are named equally, false if not.
  163. * @private
  164. *
  165. */
  166. function isRedundant(property) {
  167. const value = property.value;
  168. if (value.type === "FunctionExpression") {
  169. return !value.id; // Only anonymous should be shorthand method.
  170. }
  171. if (value.type === "Identifier") {
  172. return astUtils.getStaticPropertyName(property) === value.name;
  173. }
  174. return false;
  175. }
  176. /**
  177. * Ensures that an object's properties are consistently shorthand, or not shorthand at all.
  178. * @param {ASTNode} node Property AST node
  179. * @param {boolean} checkRedundancy Whether to check longform redundancy
  180. * @returns {void}
  181. *
  182. */
  183. function checkConsistency(node, checkRedundancy) {
  184. // We are excluding getters/setters and spread properties as they are considered neither longform nor shorthand.
  185. const properties = node.properties.filter(canHaveShorthand);
  186. // Do we still have properties left after filtering the getters and setters?
  187. if (properties.length > 0) {
  188. const shorthandProperties = properties.filter(isShorthand);
  189. /*
  190. * If we do not have an equal number of longform properties as
  191. * shorthand properties, we are using the annotations inconsistently
  192. */
  193. if (shorthandProperties.length !== properties.length) {
  194. // We have at least 1 shorthand property
  195. if (shorthandProperties.length > 0) {
  196. context.report({ node, messageId: "unexpectedMix" });
  197. } else if (checkRedundancy) {
  198. /*
  199. * If all properties of the object contain a method or value with a name matching it's key,
  200. * all the keys are redundant.
  201. */
  202. const canAlwaysUseShorthand = properties.every(isRedundant);
  203. if (canAlwaysUseShorthand) {
  204. context.report({ node, messageId: "expectedAllPropertiesShorthanded" });
  205. }
  206. }
  207. }
  208. }
  209. }
  210. /**
  211. * Fixes a FunctionExpression node by making it into a shorthand property.
  212. * @param {SourceCodeFixer} fixer The fixer object
  213. * @param {ASTNode} node A `Property` node that has a `FunctionExpression` or `ArrowFunctionExpression` as its value
  214. * @returns {Object} A fix for this node
  215. */
  216. function makeFunctionShorthand(fixer, node) {
  217. const firstKeyToken = node.computed
  218. ? sourceCode.getFirstToken(node, astUtils.isOpeningBracketToken)
  219. : sourceCode.getFirstToken(node.key);
  220. const lastKeyToken = node.computed
  221. ? sourceCode.getFirstTokenBetween(node.key, node.value, astUtils.isClosingBracketToken)
  222. : sourceCode.getLastToken(node.key);
  223. const keyText = sourceCode.text.slice(firstKeyToken.range[0], lastKeyToken.range[1]);
  224. let keyPrefix = "";
  225. // key: /* */ () => {}
  226. if (sourceCode.commentsExistBetween(lastKeyToken, node.value)) {
  227. return null;
  228. }
  229. if (node.value.async) {
  230. keyPrefix += "async ";
  231. }
  232. if (node.value.generator) {
  233. keyPrefix += "*";
  234. }
  235. const fixRange = [firstKeyToken.range[0], node.range[1]];
  236. const methodPrefix = keyPrefix + keyText;
  237. if (node.value.type === "FunctionExpression") {
  238. const functionToken = sourceCode.getTokens(node.value).find(token => token.type === "Keyword" && token.value === "function");
  239. const tokenBeforeParams = node.value.generator ? sourceCode.getTokenAfter(functionToken) : functionToken;
  240. return fixer.replaceTextRange(
  241. fixRange,
  242. methodPrefix + sourceCode.text.slice(tokenBeforeParams.range[1], node.value.range[1])
  243. );
  244. }
  245. const arrowToken = sourceCode.getTokenBefore(node.value.body, astUtils.isArrowToken);
  246. const fnBody = sourceCode.text.slice(arrowToken.range[1], node.value.range[1]);
  247. let shouldAddParensAroundParameters = false;
  248. let tokenBeforeParams;
  249. if (node.value.params.length === 0) {
  250. tokenBeforeParams = sourceCode.getFirstToken(node.value, astUtils.isOpeningParenToken);
  251. } else {
  252. tokenBeforeParams = sourceCode.getTokenBefore(node.value.params[0]);
  253. }
  254. if (node.value.params.length === 1) {
  255. const hasParen = astUtils.isOpeningParenToken(tokenBeforeParams);
  256. const isTokenOutsideNode = tokenBeforeParams.range[0] < node.range[0];
  257. shouldAddParensAroundParameters = !hasParen || isTokenOutsideNode;
  258. }
  259. const sliceStart = shouldAddParensAroundParameters
  260. ? node.value.params[0].range[0]
  261. : tokenBeforeParams.range[0];
  262. const sliceEnd = sourceCode.getTokenBefore(arrowToken).range[1];
  263. const oldParamText = sourceCode.text.slice(sliceStart, sliceEnd);
  264. const newParamText = shouldAddParensAroundParameters ? `(${oldParamText})` : oldParamText;
  265. return fixer.replaceTextRange(
  266. fixRange,
  267. methodPrefix + newParamText + fnBody
  268. );
  269. }
  270. /**
  271. * Fixes a FunctionExpression node by making it into a longform property.
  272. * @param {SourceCodeFixer} fixer The fixer object
  273. * @param {ASTNode} node A `Property` node that has a `FunctionExpression` as its value
  274. * @returns {Object} A fix for this node
  275. */
  276. function makeFunctionLongform(fixer, node) {
  277. const firstKeyToken = node.computed ? sourceCode.getTokens(node).find(token => token.value === "[") : sourceCode.getFirstToken(node.key);
  278. const lastKeyToken = node.computed ? sourceCode.getTokensBetween(node.key, node.value).find(token => token.value === "]") : sourceCode.getLastToken(node.key);
  279. const keyText = sourceCode.text.slice(firstKeyToken.range[0], lastKeyToken.range[1]);
  280. let functionHeader = "function";
  281. if (node.value.async) {
  282. functionHeader = `async ${functionHeader}`;
  283. }
  284. if (node.value.generator) {
  285. functionHeader = `${functionHeader}*`;
  286. }
  287. return fixer.replaceTextRange([node.range[0], lastKeyToken.range[1]], `${keyText}: ${functionHeader}`);
  288. }
  289. /*
  290. * To determine whether a given arrow function has a lexical identifier (`this`, `arguments`, `super`, or `new.target`),
  291. * create a stack of functions that define these identifiers (i.e. all functions except arrow functions) as the AST is
  292. * traversed. Whenever a new function is encountered, create a new entry on the stack (corresponding to a different lexical
  293. * scope of `this`), and whenever a function is exited, pop that entry off the stack. When an arrow function is entered,
  294. * keep a reference to it on the current stack entry, and remove that reference when the arrow function is exited.
  295. * When a lexical identifier is encountered, mark all the arrow functions on the current stack entry by adding them
  296. * to an `arrowsWithLexicalIdentifiers` set. Any arrow function in that set will not be reported by this rule,
  297. * because converting it into a method would change the value of one of the lexical identifiers.
  298. */
  299. const lexicalScopeStack = [];
  300. const arrowsWithLexicalIdentifiers = new WeakSet();
  301. const argumentsIdentifiers = new WeakSet();
  302. /**
  303. * Enters a function. This creates a new lexical identifier scope, so a new Set of arrow functions is pushed onto the stack.
  304. * Also, this marks all `arguments` identifiers so that they can be detected later.
  305. * @returns {void}
  306. */
  307. function enterFunction() {
  308. lexicalScopeStack.unshift(new Set());
  309. context.getScope().variables.filter(variable => variable.name === "arguments").forEach(variable => {
  310. variable.references.map(ref => ref.identifier).forEach(identifier => argumentsIdentifiers.add(identifier));
  311. });
  312. }
  313. /**
  314. * Exits a function. This pops the current set of arrow functions off the lexical scope stack.
  315. * @returns {void}
  316. */
  317. function exitFunction() {
  318. lexicalScopeStack.shift();
  319. }
  320. /**
  321. * Marks the current function as having a lexical keyword. This implies that all arrow functions
  322. * in the current lexical scope contain a reference to this lexical keyword.
  323. * @returns {void}
  324. */
  325. function reportLexicalIdentifier() {
  326. lexicalScopeStack[0].forEach(arrowFunction => arrowsWithLexicalIdentifiers.add(arrowFunction));
  327. }
  328. //--------------------------------------------------------------------------
  329. // Public
  330. //--------------------------------------------------------------------------
  331. return {
  332. Program: enterFunction,
  333. FunctionDeclaration: enterFunction,
  334. FunctionExpression: enterFunction,
  335. "Program:exit": exitFunction,
  336. "FunctionDeclaration:exit": exitFunction,
  337. "FunctionExpression:exit": exitFunction,
  338. ArrowFunctionExpression(node) {
  339. lexicalScopeStack[0].add(node);
  340. },
  341. "ArrowFunctionExpression:exit"(node) {
  342. lexicalScopeStack[0].delete(node);
  343. },
  344. ThisExpression: reportLexicalIdentifier,
  345. Super: reportLexicalIdentifier,
  346. MetaProperty(node) {
  347. if (node.meta.name === "new" && node.property.name === "target") {
  348. reportLexicalIdentifier();
  349. }
  350. },
  351. Identifier(node) {
  352. if (argumentsIdentifiers.has(node)) {
  353. reportLexicalIdentifier();
  354. }
  355. },
  356. ObjectExpression(node) {
  357. if (APPLY_CONSISTENT) {
  358. checkConsistency(node, false);
  359. } else if (APPLY_CONSISTENT_AS_NEEDED) {
  360. checkConsistency(node, true);
  361. }
  362. },
  363. "Property:exit"(node) {
  364. const isConciseProperty = node.method || node.shorthand;
  365. // Ignore destructuring assignment
  366. if (node.parent.type === "ObjectPattern") {
  367. return;
  368. }
  369. // getters and setters are ignored
  370. if (node.kind === "get" || node.kind === "set") {
  371. return;
  372. }
  373. // only computed methods can fail the following checks
  374. if (node.computed && node.value.type !== "FunctionExpression" && node.value.type !== "ArrowFunctionExpression") {
  375. return;
  376. }
  377. //--------------------------------------------------------------
  378. // Checks for property/method shorthand.
  379. if (isConciseProperty) {
  380. if (node.method && (APPLY_NEVER || AVOID_QUOTES && isStringLiteral(node.key))) {
  381. const messageId = APPLY_NEVER ? "expectedMethodLongform" : "expectedLiteralMethodLongform";
  382. // { x() {} } should be written as { x: function() {} }
  383. context.report({
  384. node,
  385. messageId,
  386. fix: fixer => makeFunctionLongform(fixer, node)
  387. });
  388. } else if (APPLY_NEVER) {
  389. // { x } should be written as { x: x }
  390. context.report({
  391. node,
  392. messageId: "expectedPropertyLongform",
  393. fix: fixer => fixer.insertTextAfter(node.key, `: ${node.key.name}`)
  394. });
  395. }
  396. } else if (APPLY_TO_METHODS && !node.value.id && (node.value.type === "FunctionExpression" || node.value.type === "ArrowFunctionExpression")) {
  397. if (IGNORE_CONSTRUCTORS && node.key.type === "Identifier" && isConstructor(node.key.name)) {
  398. return;
  399. }
  400. if (AVOID_QUOTES && isStringLiteral(node.key)) {
  401. return;
  402. }
  403. // {[x]: function(){}} should be written as {[x]() {}}
  404. if (node.value.type === "FunctionExpression" ||
  405. node.value.type === "ArrowFunctionExpression" &&
  406. node.value.body.type === "BlockStatement" &&
  407. AVOID_EXPLICIT_RETURN_ARROWS &&
  408. !arrowsWithLexicalIdentifiers.has(node.value)
  409. ) {
  410. context.report({
  411. node,
  412. messageId: "expectedMethodShorthand",
  413. fix: fixer => makeFunctionShorthand(fixer, node)
  414. });
  415. }
  416. } else if (node.value.type === "Identifier" && node.key.name === node.value.name && APPLY_TO_PROPS) {
  417. // {x: x} should be written as {x}
  418. context.report({
  419. node,
  420. messageId: "expectedPropertyShorthand",
  421. fix(fixer) {
  422. return fixer.replaceText(node, node.value.name);
  423. }
  424. });
  425. } else if (node.value.type === "Identifier" && node.key.type === "Literal" && node.key.value === node.value.name && APPLY_TO_PROPS) {
  426. if (AVOID_QUOTES) {
  427. return;
  428. }
  429. // {"x": x} should be written as {x}
  430. context.report({
  431. node,
  432. messageId: "expectedPropertyShorthand",
  433. fix(fixer) {
  434. return fixer.replaceText(node, node.value.name);
  435. }
  436. });
  437. }
  438. }
  439. };
  440. }
  441. };