|
| 1 | +'use strict'; |
| 2 | +const {isCommaToken} = require('eslint-utils'); |
| 3 | +const getDocumentationUrl = require('./utils/get-documentation-url'); |
| 4 | +const methodSelector = require('./utils/method-selector'); |
| 5 | + |
| 6 | +const MESSAGE_ID = 'array-join-separator'; |
| 7 | +const messages = { |
| 8 | + [MESSAGE_ID]: 'Missing the separator argument.' |
| 9 | +}; |
| 10 | + |
| 11 | +const emptyArraySelector = path => { |
| 12 | + const prefix = `${path}.`; |
| 13 | + return [ |
| 14 | + `[${prefix}type="ArrayExpression"]`, |
| 15 | + `[${prefix}elements.length=0]` |
| 16 | + ].join(''); |
| 17 | +}; |
| 18 | + |
| 19 | +const memberExpressionSelector = (path, {property, object}) => { |
| 20 | + const prefix = `${path}.`; |
| 21 | + |
| 22 | + const parts = [ |
| 23 | + `[${prefix}type="MemberExpression"]`, |
| 24 | + `[${prefix}computed=false]`, |
| 25 | + `[${prefix}optional!=true]`, |
| 26 | + `[${prefix}property.type="Identifier"]`, |
| 27 | + `[${prefix}property.name="${property}"]` |
| 28 | + ]; |
| 29 | + |
| 30 | + if (object) { |
| 31 | + parts.push( |
| 32 | + `[${prefix}object.type="Identifier"]`, |
| 33 | + `[${prefix}object.name="${object}"]` |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + return parts.join(''); |
| 38 | +}; |
| 39 | + |
| 40 | +// `foo.join()` |
| 41 | +const arrayJoin = methodSelector({ |
| 42 | + name: 'join', |
| 43 | + length: 0 |
| 44 | +}); |
| 45 | + |
| 46 | +// `[].join.call(foo)` and `Array.prototype.join.call(foo)` |
| 47 | +const arrayPrototypeJoin = [ |
| 48 | + methodSelector({ |
| 49 | + name: 'call', |
| 50 | + length: 1 |
| 51 | + }), |
| 52 | + memberExpressionSelector('callee.object', {property: 'join'}), |
| 53 | + `:matches(${ |
| 54 | + [ |
| 55 | + emptyArraySelector('callee.object.object'), |
| 56 | + memberExpressionSelector('callee.object.object', {property: 'prototype', object: 'Array'}) |
| 57 | + ].join(', ') |
| 58 | + })` |
| 59 | +].join(''); |
| 60 | + |
| 61 | +const selector = `:matches(${arrayJoin}, ${arrayPrototypeJoin})`; |
| 62 | + |
| 63 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 64 | +const create = context => { |
| 65 | + return { |
| 66 | + [selector](node) { |
| 67 | + const [penultimateToken, lastToken] = context.getSourceCode().getLastTokens(node, 2); |
| 68 | + const isPrototypeMethod = node.arguments.length === 1; |
| 69 | + context.report({ |
| 70 | + loc: { |
| 71 | + start: penultimateToken.loc[isPrototypeMethod ? 'end' : 'start'], |
| 72 | + end: lastToken.loc.end |
| 73 | + }, |
| 74 | + messageId: MESSAGE_ID, |
| 75 | + /** @param {import('eslint').Rule.RuleFixer} fixer */ |
| 76 | + fix(fixer) { |
| 77 | + let text = '\',\''; |
| 78 | + |
| 79 | + if (isPrototypeMethod) { |
| 80 | + text = isCommaToken(penultimateToken) ? `${text},` : `, ${text}`; |
| 81 | + } |
| 82 | + |
| 83 | + return fixer.insertTextBefore(lastToken, text); |
| 84 | + } |
| 85 | + }); |
| 86 | + } |
| 87 | + }; |
| 88 | +}; |
| 89 | + |
| 90 | +const schema = []; |
| 91 | + |
| 92 | +module.exports = { |
| 93 | + create, |
| 94 | + meta: { |
| 95 | + type: 'suggestion', |
| 96 | + docs: { |
| 97 | + description: 'Enforce using the separator argument with `Array#join()`.', |
| 98 | + url: getDocumentationUrl(__filename) |
| 99 | + }, |
| 100 | + fixable: 'code', |
| 101 | + schema, |
| 102 | + messages |
| 103 | + } |
| 104 | +}; |
0 commit comments