Skip to content

Commit

Permalink
Merge pull request #1086 from votingworks/style/lint/enforce-and-fix-…
Browse files Browse the repository at this point in the history
…gts-no-dollar-sign-names

style(lint): enforce & fix `gts-no-dollar-sign-names`
  • Loading branch information
eventualbuddha authored Oct 18, 2021
2 parents 37912b9 + db98456 commit 73964df
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 4 deletions.
20 changes: 20 additions & 0 deletions libs/eslint-plugin-vx/docs/rules/gts-no-dollar-sign-names.md
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)
```
1 change: 1 addition & 0 deletions libs/eslint-plugin-vx/src/configs/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export = {
rules: {
'vx/gts-direct-module-export-access-only': 'error',
'vx/gts-no-array-constructor': 'error',
'vx/gts-no-dollar-sign-names': 'error',
'vx/gts-no-foreach': 'error',
'vx/gts-no-import-export-type': ['error', { allowReexport: true }],
'vx/gts-no-private-fields': 'error',
Expand Down
45 changes: 45 additions & 0 deletions libs/eslint-plugin-vx/src/rules/gts-no-dollar-sign-names.ts
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,
})
}
},
}
},
})
2 changes: 2 additions & 0 deletions libs/eslint-plugin-vx/src/rules/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TSESLint } from '@typescript-eslint/experimental-utils'
import gtsDirectModuleExportAccessOnly from './gts-direct-module-export-access-only'
import gtsNoArrayConstructor from './gts-no-array-constructor'
import gtsNoDollarSignNames from './gts-no-dollar-sign-names'
import gtsNoForeach from './gts-no-foreach'
import gtsNoImportExportType from './gts-no-import-export-type'
import gtsNoPrivateFields from './gts-no-private-fields'
Expand All @@ -13,6 +14,7 @@ import noFloatingVoids from './no-floating-results'
export default {
'gts-direct-module-export-access-only': gtsDirectModuleExportAccessOnly,
'gts-no-array-constructor': gtsNoArrayConstructor,
'gts-no-dollar-sign-names': gtsNoDollarSignNames,
'gts-no-foreach': gtsNoForeach,
'gts-no-import-export-type': gtsNoImportExportType,
'gts-no-private-fields': gtsNoPrivateFields,
Expand Down
46 changes: 46 additions & 0 deletions libs/eslint-plugin-vx/tests/rules/gts-no-dollar-sign-names.test.ts
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 }],
},
],
})
8 changes: 4 additions & 4 deletions libs/hmpb-interpreter/.eslintrc
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"] }]
}
}

0 comments on commit 73964df

Please sign in to comment.