|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as eslint from 'eslint'; |
| 7 | +import { TSESTree } from '@typescript-eslint/utils'; |
| 8 | +import * as ts from 'typescript'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Disallow bracket notation for accessing properties that are valid identifiers, |
| 12 | + * especially private members (starting with underscore). Bracket notation should |
| 13 | + * only be used for properties with special characters or computed property names. |
| 14 | + * |
| 15 | + * Bad: obj['_privateMember'] |
| 16 | + * Bad: obj['normalProperty'] |
| 17 | + * Good: obj._privateMember // TypeScript will catch private access |
| 18 | + * Good: obj.normalProperty |
| 19 | + * Good: obj['property-with-dashes'] |
| 20 | + * Good: obj[computedKey] |
| 21 | + */ |
| 22 | +export default new class NoBracketNotationForIdentifiers implements eslint.Rule.RuleModule { |
| 23 | + |
| 24 | + readonly meta: eslint.Rule.RuleMetaData = { |
| 25 | + type: 'problem', |
| 26 | + docs: { |
| 27 | + description: 'Disallow bracket notation for accessing properties that are valid identifiers' |
| 28 | + }, |
| 29 | + messages: { |
| 30 | + noBracketNotation: 'Use dot notation instead of bracket notation for property \'{{property}}\'. Bracket notation bypasses TypeScript\'s type checking and access modifiers.' |
| 31 | + }, |
| 32 | + schema: [], |
| 33 | + fixable: 'code' |
| 34 | + }; |
| 35 | + |
| 36 | + create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener { |
| 37 | + |
| 38 | + /** |
| 39 | + * Check if a string is a valid JavaScript identifier |
| 40 | + */ |
| 41 | + function isValidIdentifier(str: string): boolean { |
| 42 | + if (str.includes('\\')) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + const scanner = ts.createScanner(ts.ScriptTarget.Latest, false, ts.LanguageVariant.Standard, str); |
| 46 | + const token = scanner.scan(); |
| 47 | + const isIdentifierName = token === ts.SyntaxKind.Identifier |
| 48 | + || (token >= ts.SyntaxKind.FirstKeyword && token <= ts.SyntaxKind.LastKeyword); |
| 49 | + return isIdentifierName && scanner.getTokenText() === str && scanner.scan() === ts.SyntaxKind.EndOfFileToken; |
| 50 | + } |
| 51 | + |
| 52 | + return { |
| 53 | + MemberExpression(node: any) { |
| 54 | + const memberExpr = node as TSESTree.MemberExpression; |
| 55 | + |
| 56 | + // Only check computed member expressions (bracket notation) |
| 57 | + if (!memberExpr.computed) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // Only check string literals in brackets |
| 62 | + if (memberExpr.property.type !== 'Literal' || typeof memberExpr.property.value !== 'string') { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + const propertyName = memberExpr.property.value; |
| 67 | + |
| 68 | + // If it's a valid identifier, report it |
| 69 | + if (isValidIdentifier(propertyName)) { |
| 70 | + context.report({ |
| 71 | + node: memberExpr.property, |
| 72 | + messageId: 'noBracketNotation', |
| 73 | + data: { |
| 74 | + property: propertyName |
| 75 | + }, |
| 76 | + fix(fixer) { |
| 77 | + const property = memberExpr.property as unknown as eslint.Rule.Node; |
| 78 | + const leftBracket = context.sourceCode.getTokenBefore(property); |
| 79 | + const rightBracket = context.sourceCode.getTokenAfter(property); |
| 80 | + if (leftBracket?.value !== '[' || rightBracket?.value !== ']') { |
| 81 | + return null; |
| 82 | + } |
| 83 | + const hasComments = context.sourceCode |
| 84 | + .getTokensBetween(leftBracket, rightBracket, { includeComments: true }) |
| 85 | + .some(token => token.type === 'Block' || token.type === 'Line'); |
| 86 | + if (hasComments) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + const bracketFix = fixer.replaceTextRange( |
| 91 | + [leftBracket.range[0], rightBracket.range[1]], |
| 92 | + `${memberExpr.optional ? '' : '.'}${propertyName}` |
| 93 | + ); |
| 94 | + if (memberExpr.object.type === 'Literal' && typeof memberExpr.object.value === 'number') { |
| 95 | + const object = memberExpr.object as unknown as eslint.Rule.Node; |
| 96 | + return [ |
| 97 | + fixer.insertTextBefore(object, '('), |
| 98 | + fixer.insertTextAfter(object, ')'), |
| 99 | + bracketFix |
| 100 | + ]; |
| 101 | + } |
| 102 | + |
| 103 | + return bracketFix; |
| 104 | + } |
| 105 | + }); |
| 106 | + } |
| 107 | + } |
| 108 | + }; |
| 109 | + } |
| 110 | +}; |
0 commit comments