javascript-eslint-dot-notation
Transform bracket notation with static string literal keys (e.g., `obj['prop']`) into equivalent dot notation (`obj.prop`) across JavaScript/TypeScript codebases when semantics are preserved, aligning with ESLint `dot-notation` best practices and improving readability and maintainability.
# javascript-eslint-dot-notation
Transform bracket notation with static string literal keys (e.g., `obj['prop']`) into equivalent dot notation (`obj.prop`) across JavaScript/TypeScript codebases when semantics are preserved, aligning with ESLint `dot-notation` best practices and improving readability and maintainability.
## Installation
```bash
# Install from registry
pnpm dlx codemod@latest javascript-eslint-dot-notation
# Or run locally
pnpm dlx codemod@latest workflow run -w .
```
## Usage
This codemod rewrites runtime `subscript_expression` member access into dot notation when the key is a compile-time string literal and the rewrite is semantically safe.
Targets:
`**/*.{js,jsx,mjs,cjs,ts,tsx}`
Examples:
```js
user['name'];
foo?.['bar'];
delete config['cache'];
obj['method']();
(getObj())['prop'];
```
becomes:
```js
user.name;
foo?.bar;
delete config.cache;
obj.method();
(getObj()).prop;
```
Preserve/no-op cases:
- Dynamic access like `obj[key]`
- Keys that are not valid dot-notation identifiers like `obj['kebab-case']`, `obj['with space']`, `obj['0']`, `obj['a.b']`, and `obj['#private']`
- Keys expressed with escapes or comments inside the brackets when the exact trivia cannot be preserved safely
- Type-only indexed access such as `type Name = User['name']`
Parameters:
- `allowKeywords` defaults to `true` and allows rewrites such as `obj['default'] -> obj.default`
- `allowPattern` preserves matching property names in bracket notation
- `unicodeIdentifiers` defaults to `false`; enable it to allow non-ASCII IdentifierNames
## Development
```bash
# Test the transformation
pnpm test
# Validate the workflow
pnpm dlx codemod@latest workflow validate -w workflow.yaml
# Publish to registry
codemod login
codemod publish
```
## License
MIT