Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 80d934b

Browse files
authored
chore: turn on no-poorly-typed-ts-props (typescript-eslint#1955)
1 parent b609b43 commit 80d934b

8 files changed

+48
-42
lines changed

.eslintrc.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ module.exports = {
4141
{ allow: ['arrowFunctions'] },
4242
],
4343

44+
//
45+
// Internal repo rules
46+
//
47+
48+
'@typescript-eslint/internal/no-poorly-typed-ts-props': 'error',
49+
'@typescript-eslint/internal/no-typescript-default-import': 'error',
50+
'@typescript-eslint/internal/prefer-ast-types-enum': 'error',
51+
4452
//
4553
// eslint base
4654
//
@@ -119,12 +127,6 @@ module.exports = {
119127
'import/no-self-import': 'error',
120128
// Require modules with a single export to use a default export
121129
'import/prefer-default-export': 'off', // we want everything to be named
122-
123-
//
124-
// Internal repo rules
125-
//
126-
'@typescript-eslint/internal/no-typescript-default-import': 'error',
127-
'@typescript-eslint/internal/prefer-ast-types-enum': 'error',
128130
},
129131
parserOptions: {
130132
sourceType: 'module',

packages/eslint-plugin/src/rules/no-base-to-string.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ export default util.createRule<Options, MessageIds>({
9393

9494
function collectToStringCertainty(type: ts.Type): Usefulness {
9595
const toString = typeChecker.getPropertyOfType(type, 'toString');
96-
if (toString === undefined || toString.declarations.length === 0) {
96+
const declarations = toString?.getDeclarations();
97+
if (!toString || !declarations || declarations.length === 0) {
9798
return Usefulness.Always;
9899
}
99100

@@ -107,7 +108,7 @@ export default util.createRule<Options, MessageIds>({
107108
}
108109

109110
if (
110-
toString.declarations.every(
111+
declarations.every(
111112
({ parent }) =>
112113
!ts.isInterfaceDeclaration(parent) || parent.name.text !== 'Object',
113114
)

packages/eslint-plugin/src/rules/no-unnecessary-type-arguments.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,13 @@ function getTypeParametersFromType(
111111
}
112112

113113
const sym = getAliasedSymbol(symAtLocation, checker);
114+
const declarations = sym.getDeclarations();
114115

115-
if (!sym.declarations) {
116+
if (!declarations) {
116117
return undefined;
117118
}
118119

119-
return findFirstResult(sym.declarations, decl =>
120+
return findFirstResult(declarations, decl =>
120121
tsutils.isClassLikeDeclaration(decl) ||
121122
ts.isTypeAliasDeclaration(decl) ||
122123
ts.isInterfaceDeclaration(decl)

packages/eslint-plugin/src/rules/prefer-includes.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,27 @@ export default createRule({
136136

137137
// Get the symbol of `indexOf` method.
138138
const tsNode = services.esTreeNodeToTSNodeMap.get(node.property);
139-
const indexofMethodSymbol = types.getSymbolAtLocation(tsNode);
139+
const indexofMethodDeclarations = types
140+
.getSymbolAtLocation(tsNode)
141+
?.getDeclarations();
140142
if (
141-
indexofMethodSymbol == null ||
142-
indexofMethodSymbol.declarations.length === 0
143+
indexofMethodDeclarations == null ||
144+
indexofMethodDeclarations.length === 0
143145
) {
144146
return;
145147
}
146148

147149
// Check if every declaration of `indexOf` method has `includes` method
148150
// and the two methods have the same parameters.
149-
for (const instanceofMethodDecl of indexofMethodSymbol.declarations) {
151+
for (const instanceofMethodDecl of indexofMethodDeclarations) {
150152
const typeDecl = instanceofMethodDecl.parent;
151153
const type = types.getTypeAtLocation(typeDecl);
152-
const includesMethodSymbol = type.getProperty('includes');
154+
const includesMethodDecl = type
155+
.getProperty('includes')
156+
?.getDeclarations();
153157
if (
154-
includesMethodSymbol == null ||
155-
!includesMethodSymbol.declarations.some(includesMethodDecl =>
158+
includesMethodDecl == null ||
159+
!includesMethodDecl.some(includesMethodDecl =>
156160
hasSameParameters(includesMethodDecl, instanceofMethodDecl),
157161
)
158162
) {

packages/eslint-plugin/src/rules/prefer-readonly.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ class ClassScope {
321321
public addVariableModification(node: ts.PropertyAccessExpression): void {
322322
const modifierType = this.checker.getTypeAtLocation(node.expression);
323323
if (
324-
modifierType.symbol === undefined ||
324+
!modifierType.getSymbol() ||
325325
!typeIsOrHasBaseType(modifierType, this.classType)
326326
) {
327327
return;

packages/eslint-plugin/src/rules/switch-exhaustiveness-check.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export default createRule({
129129
missingBranches: missingBranchTypes
130130
.map(missingType =>
131131
isTypeFlagSet(missingType, ts.TypeFlags.ESSymbolLike)
132-
? `typeof ${missingType.symbol.escapedName}`
132+
? `typeof ${missingType.getSymbol()?.escapedName}`
133133
: checker.typeToString(missingType),
134134
)
135135
.join(' | '),

packages/eslint-plugin/src/util/types.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ export function containsAllTypesByName(
4848
type = type.target;
4949
}
5050

51-
if (
52-
typeof type.symbol !== 'undefined' &&
53-
allowedNames.has(type.symbol.name)
54-
) {
51+
const symbol = type.getSymbol();
52+
if (symbol && allowedNames.has(symbol.name)) {
5553
return true;
5654
}
5755

@@ -90,11 +88,16 @@ export function getTypeName(
9088
// `type.getConstraint()` method doesn't return the constraint type of
9189
// the type parameter for some reason. So this gets the constraint type
9290
// via AST.
93-
const node = type.symbol.declarations[0] as ts.TypeParameterDeclaration;
94-
if (node.constraint != null) {
91+
const symbol = type.getSymbol();
92+
const decls = symbol?.getDeclarations();
93+
const typeParamDecl = decls?.[0] as ts.TypeParameterDeclaration;
94+
if (
95+
ts.isTypeParameterDeclaration(typeParamDecl) &&
96+
typeParamDecl.constraint != null
97+
) {
9598
return getTypeName(
9699
typeChecker,
97-
typeChecker.getTypeFromTypeNode(node.constraint),
100+
typeChecker.getTypeFromTypeNode(typeParamDecl.constraint),
98101
);
99102
}
100103
}
@@ -174,12 +177,8 @@ export function getDeclaration(
174177
if (!symbol) {
175178
return null;
176179
}
177-
const declarations = symbol.declarations;
178-
if (!declarations) {
179-
return null;
180-
}
181-
182-
return declarations[0];
180+
const declarations = symbol.getDeclarations();
181+
return declarations?.[0] ?? null;
183182
}
184183

185184
/**
@@ -218,22 +217,21 @@ export function typeIsOrHasBaseType(
218217
type: ts.Type,
219218
parentType: ts.Type,
220219
): boolean {
221-
if (type.symbol === undefined || parentType.symbol === undefined) {
220+
const parentSymbol = parentType.getSymbol();
221+
if (!type.getSymbol() || !parentSymbol) {
222222
return false;
223223
}
224224

225225
const typeAndBaseTypes = [type];
226226
const ancestorTypes = type.getBaseTypes();
227227

228-
if (ancestorTypes !== undefined) {
228+
if (ancestorTypes) {
229229
typeAndBaseTypes.push(...ancestorTypes);
230230
}
231231

232232
for (const baseType of typeAndBaseTypes) {
233-
if (
234-
baseType.symbol !== undefined &&
235-
baseType.symbol.name === parentType.symbol.name
236-
) {
233+
const baseSymbol = baseType.getSymbol();
234+
if (baseSymbol && baseSymbol.name === parentSymbol.name) {
237235
return true;
238236
}
239237
}

packages/eslint-plugin/tests/rules/prefer-includes.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ ruleTester.run('prefer-includes', rule, {
5757
`,
5858
`
5959
type UserDefined = {
60-
indexOf(x: any): number // don't have 'includes'.
60+
indexOf(x: any): number // don't have 'includes'
6161
}
6262
function f(a: UserDefined): void {
6363
a.indexOf(b) !== -1
@@ -66,7 +66,7 @@ ruleTester.run('prefer-includes', rule, {
6666
`
6767
type UserDefined = {
6868
indexOf(x: any, fromIndex?: number): number
69-
includes(x: any): boolean // different parameters.
69+
includes(x: any): boolean // different parameters
7070
}
7171
function f(a: UserDefined): void {
7272
a.indexOf(b) !== -1
@@ -75,7 +75,7 @@ ruleTester.run('prefer-includes', rule, {
7575
`
7676
type UserDefined = {
7777
indexOf(x: any, fromIndex?: number): number
78-
includes(x: any, fromIndex: number): boolean // different parameters.
78+
includes(x: any, fromIndex: number): boolean // different parameters
7979
}
8080
function f(a: UserDefined): void {
8181
a.indexOf(b) !== -1
@@ -84,7 +84,7 @@ ruleTester.run('prefer-includes', rule, {
8484
`
8585
type UserDefined = {
8686
indexOf(x: any, fromIndex?: number): number
87-
includes: boolean // different type.
87+
includes: boolean // different type
8888
}
8989
function f(a: UserDefined): void {
9090
a.indexOf(b) !== -1

0 commit comments

Comments
 (0)