Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions internal/checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,71 @@ func TestCheckSrcCompiler(t *testing.T) {
p.CheckSourceFiles(t.Context(), nil)
}

func TestTypePredicateParameterMismatch(t *testing.T) {
t.Parallel()

// This test verifies that the checker doesn't panic when a type predicate
// references a parameter name that doesn't match any actual function parameter.
// The issue was that getTypePredicateArgument would try to access the arguments
// array with a negative index (-1) when the parameter name wasn't found.
content := `type TypeA = { kind: 'a' };
type TypeB = { kind: 'b' };
type UnionType = TypeA | TypeB;

function isTypeA(
_value: UnionType
): value is TypeA { // "value" doesn't match parameter "_value"
return true;
}

function test(input: UnionType): void {
if (isTypeA(input)) {
console.log(input.kind);
}
}`
fs := vfstest.FromMap(map[string]string{
"/test.ts": content,
"/tsconfig.json": `
{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"strict": true
},
"files": ["test.ts"]
}
`,
}, false /*useCaseSensitiveFileNames*/)
fs = bundled.WrapFS(fs)

cd := "/"
host := compiler.NewCompilerHost(cd, fs, bundled.LibPath(), nil, nil)

parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile("/tsconfig.json", &core.CompilerOptions{}, host, nil)
assert.Equal(t, len(errors), 0, "Expected no errors in parsed command line")

p := compiler.NewProgram(compiler.ProgramOptions{
Config: parsed,
Host: host,
})

// This should not panic - it should report a diagnostic error instead
diags := p.GetSemanticDiagnostics(t.Context(), nil)

// We expect at least one diagnostic error (TS1225: Cannot find parameter 'value')
assert.Assert(t, len(diags) > 0, "Expected at least one diagnostic error")

// Verify the expected error code is present
foundExpectedError := false
for _, diag := range diags {
if diag.Code() == 1225 { // TS1225: Cannot find parameter
foundExpectedError = true
break
}
}
assert.Assert(t, foundExpectedError, "Expected to find error TS1225 (Cannot find parameter)")
}

func BenchmarkNewChecker(b *testing.B) {
repo.SkipIfNoTypeScriptSubmodule(b)
fs := osvfs.FS()
Expand Down
4 changes: 2 additions & 2 deletions internal/checker/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2405,7 +2405,7 @@ func (c *Checker) typeMaybeAssignableTo(source *Type, target *Type) bool {
func (c *Checker) getTypePredicateArgument(predicate *TypePredicate, callExpression *ast.Node) *ast.Node {
if predicate.kind == TypePredicateKindIdentifier || predicate.kind == TypePredicateKindAssertsIdentifier {
arguments := callExpression.Arguments()
if int(predicate.parameterIndex) < len(arguments) {
if predicate.parameterIndex >= 0 && int(predicate.parameterIndex) < len(arguments) {
return arguments[predicate.parameterIndex]
}
} else {
Expand Down Expand Up @@ -2496,7 +2496,7 @@ func (c *Checker) isReachableFlowNodeWorker(f *FlowState, flow *ast.FlowNode, no
case flags&ast.FlowFlagsCall != 0:
if signature := c.getEffectsSignature(flow.Node); signature != nil {
if predicate := c.getTypePredicateOfSignature(signature); predicate != nil && predicate.kind == TypePredicateKindAssertsIdentifier && predicate.t == nil {
if arguments := flow.Node.Arguments(); int(predicate.parameterIndex) < len(arguments) && c.isFalseExpression(arguments[predicate.parameterIndex]) {
if arguments := flow.Node.Arguments(); predicate.parameterIndex >= 0 && int(predicate.parameterIndex) < len(arguments) && c.isFalseExpression(arguments[predicate.parameterIndex]) {
return false
}
}
Expand Down