Skip to content

Commit 9524424

Browse files
Josh Goldbergbradzacher
andauthored
chore: enable no-unsafe-assignment internally (typescript-eslint#3280)
* chore: enable no-unsafe-assignment internally * Remove unnecessary disable * Properly fix in generate-contributors * Update packages/eslint-plugin-tslint/src/rules/config.ts Co-authored-by: Brad Zacher <[email protected]> * Update packages/scope-manager/tests/util/serializers/baseSerializer.ts Co-authored-by: Brad Zacher <[email protected]> * Update from the Twitch stream ✨ * Long delayed cleanups * Cleaned up a final few lint complaints * fix: TSESTree import * fix: moving around disables Co-authored-by: Brad Zacher <[email protected]>
1 parent 5af8db6 commit 9524424

File tree

26 files changed

+56
-45
lines changed

26 files changed

+56
-45
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ module.exports = {
6464
],
6565

6666
// TODO - enable these new recommended rules
67-
'@typescript-eslint/no-unsafe-assignment': 'off',
6867
'@typescript-eslint/no-unsafe-member-access': 'off',
6968
'@typescript-eslint/no-unsafe-return': 'off',
7069
'@typescript-eslint/restrict-template-expressions': 'off',
@@ -172,6 +171,7 @@ module.exports = {
172171
'jest/globals': true,
173172
},
174173
rules: {
174+
'@typescript-eslint/no-unsafe-assignment': 'off',
175175
'eslint-plugin/no-identical-tests': 'error',
176176
'jest/no-disabled-tests': 'warn',
177177
'jest/no-focused-tests': 'error',

packages/eslint-plugin-internal/src/util/createRule.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { ESLintUtils } from '@typescript-eslint/experimental-utils';
22

33
// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
4-
const version = require('../../package.json').version;
4+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
5+
const version: string = require('../../package.json');
56

67
const createRule = ESLintUtils.RuleCreator(
78
name =>

packages/eslint-plugin-tslint/src/rules/config.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { Configuration, RuleSeverity } from 'tslint';
44
import { CustomLinter } from '../custom-linter';
55

66
// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
7-
const version = require('../../package.json').version;
7+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
8+
const version: string = require('../../package.json');
89

910
const createRule = ESLintUtils.RuleCreator(
1011
() =>
@@ -62,8 +63,8 @@ export default createRule<Options, MessageIds>({
6263
docs: {
6364
description:
6465
'Wraps a TSLint configuration and lints the whole source using TSLint',
65-
// one off special category for this plugin
66-
category: 'TSLint' as any, // eslint-disable-line @typescript-eslint/no-explicit-any
66+
// @ts-expect-error - We know this is a one off special category for this plugin
67+
category: 'TSLint',
6768
recommended: false,
6869
},
6970
fixable: 'code',

packages/eslint-plugin/src/rules/indent-new-do-not-use/TokenInfo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { TokenOrComment } from './BinarySearchTree';
99
*/
1010
export class TokenInfo {
1111
private readonly sourceCode: TSESLint.SourceCode;
12-
public firstTokensByLineNumber: Map<number, TSESTree.Token>;
12+
public readonly firstTokensByLineNumber: Map<number, TSESTree.Token>;
1313

1414
constructor(sourceCode: TSESLint.SourceCode) {
1515
this.sourceCode = sourceCode;
@@ -28,7 +28,7 @@ export class TokenInfo {
2828
}
2929
return map;
3030
},
31-
new Map(),
31+
new Map<number, TSESTree.Token>(),
3232
);
3333
}
3434

packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,12 +1664,10 @@ export default createRule<Options, MessageIds>({
16641664

16651665
return commentMap.set(
16661666
comment,
1667-
commentMap.has(tokenOrCommentBefore)
1668-
? commentMap.get(tokenOrCommentBefore)
1669-
: tokenOrCommentBefore,
1667+
commentMap.get(tokenOrCommentBefore) ?? tokenOrCommentBefore,
16701668
);
16711669
},
1672-
new WeakMap(),
1670+
new WeakMap<TokenOrComment, TSESTree.Token>(),
16731671
);
16741672

16751673
sourceCode.lines.forEach((_, lineIndex) => {
@@ -1700,7 +1698,7 @@ export default createRule<Options, MessageIds>({
17001698
}
17011699

17021700
if (isCommentToken(firstTokenOfLine)) {
1703-
const tokenBefore = precedingTokens.get(firstTokenOfLine);
1701+
const tokenBefore = precedingTokens.get(firstTokenOfLine)!;
17041702
const tokenAfter = tokenBefore
17051703
? sourceCode.getTokenAfter(tokenBefore)!
17061704
: sourceCode.ast.tokens[0];

packages/eslint-plugin/src/rules/indent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* This is due to some really funky type conversions between different node types.
44
* This is done intentionally based on the internal implementation of the base indent rule.
55
*/
6-
/* eslint-disable @typescript-eslint/no-explicit-any */
6+
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment */
77

88
import {
99
TSESTree,
@@ -396,7 +396,7 @@ export default util.createRule<Options, MessageIds>({
396396
computed: false,
397397
method: false,
398398
shorthand: false,
399-
} as any,
399+
},
400400
],
401401

402402
// location data

packages/eslint-plugin/src/rules/naming-convention.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,17 @@ export default util.createRule<Options, MessageIds>({
8181
},
8282
defaultOptions: defaultCamelCaseAllTheThingsConfig,
8383
create(contextWithoutDefaults) {
84-
const context: Context =
84+
const context =
8585
contextWithoutDefaults.options &&
8686
contextWithoutDefaults.options.length > 0
8787
? contextWithoutDefaults
8888
: // only apply the defaults when the user provides no config
89-
Object.setPrototypeOf(
89+
(Object.setPrototypeOf(
9090
{
9191
options: defaultCamelCaseAllTheThingsConfig,
9292
},
9393
contextWithoutDefaults,
94-
);
94+
) as Context);
9595

9696
const validators = parseOptions(context);
9797

packages/eslint-plugin/src/rules/no-extra-parens.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// any is required to work around manipulating the AST in weird ways
2-
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment */
33

44
import {
55
AST_NODE_TYPES,

packages/eslint-plugin/src/rules/unified-signatures.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ export default util.createRule({
471471

472472
const scopes: Scope[] = [];
473473
let currentScope: Scope = {
474-
overloads: new Map(),
474+
overloads: new Map<string, OverloadNode[]>(),
475475
};
476476

477477
function createScope(
@@ -480,7 +480,7 @@ export default util.createRule({
480480
): void {
481481
currentScope && scopes.push(currentScope);
482482
currentScope = {
483-
overloads: new Map(),
483+
overloads: new Map<string, OverloadNode[]>(),
484484
parent,
485485
typeParameters,
486486
};

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { ESLintUtils } from '@typescript-eslint/experimental-utils';
22

33
// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
4-
const version = require('../../package.json').version;
4+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
5+
const version: string = require('../../package.json');
56

67
export const createRule = ESLintUtils.RuleCreator(
78
name =>

packages/experimental-utils/src/ast-utils/eslint-utils/ReferenceTracker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/* eslint-disable @typescript-eslint/no-namespace */
2-
32
import * as eslintUtils from 'eslint-utils';
43
import { TSESTree } from '../../ts-estree';
54
import * as TSESLint from '../../ts-eslint';
65

6+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
77
const ReferenceTrackerREAD: unique symbol = eslintUtils.ReferenceTracker.READ;
88
const ReferenceTrackerCALL: unique symbol = eslintUtils.ReferenceTracker.CALL;
99
const ReferenceTrackerCONSTRUCT: unique symbol =
1010
eslintUtils.ReferenceTracker.CONSTRUCT;
11+
/* eslint-enable @typescript-eslint/no-unsafe-assignment */
1112

1213
interface ReferenceTracker {
1314
/**

packages/experimental-utils/src/eslint-utils/applyDefault.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import { deepMerge, isObjectNotArray } from './deepMerge';
88
* @returns the options with defaults
99
*/
1010
function applyDefault<TUser extends readonly unknown[], TDefault extends TUser>(
11-
defaultOptions: TDefault,
12-
userOptions: TUser | null,
11+
defaultOptions: Readonly<TDefault>,
12+
userOptions: Readonly<TUser> | null,
1313
): TDefault {
1414
// clone defaults
15-
const options: AsMutable<TDefault> = JSON.parse(
15+
const options = JSON.parse(
1616
JSON.stringify(defaultOptions),
17-
);
17+
) as AsMutable<TDefault>;
1818

1919
if (userOptions === null || userOptions === undefined) {
2020
return options;

packages/experimental-utils/src/ts-eslint-scope/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ export * from './Referencer';
99
export * from './Scope';
1010
export * from './ScopeManager';
1111
export * from './Variable';
12+
13+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
1214
export const version: string = ESLintVersion;

packages/experimental-utils/typings/eslint-scope.d.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ declare module 'eslint-scope/lib/scope-manager' {
5757
export = ScopeManager;
5858
}
5959
declare module 'eslint-scope' {
60-
const version: string;
61-
const analyze: unknown;
62-
export { analyze, version };
60+
export const version: string;
61+
export const analyze: unknown;
6362
}

packages/experimental-utils/typings/eslint-utils.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ declare module 'eslint-utils' {
3232
export const isSemicolonToken: unknown;
3333
export const PatternMatcher: unknown;
3434
export const ReferenceTracker: {
35-
READ: never;
36-
CALL: never;
37-
CONSTRUCT: never;
35+
readonly READ: never;
36+
readonly CALL: never;
37+
readonly CONSTRUCT: never;
3838
new (): never;
3939
};
4040
}

packages/parser/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export {
55
} from '@typescript-eslint/typescript-estree';
66

77
// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
8+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
89
export const version: string = require('../package.json').version;

packages/scope-manager/src/scope/GlobalScope.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class GlobalScope extends ScopeBase<
3434
constructor(scopeManager: ScopeManager, block: GlobalScope['block']) {
3535
super(scopeManager, ScopeType.global, null, block, false);
3636
this.implicit = {
37-
set: new Map(),
37+
set: new Map<string, Variable>(),
3838
variables: [],
3939
leftToBeResolved: [],
4040
};

packages/scope-manager/tests/util/serializers/baseSerializer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ function createSerializer<TConstructor extends ConstructorSignature>(
3333
): string {
3434
const id = thing.$id != null ? `$${thing.$id}` : '';
3535
// If `type` is a base class, we should print out the name of the subclass
36-
const constructorName = Object.getPrototypeOf(thing).constructor.name;
36+
// eslint-disable-next-line @typescript-eslint/ban-types
37+
const constructorName = (Object.getPrototypeOf(thing) as Object)
38+
.constructor.name;
3739

3840
if (constructorName === 'ImplicitLibVariable' && thing.name === 'const') {
3941
return 'ImplicitGlobalConstTypeVariable';

packages/typescript-estree/src/ast-converter.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ export function astConverter(
1515
* The TypeScript compiler produced fundamental parse errors when parsing the
1616
* source.
1717
*/
18-
// internal typescript api...
19-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
20-
const parseDiagnostics = (ast as any).parseDiagnostics;
18+
const { parseDiagnostics } = ast;
2119
if (parseDiagnostics.length) {
2220
throw convertError(parseDiagnostics[0]);
2321
}

packages/typescript-estree/src/convert.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// There's lots of funny stuff due to the typing of ts.Node
2-
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-call */
2+
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call */
33
import * as ts from 'typescript';
44
import {
55
canContainDirective,

packages/typescript-estree/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export { clearCaches } from './create-program/createWatchProgram';
88
export { visitorKeys } from '@typescript-eslint/visitor-keys';
99

1010
// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
11+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
1112
export const version: string = require('../package.json').version;

packages/typescript-estree/tests/ast-alignment/parse.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/restrict-plus-operands */
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
22

33
import type babelParser from '@babel/parser';
44
import { ParserPlugin } from '@babel/parser';
55
import { codeFrameColumns } from '@babel/code-frame';
6+
import type { File } from '@babel/types';
67
import * as parser from '../../src/parser';
8+
import { TSESTree } from '@typescript-eslint/types';
79

810
function createError(
911
message: string,
@@ -19,8 +21,8 @@ function createError(
1921
return error;
2022
}
2123

22-
function parseWithBabelParser(text: string, jsx = true): any {
23-
const babel: typeof babelParser = require('@babel/parser');
24+
function parseWithBabelParser(text: string, jsx = true): File {
25+
const babel = require('@babel/parser') as typeof babelParser;
2426
const plugins: ParserPlugin[] = [
2527
'classProperties',
2628
'decorators-legacy',
@@ -96,7 +98,7 @@ export function parse(
9698
);
9799
}
98100
} catch (error) {
99-
const loc = error.loc;
101+
const loc = error.loc as TSESTree.LineAndColumnData | undefined;
100102
if (loc) {
101103
error.codeFrame = codeFrameColumns(
102104
text,
@@ -112,6 +114,7 @@ export function parse(
112114
);
113115
error.message += `\n${error.codeFrame}`;
114116
}
117+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
115118
result.parseError = error;
116119
}
117120

packages/typescript-estree/tests/ast-alignment/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// babel types are something we don't really care about
2-
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/restrict-plus-operands */
2+
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/restrict-plus-operands */
33
import { AST_NODE_TYPES, TSESTree } from '../../src/ts-estree';
44
import { deeplyCopy, omitDeep } from '../../tools/test-utils';
55
import * as BabelTypes from '@babel/types';

packages/typescript-estree/typings/typescript.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ declare module 'typescript' {
44
interface SourceFile {
55
// this is marked as internal to typescript
66
externalModuleIndicator?: Node;
7+
parseDiagnostics: DiagnosticWithLocation[];
78
}
89
}

tests/integration/utils/generate-package-json.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
2+
13
const fs = require('fs');
24
// eslint-disable-next-line import/no-absolute-path
35
const rootPackageJSON = require('/usr/root-package.json');

tools/generate-contributors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ async function* fetchUsers(page = 1): AsyncIterableIterator<Contributor[]> {
4242
const response = await fetch(`${contributorsApiUrl}&page=${page}`, {
4343
method: 'GET',
4444
});
45-
const contributors:
45+
const contributors = (await response.json()) as
4646
| Contributor[]
47-
| { message: string } = await response.json();
47+
| { message: string };
4848

4949
if (!Array.isArray(contributors)) {
5050
throw new Error(contributors.message);

0 commit comments

Comments
 (0)