Blame view
node_modules/eslint/lib/rules/dot-notation.js
4.62 KB
f7563de62
|
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
/** * @fileoverview Rule to warn about using dot notation instead of square bracket notation when possible. * @author Josh Perez */ "use strict"; //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ const astUtils = require("../ast-utils"); //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ const validIdentifier = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/; const keywords = require("../util/keywords"); module.exports = { meta: { docs: { description: "enforce dot notation whenever possible", category: "Best Practices", recommended: false }, schema: [ { type: "object", properties: { allowKeywords: { type: "boolean" }, allowPattern: { type: "string" } }, additionalProperties: false } ], fixable: "code" }, create(context) { const options = context.options[0] || {}; const allowKeywords = options.allowKeywords === void 0 || !!options.allowKeywords; const sourceCode = context.getSourceCode(); let allowPattern; if (options.allowPattern) { allowPattern = new RegExp(options.allowPattern); } return { MemberExpression(node) { if ( node.computed && node.property.type === "Literal" && validIdentifier.test(node.property.value) && (allowKeywords || keywords.indexOf(String(node.property.value)) === -1) ) { if (!(allowPattern && allowPattern.test(node.property.value))) { context.report({ node: node.property, message: "[{{propertyValue}}] is better written in dot notation.", data: { propertyValue: JSON.stringify(node.property.value) }, fix(fixer) { const leftBracket = sourceCode.getTokenAfter(node.object, astUtils.isOpeningBracketToken); const rightBracket = sourceCode.getLastToken(node); if (sourceCode.getFirstTokenBetween(leftBracket, rightBracket, { includeComments: true, filter: astUtils.isCommentToken })) { // Don't perform any fixes if there are comments inside the brackets. return null; } const textBeforeDot = astUtils.isDecimalInteger(node.object) ? " " : ""; return fixer.replaceTextRange( [leftBracket.range[0], rightBracket.range[1]], `${textBeforeDot}.${node.property.value}` ); } }); } } if ( !allowKeywords && !node.computed && keywords.indexOf(String(node.property.name)) !== -1 ) { context.report({ node: node.property, message: ".{{propertyName}} is a syntax error.", data: { propertyName: node.property.name }, fix(fixer) { const dot = sourceCode.getTokenBefore(node.property); const textAfterDot = sourceCode.text.slice(dot.range[1], node.property.range[0]); if (textAfterDot.trim()) { // Don't perform any fixes if there are comments between the dot and the property name. return null; } return fixer.replaceTextRange( [dot.range[0], node.property.range[1]], `[${textAfterDot}"${node.property.name}"]` ); } }); } } }; } }; |