|
| 1 | +'use strict'; |
| 2 | +const { |
| 3 | + isCommaToken, |
| 4 | + isOpeningParenToken, |
| 5 | +} = require('@eslint-community/eslint-utils'); |
| 6 | +const {isCallExpression, isMethodCall} = require('./ast/index.js'); |
| 7 | +const {removeParentheses} = require('./fix/index.js'); |
| 8 | +const {isNodeMatchesNameOrPath} = require('./utils/index.js'); |
| 9 | + |
| 10 | +const MESSAGE_ID_ERROR = 'prefer-structured-clone/error'; |
| 11 | +const MESSAGE_ID_SUGGESTION = 'prefer-structured-clone/suggestion'; |
| 12 | +const messages = { |
| 13 | + [MESSAGE_ID_ERROR]: 'Prefer `structuredClone(…)` over `{{description}}` to create a deep clone.', |
| 14 | + [MESSAGE_ID_SUGGESTION]: 'Switch to `structuredClone(…)`.', |
| 15 | +}; |
| 16 | + |
| 17 | +const lodashCloneDeepFunctions = [ |
| 18 | + '_.cloneDeep', |
| 19 | + 'lodash.cloneDeep', |
| 20 | +]; |
| 21 | + |
| 22 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 23 | +const create = context => { |
| 24 | + const {functions: configFunctions} = { |
| 25 | + functions: [], |
| 26 | + ...context.options[0], |
| 27 | + }; |
| 28 | + const functions = [...configFunctions, ...lodashCloneDeepFunctions]; |
| 29 | + |
| 30 | + // `JSON.parse(JSON.stringify(…))` |
| 31 | + context.on('CallExpression', callExpression => { |
| 32 | + if (!( |
| 33 | + // `JSON.stringify()` |
| 34 | + isMethodCall(callExpression, { |
| 35 | + object: 'JSON', |
| 36 | + method: 'parse', |
| 37 | + argumentsLength: 1, |
| 38 | + optionalCall: false, |
| 39 | + optionalMember: false, |
| 40 | + }) |
| 41 | + // `JSON.parse()` |
| 42 | + && isMethodCall(callExpression.arguments[0], { |
| 43 | + object: 'JSON', |
| 44 | + method: 'stringify', |
| 45 | + argumentsLength: 1, |
| 46 | + optionalCall: false, |
| 47 | + optionalMember: false, |
| 48 | + }) |
| 49 | + )) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + const jsonParse = callExpression; |
| 54 | + const jsonStringify = callExpression.arguments[0]; |
| 55 | + |
| 56 | + return { |
| 57 | + node: jsonParse, |
| 58 | + loc: { |
| 59 | + start: jsonParse.loc.start, |
| 60 | + end: jsonStringify.callee.loc.end, |
| 61 | + }, |
| 62 | + messageId: MESSAGE_ID_ERROR, |
| 63 | + data: { |
| 64 | + description: 'JSON.parse(JSON.stringify(…))', |
| 65 | + }, |
| 66 | + suggest: [ |
| 67 | + { |
| 68 | + messageId: MESSAGE_ID_SUGGESTION, |
| 69 | + * fix(fixer) { |
| 70 | + yield fixer.replaceText(jsonParse.callee, 'structuredClone'); |
| 71 | + |
| 72 | + const {sourceCode} = context; |
| 73 | + |
| 74 | + yield fixer.remove(jsonStringify.callee); |
| 75 | + yield * removeParentheses(jsonStringify.callee, fixer, sourceCode); |
| 76 | + |
| 77 | + const openingParenthesisToken = sourceCode.getTokenAfter(jsonStringify.callee, isOpeningParenToken); |
| 78 | + yield fixer.remove(openingParenthesisToken); |
| 79 | + |
| 80 | + const [ |
| 81 | + penultimateToken, |
| 82 | + closingParenthesisToken, |
| 83 | + ] = sourceCode.getLastTokens(jsonStringify, 2); |
| 84 | + |
| 85 | + if (isCommaToken(penultimateToken)) { |
| 86 | + yield fixer.remove(penultimateToken); |
| 87 | + } |
| 88 | + |
| 89 | + yield fixer.remove(closingParenthesisToken); |
| 90 | + }, |
| 91 | + }, |
| 92 | + ], |
| 93 | + }; |
| 94 | + }); |
| 95 | + |
| 96 | + // `_.cloneDeep(foo)` |
| 97 | + context.on('CallExpression', callExpression => { |
| 98 | + if (!isCallExpression(callExpression, { |
| 99 | + argumentsLength: 1, |
| 100 | + optional: false, |
| 101 | + })) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + const {callee} = callExpression; |
| 106 | + const matchedFunction = functions.find(nameOrPath => isNodeMatchesNameOrPath(callee, nameOrPath)); |
| 107 | + |
| 108 | + if (!matchedFunction) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + return { |
| 113 | + node: callee, |
| 114 | + messageId: MESSAGE_ID_ERROR, |
| 115 | + data: { |
| 116 | + description: `${matchedFunction.trim()}(…)`, |
| 117 | + }, |
| 118 | + suggest: [ |
| 119 | + { |
| 120 | + messageId: MESSAGE_ID_SUGGESTION, |
| 121 | + fix: fixer => fixer.replaceText(callee, 'structuredClone'), |
| 122 | + }, |
| 123 | + ], |
| 124 | + }; |
| 125 | + }); |
| 126 | +}; |
| 127 | + |
| 128 | +const schema = [ |
| 129 | + { |
| 130 | + type: 'object', |
| 131 | + additionalProperties: false, |
| 132 | + properties: { |
| 133 | + functions: { |
| 134 | + type: 'array', |
| 135 | + uniqueItems: true, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | +]; |
| 140 | + |
| 141 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 142 | +module.exports = { |
| 143 | + create, |
| 144 | + meta: { |
| 145 | + type: 'suggestion', |
| 146 | + docs: { |
| 147 | + description: 'Prefer using `structuredClone` to create a deep clone.', |
| 148 | + }, |
| 149 | + hasSuggestions: true, |
| 150 | + schema, |
| 151 | + messages, |
| 152 | + }, |
| 153 | +}; |
0 commit comments