Skip to content

Commit c068a4f

Browse files
fix(scope-manager): exclude Program from DefinitionBase node types (typescript-eslint#11469)
* fix: exclude Program from DefinitionBase node types * fix eslint * Add NodeWithParent type --------- Co-authored-by: Kirk Waiblinger <53019676+kirkwaiblinger@users.noreply.github.com>
1 parent f9975aa commit c068a4f

File tree

7 files changed

+21
-21
lines changed

7 files changed

+21
-21
lines changed

packages/eslint-plugin/src/rules/no-confusing-void-expression.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
1+
import type {
2+
NodeWithParent,
3+
TSESLint,
4+
TSESTree,
5+
} from '@typescript-eslint/utils';
26

37
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
48
import * as tsutils from 'ts-api-utils';
@@ -300,8 +304,8 @@ export default createRule<Options, MessageId>({
300304
* @param node The void expression node to check.
301305
* @returns Invalid ancestor node if it was found. `null` otherwise.
302306
*/
303-
function findInvalidAncestor(node: TSESTree.Node): InvalidAncestor | null {
304-
const parent = nullThrows(node.parent, NullThrowsReasons.MissingParent);
307+
function findInvalidAncestor(node: NodeWithParent): InvalidAncestor | null {
308+
const parent = node.parent;
305309
if (
306310
parent.type === AST_NODE_TYPES.SequenceExpression &&
307311
node !== parent.expressions[parent.expressions.length - 1]
@@ -365,17 +369,14 @@ export default createRule<Options, MessageId>({
365369
/** Checks whether the return statement is the last statement in a function body. */
366370
function isFinalReturn(node: TSESTree.ReturnStatement): boolean {
367371
// the parent must be a block
368-
const block = nullThrows(node.parent, NullThrowsReasons.MissingParent);
372+
const block = node.parent;
369373
if (block.type !== AST_NODE_TYPES.BlockStatement) {
370374
// e.g. `if (cond) return;` (not in a block)
371375
return false;
372376
}
373377

374378
// the block's parent must be a function
375-
const blockParent = nullThrows(
376-
block.parent,
377-
NullThrowsReasons.MissingParent,
378-
);
379+
const blockParent = block.parent;
379380
if (
380381
![
381382
AST_NODE_TYPES.ArrowFunctionExpression,

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ function isMergableExported(variable: ScopeVariable): boolean {
438438

439439
if (
440440
(MERGABLE_TYPES.has(def.node.type) &&
441-
def.node.parent?.type === AST_NODE_TYPES.ExportNamedDeclaration) ||
442-
def.node.parent?.type === AST_NODE_TYPES.ExportDefaultDeclaration
441+
def.node.parent.type === AST_NODE_TYPES.ExportNamedDeclaration) ||
442+
def.node.parent.type === AST_NODE_TYPES.ExportDefaultDeclaration
443443
) {
444444
return true;
445445
}
@@ -458,14 +458,12 @@ function isExported(variable: ScopeVariable): boolean {
458458
let node = definition.node;
459459

460460
if (node.type === AST_NODE_TYPES.VariableDeclarator) {
461-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
462-
node = node.parent!;
461+
node = node.parent;
463462
} else if (definition.type === TSESLint.Scope.DefinitionType.Parameter) {
464463
return false;
465464
}
466465

467-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
468-
return node.parent!.type.startsWith('Export');
466+
return node.parent.type.startsWith('Export');
469467
});
470468
}
471469

packages/scope-manager/src/definition/DefinitionBase.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { TSESTree } from '@typescript-eslint/types';
1+
import type { NodeWithParent, TSESTree } from '@typescript-eslint/types';
22

33
import type { DefinitionType } from './DefinitionType';
44

@@ -8,7 +8,7 @@ const generator = createIdGenerator();
88

99
export abstract class DefinitionBase<
1010
Type extends DefinitionType,
11-
Node extends TSESTree.Node,
11+
Node extends NodeWithParent,
1212
Parent extends TSESTree.Node | null,
1313
Name extends TSESTree.Node,
1414
> {

packages/scope-manager/src/definition/ImplicitGlobalVariableDefinition.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type { TSESTree } from '@typescript-eslint/types';
1+
import type { NodeWithParent, TSESTree } from '@typescript-eslint/types';
22

33
import { DefinitionBase } from './DefinitionBase';
44
import { DefinitionType } from './DefinitionType';
55

66
export class ImplicitGlobalVariableDefinition extends DefinitionBase<
77
DefinitionType.ImplicitGlobalVariable,
8-
TSESTree.Node,
8+
NodeWithParent,
99
null,
1010
TSESTree.BindingName
1111
> {

packages/scope-manager/src/referencer/Reference.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { TSESTree } from '@typescript-eslint/types';
1+
import type { NodeWithParent, TSESTree } from '@typescript-eslint/types';
22

33
import type { Scope } from '../scope';
44
import type { Variable } from '../variable';
@@ -12,7 +12,7 @@ export enum ReferenceFlag {
1212
}
1313

1414
export interface ReferenceImplicitGlobal {
15-
node: TSESTree.Node;
15+
node: NodeWithParent;
1616
pattern: TSESTree.BindingName;
1717
ref?: Reference;
1818
}

packages/types/src/ts-estree.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,4 @@ declare module './generated/ast-spec' {
251251
}
252252

253253
export * as TSESTree from './generated/ast-spec';
254+
export type NodeWithParent = Exclude<TSESTree.Node, TSESTree.Program>;

packages/utils/src/ts-estree.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// for convenience's sake - export the types directly from here so consumers
22
// don't need to reference/install both packages in their code
3-
43
export {
54
AST_NODE_TYPES,
65
AST_TOKEN_TYPES,
76
TSESTree,
87
} from '@typescript-eslint/types';
8+
export type { NodeWithParent } from '@typescript-eslint/types';
99

1010
export type {
1111
ParserServices,

0 commit comments

Comments
 (0)