Skip to content

Commit c5039e6

Browse files
authored
Fix go to implementation crash on invalid CJS shorthand exports (#3603)
1 parent 515d036 commit c5039e6

7 files changed

Lines changed: 114 additions & 0 deletions
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/testutil"
8+
)
9+
10+
func TestGoToImplementationReachingNonExistentExport1(t *testing.T) {
11+
t.Parallel()
12+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
13+
const content = `
14+
// @Filename: /github.ts
15+
export { transformRecordedData };
16+
17+
// @Filename: /gitGateway.ts
18+
import { transformRecordedData as transformGitHub } from './github';
19+
20+
const methods = { github: {
21+
transformData: /*impl*/transformGitHub,
22+
}};
23+
`
24+
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
25+
defer done()
26+
f.VerifyBaselineGoToImplementation(t, "impl")
27+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/testutil"
8+
)
9+
10+
func TestGoToImplementationReachingNonExistentExport2(t *testing.T) {
11+
t.Parallel()
12+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
13+
const content = `
14+
// @allowJs: true
15+
// @checkJs: true
16+
17+
// @Filename: /github.js
18+
module.exports = { transformRecordedData };
19+
20+
// @Filename: /gitGateway.js
21+
const { transformRecordedData: transformGitHub } = require('./github');
22+
23+
const methods = { github: {
24+
transformData: /*impl*/transformGitHub,
25+
}};
26+
`
27+
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
28+
defer done()
29+
f.VerifyBaselineGoToImplementation(t, "impl")
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/testutil"
8+
)
9+
10+
func TestGoToImplementationReachingNonExistentExport3(t *testing.T) {
11+
t.Parallel()
12+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
13+
const content = `
14+
// @allowJs: true
15+
// @checkJs: true
16+
17+
// @Filename: /github.js
18+
export { transformRecordedData };
19+
20+
// @Filename: /gitGateway.js
21+
import { transformRecordedData as transformGitHub } from './github';
22+
23+
const methods = { github: {
24+
transformData: /*impl*/transformGitHub,
25+
}};
26+
`
27+
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
28+
defer done()
29+
f.VerifyBaselineGoToImplementation(t, "impl")
30+
}

internal/ls/importTracker.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,9 @@ func getImportOrExportSymbol(node *ast.Node, symbol *ast.Symbol, checker *checke
578578
}
579579
// Search on the local symbol in the exporting module, not the exported symbol.
580580
importedSymbol = skipExportSpecifierSymbol(importedSymbol, checker)
581+
if importedSymbol == nil {
582+
return nil
583+
}
581584
// Similarly, skip past the symbol for 'export ='
582585
if importedSymbol.Name == "export=" {
583586
importedSymbol = getExportEqualsLocalSymbol(importedSymbol, checker)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// === goToImplementation ===
2+
// === /gitGateway.ts ===
3+
// import { transformRecordedData as transformGitHub } from './github';
4+
//
5+
// const methods = { github: {
6+
// transformData: /*GOTO IMPL*/transformGitHub,
7+
// }};
8+
//
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// === goToImplementation ===
2+
// === /gitGateway.js ===
3+
// const { transformRecordedData: transformGitHub } = require('./github');
4+
//
5+
// const methods = { github: {
6+
// transformData: /*GOTO IMPL*/transformGitHub,
7+
// }};
8+
//
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// === goToImplementation ===
2+
// === /gitGateway.js ===
3+
// import { transformRecordedData as transformGitHub } from './github';
4+
//
5+
// const methods = { github: {
6+
// transformData: /*GOTO IMPL*/transformGitHub,
7+
// }};
8+
//

0 commit comments

Comments
 (0)