Skip to content

Commit bff488b

Browse files
committed
✨ add typescript rules
1 parent e5c0ce4 commit bff488b

File tree

5 files changed

+71
-12
lines changed

5 files changed

+71
-12
lines changed

eslint-config-typescript/base.yaml

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,55 @@
11
extends:
2-
- 'plugin:@typescript-eslint/strict'
3-
# - 'plugin:@shopify/typescript'
2+
- 'plugin:@typescript-eslint/strict-type-checked'
3+
- 'plugin:@typescript-eslint/stylistic-type-checked'
44
- 'plugin:import/typescript'
55

66
parserOptions:
77
project: true
8-
98
rules:
9+
# allow specifying a type that could be inferred
1010
'@typescript-eslint/no-inferrable-types': 'off'
11+
12+
# prevent unused vars
1113
'@typescript-eslint/no-unused-vars':
1214
['warn', { args: 'none', destructuredArrayIgnorePattern: '^_' }]
13-
'@typescript-eslint/naming-convention': 'off'
14-
'@shopify/binary-assignment-parens': 'off'
15+
16+
# ensure only boolean types are used for conditions
17+
'@typescript-eslint/strict-boolean-expressions':
18+
- 'error'
19+
- allowString: false
20+
allowNumber: false
21+
allowNullableObject: false
22+
23+
# ensure switches on union types handle all cases
24+
'@typescript-eslint/switch-exhaustiveness-check': 'error'
25+
26+
# ensure any constant numbers are properly named to increase readability
27+
'@typescript-eslint/no-magic-numbers':
28+
- 'error'
29+
- enforceConst: true
30+
ignoreArrayIndexes: true
31+
ignoreDefaultValues: true
32+
ignoreClassFieldInitialValues: true
33+
34+
ignoreEnums: true
35+
ignoreNumericLiteralTypes: true
36+
ignoreReadonlyClassProperties: true
37+
ignoreTypeIndexes: true
38+
no-magic-numbers: 'off'
39+
40+
# TODO: do we want this?
41+
# require enum values to be initialized in order to prevent value shifting
42+
'@typescript-eslint/prefer-enum-initializers': 'error'
43+
44+
# allow use of both `type` and `interface`
45+
'@typescript-eslint/consistent-type-definitions': 'off'
46+
47+
# ensure private members are marked readonly when possible
48+
'@typescript-eslint/prefer-readonly': 'error'
49+
50+
# allow `indexOf()`
51+
'@typescript-eslint/prefer-includes': 'off'
52+
53+
# typescript handles these rules for us
54+
consistent-return: 'off'
55+
no-invalid-this: 'off'

eslint-config-typescript/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
"test": "node --require ts-node/register --test test/index.ts"
2121
},
2222
"dependencies": {
23-
"@typescript-eslint/eslint-plugin": "^5.30.7",
24-
"@typescript-eslint/parser": "^5.30.7"
23+
"@typescript-eslint/eslint-plugin": "^6.7.3",
24+
"@typescript-eslint/parser": "^6.7.3",
25+
"eslint-plugin-import": "^2.28.1"
2526
},
2627
"peerDependencies": {
2728
"@dudeofawesome/eslint-config": "^1.3.0",

eslint-config-typescript/test/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ describe('eslint-config-typescript', () => {
2323
it(`should fail radix`, async () =>
2424
testRuleFail(linter, `parseInt('10');\n`, 'radix', true));
2525

26-
// TODO: test for shopify rule
27-
28-
// TODO: test for prettier rule
26+
it(`should fail @typescript-eslint/strict-boolean-expressions`, async () =>
27+
testRuleFail(
28+
linter,
29+
`let foo: unknown = 'foo';\nfoo = 'bar';\nif (foo) Number();\n`,
30+
'@typescript-eslint/strict-boolean-expressions',
31+
true,
32+
));
2933
});
3034
});

eslint-config/rules/readability.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ rules:
33
no-lonely-if: 'error'
44

55
# ensure any constant numbers are properly named to increase readability
6-
no-magic-numbers: 'error'
6+
no-magic-numbers:
7+
- 'error'
8+
- enforceConst: true
9+
ignoreArrayIndexes: true
10+
ignoreDefaultValues: true
11+
ignoreClassFieldInitialValues: true
712

813
# don't allow multiple variable creation with single keyword
914
one-var: ['error', 'never']

utils/testing/eslint.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ export function filePath(typescript: boolean = false): string {
99
export function defaultTestSet(linter: ESLint) {
1010
describe('[standard tests] passes', () => {
1111
it(`should parse javascript`, () =>
12-
testNoFail(linter, `((a) => a.split(''))()\n`));
12+
testNoFail(
13+
linter,
14+
`
15+
(
16+
/** @param {string} a */
17+
(a) => a.split('')
18+
)('test');
19+
`,
20+
));
1321

1422
it(`should allow nested ternaries`, () =>
1523
testNoFail(

0 commit comments

Comments
 (0)