Skip to content

Commit 86fadc6

Browse files
feat: revisit TypeScript ruleset, upgrade to v2
1 parent 3180ae0 commit 86fadc6

4 files changed

Lines changed: 247 additions & 45 deletions

File tree

packages/eslint-config-typescript/index.js

Lines changed: 113 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module.exports = {
4444
},
4545

4646
rules: {
47+
4748
// TS code is mostly self-documented and having JSDoc directives for everything is redundant
4849
// when you can easily infer return values and argument types from the code itself.
4950
'valid-jsdoc': 'off',
@@ -56,10 +57,31 @@ module.exports = {
5657
// typescript specific declarations properly.
5758
'no-unused-vars': 'off',
5859

60+
'no-useless-constructor': 'off',
61+
5962
// Require that member overloads be consecutive
6063
// Grouping overloaded members together can improve readability of the code.
6164
'@typescript-eslint/adjacent-overload-signatures': 'warn',
6265

66+
// Requires using either T[] or Array<T> for arrays
67+
// This rule aims to standardise usage of array types within your codebase.
68+
'@typescript-eslint/array-type': ['warn', {
69+
default: 'array-simple',
70+
readonly: 'array-simple',
71+
}],
72+
73+
// Disallows awaiting a value that is not a Promise
74+
// This rule disallows awaiting a value that is not a "Thenable" (an object which has then
75+
// method, such as a Promise). While it is valid JavaScript to await a non-Promise-like value
76+
// (it will resolve immediately), this pattern is often a programmer error, such as forgetting
77+
// to add parenthesis to call a function that returns a Promise.
78+
'@typescript-eslint/await-thenable': 'warn',
79+
80+
// Bans “// @ts-ignore” comments from being used
81+
// Suppressing Typescript Compiler Errors can be hard to discover.
82+
'@typescript-eslint/ban-ts-ignore': 'error',
83+
84+
6385
// Require PascalCased class and interface names
6486
// This rule aims to make it easy to differentiate classes from regular variables at a glance.
6587
'@typescript-eslint/class-name-casing': 'warn',
@@ -75,28 +97,15 @@ module.exports = {
7597

7698
// Require explicit accessibility modifiers on class properties and methods
7799
// This rule aims to make code more readable and explicit about who can use which properties.
78-
'@typescript-eslint/explicit-member-accessibility': 'warn',
79-
80-
// Require a specific member delimiter style for interfaces and type literals
81-
// This rule aims to standardise the way interface and type literal members are delimited.
82-
'@typescript-eslint/member-delimiter-style': ['warn', {
83-
multiline: {
84-
delimiter: 'none',
85-
},
86-
singleline: {
87-
delimiter: 'comma',
88-
},
100+
'@typescript-eslint/explicit-member-accessibility': ['warn', {
101+
accessibility: 'no-public',
89102
}],
90103

91104
// Require a consistent member declaration order
92105
// A consistent ordering of fields, methods and constructors can make interfaces, type literals,
93106
// classes and class expressions easier to read, navigate and edit.
94107
'@typescript-eslint/member-ordering': 'warn',
95108

96-
// Enforces the use of `as Type` assertions instead of `<Type>` assertions
97-
// This rule aims to standardise the use of type assertion style across the codebase.
98-
'@typescript-eslint/no-angle-bracket-type-assertion': 'warn',
99-
100109
// Disallow generic Array constructors
101110
// Use of the Array constructor to construct a new array is generally discouraged in favor of
102111
// array literal notation because of the single-argument pitfall and because the Array global
@@ -115,6 +124,37 @@ module.exports = {
115124
// the type can be easily inferred from its value.
116125
'@typescript-eslint/no-explicit-any': 'warn',
117126

127+
// Forbids the use of classes as namespaces
128+
// This rule warns when a class is accidentally used as a namespace.
129+
'@typescript-eslint/no-extraneous-class': ['warn', {
130+
allowConstructorOnly: true,
131+
}],
132+
133+
// @TODO(semver-major): -> error
134+
// Requires Promise-like values to be handled appropriately
135+
// This rule forbids usage of Promise-like values in statements without handling their errors
136+
// appropriately. Unhandled promises can cause several issues, such as improperly sequenced
137+
// operations, ignored Promise rejections and more.
138+
'@typescript-eslint/no-floating-promises': 'warn',
139+
140+
// @TODO(semver-major): -> error. The indexes are treated as strings!
141+
// Disallow iterating over an array with a for-in loop
142+
// A for-in loop (for (var k in o)) iterates over the properties of an Object. While it is legal
143+
// to use for-in loops with array types, it is not common. for-in will iterate over the indices
144+
// of the array as strings, omitting any "holes" in the array.
145+
'@typescript-eslint/no-for-in-array': 'warn',
146+
147+
// @TODO(semver-major): -> error
148+
// Enforce valid definition of new and constructor
149+
// Warns on apparent attempts to define constructors for interfaces or new for classes.
150+
'@typescript-eslint/no-misused-new': 'warn',
151+
152+
// @TODO(semver-major): -> error
153+
// Avoid using promises in places not designed to handle them
154+
// This rule forbids using promises in places where the Typescript compiler allows them but they
155+
// are not handled properly. These situations can often arise due to a missing await keyword or
156+
// just a misunderstanding of the way async functions are handled/awaited.
157+
'@typescript-eslint/no-misused-promises': 'warn',
118158

119159
// Disallow the use of custom TypeScript modules and namespaces
120160
// Custom TypeScript modules (module foo {}) and namespaces (namespace foo {}) are considered
@@ -131,14 +171,38 @@ module.exports = {
131171
// explicitly declare all properties in the class.
132172
'@typescript-eslint/no-parameter-properties': 'warn',
133173

174+
// Disallows invocation of require()
175+
// Prefer the newer ES6-style imports over require().
176+
'@typescript-eslint/no-require-imports': 'warn',
177+
178+
// Disallow aliasing this
179+
// Assigning a variable to this instead of properly using arrow lambdas may be a symptom of
180+
// pre-ES6 practices or not managing scope well.
181+
'@typescript-eslint/no-this-alias': ['warn', {
182+
allowDestructuring: true,
183+
}],
184+
185+
// Warns when a namespace qualifier is unnecessary
186+
// This rule aims to let users know when a namespace or enum qualifier is unnecessary, whether
187+
// used for a type or for a value.
188+
'@typescript-eslint/no-unnecessary-qualifier': 'warn',
189+
190+
// Warns if a type assertion does not change the type of an expression
191+
// This rule aims to prevent unnecessary type assertions.
192+
'@typescript-eslint/no-unnecessary-type-assertion': 'warn',
193+
134194
// Disallow /// <reference path="" /> comments
135195
// Triple-slash reference directive comments should not be used anymore. Use import instead.
136-
'@typescript-eslint/no-triple-slash-reference': 'error',
196+
'@typescript-eslint/triple-slash-reference': ['error', {
197+
path: 'never',
198+
types: 'never',
199+
lib: 'never',
200+
}],
137201

138202
// Variables that are declared and not used anywhere in the code are most likely an error due
139203
// to incomplete refactoring. Such variables take up space in the code and can lead to
140204
// confusion by readers.
141-
'@typescript-eslint/no-unused-vars': 'error',
205+
'@typescript-eslint/no-unused-vars': base.rules['no-unused-vars'],
142206

143207
// Disallow the use of variables before they are defined
144208
// This rule will warn when it encounters a reference to an identifier that has not yet been
@@ -154,16 +218,44 @@ module.exports = {
154218
// style imports or import foo = require("foo") imports.
155219
'@typescript-eslint/no-var-requires': 'error',
156220

221+
// Disallow unnecessary constructors
222+
// This rule flags class constructors that can be safely removed without changing how the class
223+
// works.
224+
'@typescript-eslint/no-useless-constructor': base.rules['no-useless-constructor'],
225+
157226
// Require the use of the namespace keyword instead of the module keyword to declare custom
158227
// TypeScript modules
159228
// In an effort to prevent further confusion between custom TypeScript modules and the new
160229
// ES2015 modules, starting with TypeScript v1.5 the keyword namespace is now the preferred way
161230
// to declare custom TypeScript modules.
162231
'@typescript-eslint/prefer-namespace-keyword': 'warn',
163232

164-
// Require consistent spacing around type annotations
165-
// This rule aims to enforce specific spacing patterns around type annotations and function
166-
// types in type literals.
167-
'@typescript-eslint/type-annotation-spacing': 'warn',
233+
// Functions that return promises must be async
234+
235+
// Requires any function or method that returns a Promise to be marked async. Ensures that each
236+
// function is only capable of:
237+
// - returning a rejected promise, or
238+
// - throwing an Error object
239+
//
240+
// In contrast, non-async Promise-returning functions are technically capable of either. Code
241+
// that handles the results of those functions will often need to handle both cases, which can
242+
// get complex.
243+
'@typescript-eslint/promise-function-async': 'warn',
244+
245+
// Enforce giving compare argument to Array#sort
246+
// This rule is aimed at preventing the calls of Array#sort method. This rule ignores the sort
247+
// methods of user-defined types.
248+
'@typescript-eslint/require-array-sort-compare': 'warn',
249+
250+
// Disallow async functions which have no await expression
251+
'@typescript-eslint/require-await': base.rules['require-await'],
252+
253+
// @TODO(semver-major): -> error
254+
// When adding two variables, operands must both be of type number or of type string
255+
'@typescript-eslint/restrict-plus-operands': 'warn',
256+
257+
// Enforces unbound methods are called with their expected scope
258+
// Class functions don't preserve the class scope when passed as standalone variables.
259+
'@typescript-eslint/unbound-method': 'warn',
168260
},
169261
}

packages/eslint-config-typescript/package-lock.json

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/eslint-config-typescript/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
],
1313
"dependencies": {
1414
"@strv/eslint-config-base": "^2.0.0",
15-
"@typescript-eslint/eslint-plugin": "^1.6.0",
16-
"@typescript-eslint/parser": "^1.6.0"
15+
"@typescript-eslint/eslint-plugin": "^2.0.0",
16+
"@typescript-eslint/parser": "^2.0.0"
1717
},
1818
"engines": {
1919
"node": ">=10"

0 commit comments

Comments
 (0)