|
| 1 | +'use strict'; |
| 2 | +const {hasSideEffect, isCommaToken, isOpeningParenToken, isSemicolonToken} = require('eslint-utils'); |
| 3 | +const getDocumentationUrl = require('./utils/get-documentation-url'); |
| 4 | +const methodSelector = require('./utils/method-selector'); |
| 5 | + |
| 6 | +const ERROR = 'error'; |
| 7 | +const SUGGESTION = 'suggestion'; |
| 8 | +const messages = { |
| 9 | + [ERROR]: 'Do not call `Array#push()` multiple times.', |
| 10 | + [SUGGESTION]: 'Merge with previous one.' |
| 11 | +}; |
| 12 | + |
| 13 | +const arrayPushExpressionStatement = [ |
| 14 | + 'ExpressionStatement', |
| 15 | + methodSelector({ |
| 16 | + name: 'push', |
| 17 | + property: 'expression' |
| 18 | + }) |
| 19 | +].join(''); |
| 20 | + |
| 21 | +const selector = `${arrayPushExpressionStatement} + ${arrayPushExpressionStatement}`; |
| 22 | + |
| 23 | +const getCallExpressionArgumentsText = (node, sourceCode) => { |
| 24 | + const openingParenthesisToken = sourceCode.getTokenAfter(node.callee, isOpeningParenToken); |
| 25 | + const closingParenthesisToken = sourceCode.getLastToken(node); |
| 26 | + |
| 27 | + return sourceCode.text.slice( |
| 28 | + openingParenthesisToken.range[1], |
| 29 | + closingParenthesisToken.range[0] |
| 30 | + ); |
| 31 | +}; |
| 32 | + |
| 33 | +function getFirstExpression(node, sourceCode) { |
| 34 | + const {parent} = node; |
| 35 | + const visitorKeys = sourceCode.visitorKeys[parent.type] || Object.keys(parent); |
| 36 | + |
| 37 | + for (const property of visitorKeys) { |
| 38 | + const value = parent[property]; |
| 39 | + if (Array.isArray(value)) { |
| 40 | + const index = value.indexOf(node); |
| 41 | + |
| 42 | + if (index !== -1) { |
| 43 | + return value[index - 1]; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /* istanbul ignore next */ |
| 49 | + throw new Error('Cannot find the first `Array#push()` call.\nPlease open an issue at https://github.com/sindresorhus/eslint-plugin-unicorn/issues/new?title=%60no-array-push-push%60%3A%20Cannot%20find%20first%20%60push()%60'); |
| 50 | +} |
| 51 | + |
| 52 | +function create(context) { |
| 53 | + const sourceCode = context.getSourceCode(); |
| 54 | + |
| 55 | + return { |
| 56 | + [selector](secondExpression) { |
| 57 | + const firstExpression = getFirstExpression(secondExpression, sourceCode); |
| 58 | + const firstCall = firstExpression.expression; |
| 59 | + const secondCall = secondExpression.expression; |
| 60 | + |
| 61 | + const firstCallArray = firstCall.callee.object; |
| 62 | + const secondCallArray = secondCall.callee.object; |
| 63 | + |
| 64 | + // Not same array |
| 65 | + if (sourceCode.getText(firstCallArray) !== sourceCode.getText(secondCallArray)) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + const secondCallArguments = secondCall.arguments; |
| 70 | + const problem = { |
| 71 | + node: secondCall.callee.property, |
| 72 | + messageId: ERROR |
| 73 | + }; |
| 74 | + |
| 75 | + const fix = function * (fixer) { |
| 76 | + if (secondCallArguments.length > 0) { |
| 77 | + const text = getCallExpressionArgumentsText(secondCall, sourceCode); |
| 78 | + |
| 79 | + const [penultimateToken, lastToken] = sourceCode.getLastTokens(firstCall, 2); |
| 80 | + yield (isCommaToken(penultimateToken) ? fixer.insertTextAfter(penultimateToken, ` ${text}`) : fixer.insertTextBefore( |
| 81 | + lastToken, |
| 82 | + firstCall.arguments.length > 0 ? `, ${text}` : text |
| 83 | + )); |
| 84 | + } |
| 85 | + |
| 86 | + const shouldKeepSemicolon = !isSemicolonToken(sourceCode.getLastToken(firstExpression)) && |
| 87 | + isSemicolonToken(sourceCode.getLastToken(secondExpression)); |
| 88 | + |
| 89 | + yield fixer.replaceTextRange( |
| 90 | + [firstExpression.range[1], secondExpression.range[1]], |
| 91 | + shouldKeepSemicolon ? ';' : '' |
| 92 | + ); |
| 93 | + }; |
| 94 | + |
| 95 | + if ( |
| 96 | + hasSideEffect(firstCallArray, sourceCode) || |
| 97 | + secondCallArguments.some(element => hasSideEffect(element, sourceCode)) |
| 98 | + ) { |
| 99 | + problem.suggest = [ |
| 100 | + { |
| 101 | + messageId: SUGGESTION, |
| 102 | + fix |
| 103 | + } |
| 104 | + ]; |
| 105 | + } else { |
| 106 | + problem.fix = fix; |
| 107 | + } |
| 108 | + |
| 109 | + context.report(problem); |
| 110 | + } |
| 111 | + }; |
| 112 | +} |
| 113 | + |
| 114 | +module.exports = { |
| 115 | + create, |
| 116 | + meta: { |
| 117 | + type: 'suggestion', |
| 118 | + docs: { |
| 119 | + url: getDocumentationUrl(__filename) |
| 120 | + }, |
| 121 | + fixable: 'code', |
| 122 | + messages |
| 123 | + } |
| 124 | +}; |
0 commit comments