Skip to content

Commit 382a149

Browse files
committed
Add NodeWithParent type
1 parent 308ac8b commit 382a149

File tree

6 files changed

+14
-20
lines changed

6 files changed

+14
-20
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/scope-manager/src/definition/DefinitionBase.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
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

55
import { createIdGenerator } from '../ID';
66

77
const generator = createIdGenerator();
8-
/**
9-
* Helper type to exclude Program from valid definition nodes
10-
* Program is the root node and doesn't have a parent property
11-
*/
12-
export type NodeWithParent = Exclude<TSESTree.Node, TSESTree.Program>;
138

149
export abstract class DefinitionBase<
1510
Type extends DefinitionType,

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import type { TSESTree } from '@typescript-eslint/types';
2-
3-
import type { NodeWithParent } from './DefinitionBase';
1+
import type { NodeWithParent, TSESTree } from '@typescript-eslint/types';
42

53
import { DefinitionBase } from './DefinitionBase';
64
import { DefinitionType } from './DefinitionType';

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

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

3-
import type { NodeWithParent } from '../definition/DefinitionBase';
43
import type { Scope } from '../scope';
54
import type { Variable } from '../variable';
65

packages/types/src/ts-estree.ts

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

256256
export * as TSESTree from './generated/ast-spec';
257+
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)