-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style(lint): enforce & fix
gts-no-dollar-sign-names
This new rule enforces that we do not have any `$` characters in our identifier names except where that's the right idiom. We enable `$0` in `hmpb-interpreter` since it uses it to refer to the process name as is idiomatic in *nix environments.
- Loading branch information
1 parent
37912b9
commit db98456
Showing
6 changed files
with
118 additions
and
4 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
libs/eslint-plugin-vx/docs/rules/gts-no-dollar-sign-names.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Disallows use of $ in identifiers, except when aligning with naming conventions for third party frameworks. (`vx/gts-no-dollar-sign-names`) | ||
|
||
This rule is from | ||
[Google TypeScript Style Guide section "Identifiers"](https://google.github.io/styleguide/tsguide.html#identifiers): | ||
|
||
> Identifiers should not generally use `$`, except when aligning with naming conventions for third party frameworks. | ||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
const $button = useRef<HTMLButtonElement>(null) | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
const buttonRef = useRef<HTMLButtonElement>(null) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
libs/eslint-plugin-vx/src/rules/gts-no-dollar-sign-names.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { TSESTree } from '@typescript-eslint/experimental-utils' | ||
import { createRule } from '../util' | ||
|
||
export default createRule({ | ||
name: 'gts-no-dollar-sign-names', | ||
meta: { | ||
docs: { | ||
description: | ||
'Disallows use of $ in identifiers, except when aligning with naming conventions for third party frameworks.', | ||
category: 'Best Practices', | ||
recommended: 'error', | ||
suggestion: false, | ||
requiresTypeChecking: false, | ||
}, | ||
messages: { | ||
noDollarSign: `Do not use $ in names`, | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
allowedNames: { type: 'array', items: { type: 'string' } }, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
type: 'problem', | ||
}, | ||
defaultOptions: [{ allowedNames: [] as string[] }], | ||
|
||
create(context) { | ||
const { allowedNames = [] } = context.options[0] ?? {} | ||
|
||
return { | ||
Identifier(node: TSESTree.Identifier): void { | ||
if (node.name.includes('$') && !allowedNames.includes(node.name)) { | ||
context.report({ | ||
messageId: 'noDollarSign', | ||
node, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
libs/eslint-plugin-vx/tests/rules/gts-no-dollar-sign-names.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { ESLintUtils } from '@typescript-eslint/experimental-utils' | ||
import { join } from 'path' | ||
import rule from '../../src/rules/gts-no-dollar-sign-names' | ||
|
||
const ruleTester = new ESLintUtils.RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
tsconfigRootDir: join(__dirname, '../fixtures'), | ||
project: './tsconfig.json', | ||
}, | ||
parser: '@typescript-eslint/parser', | ||
}) | ||
|
||
ruleTester.run('gts-no-dollar-sign-names', rule, { | ||
valid: [ | ||
{ code: `abc` }, | ||
{ code: `'$abc'` }, | ||
{ | ||
code: `$0`, | ||
options: [{ allowedNames: ['$0'] }], | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: '$abc', | ||
errors: [{ messageId: 'noDollarSign', line: 1 }], | ||
}, | ||
{ | ||
code: '$abc()', | ||
errors: [{ messageId: 'noDollarSign', line: 1 }], | ||
}, | ||
{ | ||
// Do we ever want to specifically allow jQuery? | ||
code: `const $button = $('.button')`, | ||
errors: [ | ||
{ messageId: 'noDollarSign', line: 1 }, | ||
{ messageId: 'noDollarSign', line: 1 }, | ||
], | ||
}, | ||
{ | ||
// Do we ever want to specifically allow RxJS? | ||
code: 'device$.subscribe()', | ||
errors: [{ messageId: 'noDollarSign', line: 1 }], | ||
}, | ||
], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
{ | ||
"extends": [ | ||
"plugin:vx/recommended" | ||
], | ||
"extends": ["plugin:vx/recommended"], | ||
"rules": { | ||
"camelcase": ["error", { "allow": ["matrix_t", "^.+_\\d+"] }], | ||
"new-cap": ["error", { "newIsCapExceptionPattern": "^jsfeat\\.." }], | ||
"no-bitwise": "off" | ||
"no-bitwise": "off", | ||
|
||
"vx/gts-no-dollar-sign-names": ["error", { "allowedNames": ["$0"] }] | ||
} | ||
} |