Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions .eslint-plugin-local/code-no-bracket-notation-for-identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,23 @@
*--------------------------------------------------------------------------------------------*/

import * as eslint from 'eslint';
import type * as ESTree from 'estree';
import { TSESTree } from '@typescript-eslint/utils';
import * as ts from 'typescript';

/**
* Disallow bracket notation for accessing properties that are valid identifiers,
* especially private members (starting with underscore). Bracket notation should
* only be used for properties with special characters or computed property names.
*
* Bad: obj['_privateMember']
* Bad: obj['normalProperty']
* Good: obj._privateMember // TypeScript will catch private access
* Good: obj.normalProperty
* Good: obj['property-with-dashes']
* Good: obj[computedKey]
* Prefer dot notation for identifier properties, including TypeScript private members.
* Allow bracket notation for environment-variable names accessed directly through process.env.
*/
export default new class NoBracketNotationForIdentifiers implements eslint.Rule.RuleModule {

readonly meta: eslint.Rule.RuleMetaData = {
type: 'problem',
docs: {
description: 'Disallow bracket notation for accessing properties that are valid identifiers'
description: 'Disallow bracket notation for identifier properties except on process.env'
},
messages: {
noBracketNotation: 'Use dot notation instead of bracket notation for property \'{{property}}\'. Bracket notation bypasses TypeScript\'s type checking and access modifiers.'
noBracketNotation: 'Use dot notation instead of bracket notation for property \'{{property}}\'.'
},
schema: [],
fixable: 'code'
Expand All @@ -47,7 +40,7 @@ export default new class NoBracketNotationForIdentifiers implements eslint.Rule.
}

return {
MemberExpression(node: any) {
MemberExpression(node: ESTree.MemberExpression) {
const memberExpr = node as TSESTree.MemberExpression;

// Only check computed member expressions (bracket notation)
Expand All @@ -65,6 +58,13 @@ export default new class NoBracketNotationForIdentifiers implements eslint.Rule.
return;
}

const receiver = memberExpr.object;
if (receiver.type === 'MemberExpression' && !receiver.computed
&& receiver.object.type === 'Identifier' && receiver.object.name === 'process'
&& receiver.property.type === 'Identifier' && receiver.property.name === 'env') {
return;
}

const propertyName = memberExpr.property.value;

// If it's a valid identifier, report it
Expand Down
23 changes: 23 additions & 0 deletions build/lib/test/codeNoBracketNotationForIdentifiers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ new RuleTester().run('code-no-bracket-notation-for-identifiers', rule, {
'object[`property`];',
String.raw`object["\u0061"];`,
String.raw`object["a\x62"];`,
'process.env["ProgramW6432"];',
'process.env["PROGRAMFILES"];',
'process.env["https_proxy"];',
'process.env["PATH"] = "value";',
'delete process.env["PATH"];',
'process.env.PATH;',
'process.env?.["PATH"];',
'process?.env?.["PATH"];',
'(process.env)["PATH"];',
],
invalid: [
{
Expand Down Expand Up @@ -76,5 +85,19 @@ new RuleTester().run('code-no-bracket-notation-for-identifiers', rule, {
output: null,
errors: [{ messageId: 'noBracketNotation', data: { property: 'property' } }],
},
...[
{ code: 'opts["f"];', output: 'opts.f;', property: 'f' },
{ code: 'env["PATH"];', output: 'env.PATH;', property: 'PATH' },
{ code: 'safeProcess.env["PATH"];', output: 'safeProcess.env.PATH;', property: 'PATH' },
{ code: 'process.versions["node"];', output: 'process.versions.node;', property: 'node' },
{ code: 'process.env.nested["PATH"];', output: 'process.env.nested.PATH;', property: 'PATH' },
{ code: 'other.process.env["PATH"];', output: 'other.process.env.PATH;', property: 'PATH' },
{ code: 'process[key]["PATH"];', output: 'process[key].PATH;', property: 'PATH' },
{ code: 'const env = process.env; env["PATH"];', output: 'const env = process.env; env.PATH;', property: 'PATH' },
].map(({ code, output, property }) => ({
code,
output,
errors: [{ messageId: 'noBracketNotation', data: { property } }],
})),
],
});