Skip to content

Commit cc15e4a

Browse files
Add second test case for asserts predicate and convert to testdata files
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
1 parent 1d27d14 commit cc15e4a

File tree

3 files changed

+40
-65
lines changed

3 files changed

+40
-65
lines changed

internal/checker/checker_test.go

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -80,71 +80,6 @@ func TestCheckSrcCompiler(t *testing.T) {
8080
p.CheckSourceFiles(t.Context(), nil)
8181
}
8282

83-
func TestTypePredicateParameterMismatch(t *testing.T) {
84-
t.Parallel()
85-
86-
// This test verifies that the checker doesn't panic when a type predicate
87-
// references a parameter name that doesn't match any actual function parameter.
88-
// The issue was that getTypePredicateArgument would try to access the arguments
89-
// array with a negative index (-1) when the parameter name wasn't found.
90-
content := `type TypeA = { kind: 'a' };
91-
type TypeB = { kind: 'b' };
92-
type UnionType = TypeA | TypeB;
93-
94-
function isTypeA(
95-
_value: UnionType
96-
): value is TypeA { // "value" doesn't match parameter "_value"
97-
return true;
98-
}
99-
100-
function test(input: UnionType): void {
101-
if (isTypeA(input)) {
102-
console.log(input.kind);
103-
}
104-
}`
105-
fs := vfstest.FromMap(map[string]string{
106-
"/test.ts": content,
107-
"/tsconfig.json": `
108-
{
109-
"compilerOptions": {
110-
"target": "es2015",
111-
"module": "commonjs",
112-
"strict": true
113-
},
114-
"files": ["test.ts"]
115-
}
116-
`,
117-
}, false /*useCaseSensitiveFileNames*/)
118-
fs = bundled.WrapFS(fs)
119-
120-
cd := "/"
121-
host := compiler.NewCompilerHost(cd, fs, bundled.LibPath(), nil, nil)
122-
123-
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile("/tsconfig.json", &core.CompilerOptions{}, host, nil)
124-
assert.Equal(t, len(errors), 0, "Expected no errors in parsed command line")
125-
126-
p := compiler.NewProgram(compiler.ProgramOptions{
127-
Config: parsed,
128-
Host: host,
129-
})
130-
131-
// This should not panic - it should report a diagnostic error instead
132-
diags := p.GetSemanticDiagnostics(t.Context(), nil)
133-
134-
// We expect at least one diagnostic error (TS1225: Cannot find parameter 'value')
135-
assert.Assert(t, len(diags) > 0, "Expected at least one diagnostic error")
136-
137-
// Verify the expected error code is present
138-
foundExpectedError := false
139-
for _, diag := range diags {
140-
if diag.Code() == 1225 { // TS1225: Cannot find parameter
141-
foundExpectedError = true
142-
break
143-
}
144-
}
145-
assert.Assert(t, foundExpectedError, "Expected to find error TS1225 (Cannot find parameter)")
146-
}
147-
14883
func BenchmarkNewChecker(b *testing.B) {
14984
repo.SkipIfNoTypeScriptSubmodule(b)
15085
fs := osvfs.FS()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @strict: true
2+
// @noemit: true
3+
4+
// This test verifies that the checker doesn't panic when an assertion predicate
5+
// references a parameter name that doesn't match any actual function parameter.
6+
// This specifically tests the code path in isReachableFlowNodeWorker.
7+
8+
function assertCondition(
9+
_condition: boolean
10+
): asserts condition { // "condition" doesn't match parameter "_condition"
11+
if (!_condition) {
12+
throw new Error('Condition failed');
13+
}
14+
}
15+
16+
function test(): void {
17+
assertCondition(false);
18+
console.log("unreachable");
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @strict: true
2+
// @noemit: true
3+
4+
// This test verifies that the checker doesn't panic when a type predicate
5+
// references a parameter name that doesn't match any actual function parameter.
6+
7+
type TypeA = { kind: 'a' };
8+
type TypeB = { kind: 'b' };
9+
type UnionType = TypeA | TypeB;
10+
11+
function isTypeA(
12+
_value: UnionType
13+
): value is TypeA { // "value" doesn't match parameter "_value"
14+
return true;
15+
}
16+
17+
function test(input: UnionType): void {
18+
if (isTypeA(input)) {
19+
console.log(input.kind);
20+
}
21+
}

0 commit comments

Comments
 (0)