From 3db653276b97d79f9b0c93b48f4fd0b62d58f694 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 31 Jul 2021 20:20:11 -0400 Subject: [PATCH] chore: enabled no-unsafe-member-access internally (#3483) * chore: enabled no-unsafe-member-access internally * Update packages/scope-manager/tests/util/serializers/TSESTreeNode.ts Co-authored-by: Brad Zacher * fix: missing import * fix: one last lint disable * Update convert.ts * Update .eslintrc.js * Update .eslintrc.js Co-authored-by: Brad Zacher --- .eslintrc.js | 7 +++++-- packages/eslint-plugin/src/util/createRule.ts | 2 +- packages/experimental-utils/src/eslint-utils/RuleTester.ts | 2 +- packages/parser/src/index.ts | 2 +- .../scope-manager/tests/util/serializers/TSESTreeNode.ts | 4 ++-- packages/typescript-estree/src/convert.ts | 2 +- packages/typescript-estree/src/index.ts | 2 +- packages/typescript-estree/src/node-utils.ts | 3 +-- .../typescript-estree/src/semantic-or-syntactic-errors.ts | 2 +- packages/typescript-estree/src/simple-traverse.ts | 1 + packages/typescript-estree/tests/ast-alignment/parse.ts | 2 +- packages/typescript-estree/tests/ast-alignment/utils.ts | 2 +- packages/typescript-estree/typings/typescript.d.ts | 6 +++++- 13 files changed, 22 insertions(+), 15 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index e03f3106c93..df695f85e94 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -65,7 +65,6 @@ module.exports = { ], // TODO - enable these new recommended rules - '@typescript-eslint/no-unsafe-member-access': 'off', '@typescript-eslint/restrict-template-expressions': 'off', // TODO - enable this '@typescript-eslint/naming-convention': 'off', @@ -163,8 +162,10 @@ module.exports = { // all test files { files: [ - 'packages/*/tests/**/*.test.ts', 'packages/*/tests/**/*.spec.ts', + 'packages/*/tests/**/*.test.ts', + 'packages/*/tests/**/spec.ts', + 'packages/*/tests/**/test.ts', 'packages/parser/tests/**/*.ts', ], env: { @@ -172,6 +173,7 @@ module.exports = { }, rules: { '@typescript-eslint/no-unsafe-assignment': 'off', + '@typescript-eslint/no-unsafe-member-access': 'off', '@typescript-eslint/no-unsafe-return': 'off', 'eslint-plugin/no-identical-tests': 'error', 'jest/no-disabled-tests': 'warn', @@ -198,6 +200,7 @@ module.exports = { rules: { '@typescript-eslint/explicit-function-return-type': 'off', '@typescript-eslint/no-unsafe-call': 'off', + '@typescript-eslint/no-unsafe-member-access': 'off', '@typescript-eslint/no-unsafe-return': 'off', '@typescript-eslint/restrict-plus-operands': 'off', }, diff --git a/packages/eslint-plugin/src/util/createRule.ts b/packages/eslint-plugin/src/util/createRule.ts index d56fe6d2a7d..8e69adb9565 100644 --- a/packages/eslint-plugin/src/util/createRule.ts +++ b/packages/eslint-plugin/src/util/createRule.ts @@ -1,7 +1,7 @@ import { ESLintUtils } from '@typescript-eslint/experimental-utils'; // note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder -// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment +// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access const version: string = require('../../package.json').version; export const createRule = ESLintUtils.RuleCreator( diff --git a/packages/experimental-utils/src/eslint-utils/RuleTester.ts b/packages/experimental-utils/src/eslint-utils/RuleTester.ts index a3210162e98..f2af7a290fd 100644 --- a/packages/experimental-utils/src/eslint-utils/RuleTester.ts +++ b/packages/experimental-utils/src/eslint-utils/RuleTester.ts @@ -32,7 +32,7 @@ class RuleTester extends TSESLint.RuleTester { try { // instead of creating a hard dependency, just use a soft require // a bit weird, but if they're using this tooling, it'll be installed - // eslint-disable-next-line @typescript-eslint/no-unsafe-call + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access require(parser).clearCaches(); } catch { // ignored diff --git a/packages/parser/src/index.ts b/packages/parser/src/index.ts index c1bb82bf593..928671a3c5c 100644 --- a/packages/parser/src/index.ts +++ b/packages/parser/src/index.ts @@ -6,5 +6,5 @@ export { } from '@typescript-eslint/typescript-estree'; // note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder -// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment +// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access export const version: string = require('../package.json').version; diff --git a/packages/scope-manager/tests/util/serializers/TSESTreeNode.ts b/packages/scope-manager/tests/util/serializers/TSESTreeNode.ts index 3d57ab11d73..8a75e729fd8 100644 --- a/packages/scope-manager/tests/util/serializers/TSESTreeNode.ts +++ b/packages/scope-manager/tests/util/serializers/TSESTreeNode.ts @@ -1,4 +1,4 @@ -import { AST_NODE_TYPES } from '@typescript-eslint/types'; +import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/types'; import { NewPlugin } from 'pretty-format'; import { createIdGenerator } from '../../../src/ID'; @@ -24,7 +24,7 @@ const serializer: NewPlugin = { // make sure it's not one of the classes from the package Object.getPrototypeOf(val) === Object.prototype && 'type' in val && - val.type in AST_NODE_TYPES + (val as TSESTree.Node).type in AST_NODE_TYPES ); }, serialize(node: Node): string { diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index a49b15a5e53..7f8514d8bc0 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -1,5 +1,5 @@ // There's lots of funny stuff due to the typing of ts.Node -/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return */ +/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access */ import * as ts from 'typescript'; import { canContainDirective, diff --git a/packages/typescript-estree/src/index.ts b/packages/typescript-estree/src/index.ts index b2a0581dc92..5ed06629f17 100644 --- a/packages/typescript-estree/src/index.ts +++ b/packages/typescript-estree/src/index.ts @@ -9,5 +9,5 @@ export { createProgramFromConfigFile as createProgram } from './create-program/u export { visitorKeys } from '@typescript-eslint/visitor-keys'; // note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder -// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment +// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access export const version: string = require('../package.json').version; diff --git a/packages/typescript-estree/src/node-utils.ts b/packages/typescript-estree/src/node-utils.ts index 1969b6b2fb3..8c223ed650a 100644 --- a/packages/typescript-estree/src/node-utils.ts +++ b/packages/typescript-estree/src/node-utils.ts @@ -632,8 +632,7 @@ export function nodeHasTokens(n: ts.Node, ast: ts.SourceFile): boolean { // If we have a token or node that has a non-zero width, it must have tokens. // Note: getWidth() does not take trivia into account. return n.kind === SyntaxKind.EndOfFileToken - ? // eslint-disable-next-line @typescript-eslint/no-explicit-any - !!(n as any).jsDoc + ? !!(n as ts.JSDocContainer).jsDoc : n.getWidth(ast) !== 0; } diff --git a/packages/typescript-estree/src/semantic-or-syntactic-errors.ts b/packages/typescript-estree/src/semantic-or-syntactic-errors.ts index 023d7e3faf8..fab5cd642e0 100644 --- a/packages/typescript-estree/src/semantic-or-syntactic-errors.ts +++ b/packages/typescript-estree/src/semantic-or-syntactic-errors.ts @@ -45,7 +45,7 @@ export function getFirstSemanticOrSyntacticError( * and log a a warning. */ /* istanbul ignore next */ - console.warn(`Warning From TSC: "${e.message}`); // eslint-disable-line no-console + console.warn(`Warning From TSC: "${(e as Error).message}`); // eslint-disable-line no-console /* istanbul ignore next */ return undefined; } diff --git a/packages/typescript-estree/src/simple-traverse.ts b/packages/typescript-estree/src/simple-traverse.ts index 4d4ddaa5f18..f513e54b14f 100644 --- a/packages/typescript-estree/src/simple-traverse.ts +++ b/packages/typescript-estree/src/simple-traverse.ts @@ -3,6 +3,7 @@ import { TSESTree } from './ts-estree'; // eslint-disable-next-line @typescript-eslint/no-explicit-any function isValidNode(x: any): x is TSESTree.Node { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access return x !== null && typeof x === 'object' && typeof x.type === 'string'; } diff --git a/packages/typescript-estree/tests/ast-alignment/parse.ts b/packages/typescript-estree/tests/ast-alignment/parse.ts index 0a643827f3e..7a463d97256 100644 --- a/packages/typescript-estree/tests/ast-alignment/parse.ts +++ b/packages/typescript-estree/tests/ast-alignment/parse.ts @@ -1,4 +1,4 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access */ import type babelParser from '@babel/parser'; import { ParserPlugin } from '@babel/parser'; diff --git a/packages/typescript-estree/tests/ast-alignment/utils.ts b/packages/typescript-estree/tests/ast-alignment/utils.ts index 34b318898c1..3cecac3f09f 100644 --- a/packages/typescript-estree/tests/ast-alignment/utils.ts +++ b/packages/typescript-estree/tests/ast-alignment/utils.ts @@ -1,5 +1,5 @@ // babel types are something we don't really care about -/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/restrict-plus-operands */ +/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-plus-operands */ import { AST_NODE_TYPES, TSESTree } from '../../src/ts-estree'; import { deeplyCopy, omitDeep } from '../../tools/test-utils'; import * as BabelTypes from '@babel/types'; diff --git a/packages/typescript-estree/typings/typescript.d.ts b/packages/typescript-estree/typings/typescript.d.ts index 73268ec9052..a247b280884 100644 --- a/packages/typescript-estree/typings/typescript.d.ts +++ b/packages/typescript-estree/typings/typescript.d.ts @@ -1,9 +1,13 @@ import 'typescript'; +// these additions are marked as internal to typescript declare module 'typescript' { interface SourceFile { - // this is marked as internal to typescript externalModuleIndicator?: Node; parseDiagnostics: DiagnosticWithLocation[]; } + + interface JSDocContainer { + jsDoc?: JSDoc[]; + } }