Skip to content

Commit 51c8df6

Browse files
committed
make as clause interrupt type but not data operators
1 parent c581ad1 commit 51c8df6

4 files changed

Lines changed: 23 additions & 2 deletions

File tree

src/cognitive-complexity/cognitive-complexity.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
ChainableBinaryOperator,
2020
ChainableBinaryTypeOperator,
2121
pausesASequenceOfBinaryOperators,
22+
pausesASequenceOfBinaryTypeOperators,
2223
} from "./node-inspection";
2324
import { Scope } from "./Scope";
2425

@@ -240,6 +241,7 @@ function nodeCost(
240241
const opSequenceInProgress = mutCtx.precedingOperator;
241242
const typeOpSequenceInProgress = mutCtx.precedingTypeOperator;
242243
const pauseOpSequence = pausesASequenceOfBinaryOperators(node);
244+
const pauseTypeOpSequence = pausesASequenceOfBinaryTypeOperators(node);
243245

244246
// Check if the node ends any ongoing sequence of binary operators
245247
if (breaksASequenceOfBinaryOperators(node)) {
@@ -262,6 +264,7 @@ function nodeCost(
262264
mutCtx.precedingTypeOperator = node.kind;
263265
} else if (pauseOpSequence) {
264266
mutCtx.precedingOperator = undefined;
267+
} else if (pauseTypeOpSequence) {
265268
mutCtx.precedingTypeOperator = undefined;
266269
}
267270

@@ -290,6 +293,7 @@ function nodeCost(
290293
// continue a paused sequence
291294
if (pauseOpSequence) {
292295
mutCtx.precedingOperator = opSequenceInProgress;
296+
} else if (pauseTypeOpSequence) {
293297
mutCtx.precedingTypeOperator = typeOpSequenceInProgress;
294298
}
295299

src/cognitive-complexity/node-inspection.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,15 @@ export function breaksASequenceOfBinaryOperators(node: ts.Node) {
160160
export function pausesASequenceOfBinaryOperators(node: ts.Node) {
161161
return ts.isCallLikeExpression(node)
162162
|| ts.isPrefixUnaryExpression(node)
163-
|| ts.isParenthesizedExpression(node)
163+
}
164+
165+
/**
166+
* @see {pausesASequenceOfBinaryOperators} but for type operators
167+
*/
168+
export function pausesASequenceOfBinaryTypeOperators(node: ts.Node) {
169+
return ts.isParenthesizedExpression(node)
164170
|| ts.isTypeReferenceNode(node)
171+
|| ts.isAsExpression(node)
165172
}
166173

167174
export function passThroughNameBeingAssigned(node: ts.Node): boolean {

test/cases/18-type-operators.expected.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"18-type-operators.ts": {
3-
"score": 42,
3+
"score": 45,
44
"inner": [
55
{
66
"name": "A",
@@ -88,6 +88,12 @@
8888
"score": 3,
8989
"line": 32,
9090
"column": 1
91+
},
92+
{
93+
"name": "asBreaksTypeOpSequence_butNotDataOpSequence",
94+
"score": 3,
95+
"line": 34,
96+
"column": 1
9197
}
9298
]
9399
}

test/cases/18-type-operators.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ function breaksInSequencesOfTypeOperators<T extends true | true = true | true>(p
3030
type InParentheses = NonNullable<true | true>
3131

3232
type PauseSequenceInParentheses = NonNullable<true | true> | number | NonNullable<true | true> | number
33+
34+
function asBreaksTypeOpSequence_butNotDataOpSequence() {
35+
1 && (1 && 1 as 1 | 1 as 1 | 1) && 1 // 1 sequence of bitwise ors, 2 sequences of unions
36+
}

0 commit comments

Comments
 (0)