Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
27 changes: 27 additions & 0 deletions internal/fourslash/tests/importAllowRenameOfImportPath2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package fourslash_test

import (
"testing"

"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/fourslash"
"github.com/microsoft/typescript-go/internal/ls/lsutil"
"github.com/microsoft/typescript-go/internal/testutil"
)

func TestAllowRenameOfImportPath2(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @Filename: /src/example.ts
import brushPackageJson from './visx-brush/[|package|].json';
// @Filename: /src/visx-brush/package.json
{ "name": "brush" }
`

f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
prefs := &lsutil.UserPreferences{
AllowRenameOfImportPath: core.TSTrue,
}
f.VerifyBaselineRename(t, prefs, f.Ranges()[0])
}
27 changes: 27 additions & 0 deletions internal/fourslash/tests/importAllowRenameOfImportPath3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package fourslash_test

import (
"testing"

"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/fourslash"
"github.com/microsoft/typescript-go/internal/ls/lsutil"
"github.com/microsoft/typescript-go/internal/testutil"
)

func TestAllowRenameOfImportPath3(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @Filename: /src/example.ts
import stuff from './[|stuff|].cts';
// @Filename: /src/stuff.cts
export = { name: "stuff" };
`

f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
prefs := &lsutil.UserPreferences{
AllowRenameOfImportPath: core.TSTrue,
}
f.VerifyBaselineRename(t, prefs, f.Ranges()[0])
}
29 changes: 29 additions & 0 deletions internal/fourslash/tests/importAllowRenameOfImportPath4_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package fourslash_test

import (
"testing"

"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/fourslash"
"github.com/microsoft/typescript-go/internal/ls/lsutil"
"github.com/microsoft/typescript-go/internal/testutil"
)

func TestAllowRenameOfImportPath4(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @Filename: /src/example.ts
import styles from './[|styles|].css';
// @Filename: /src/styles.css
body { color: red; }
// @Filename: /src/globals.d.ts
declare module "*.css";
`

f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
prefs := &lsutil.UserPreferences{
AllowRenameOfImportPath: core.TSTrue,
}
f.VerifyBaselineRename(t, prefs, f.Ranges()[0])
}
27 changes: 27 additions & 0 deletions internal/fourslash/tests/importAllowRenameOfImportPath5_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package fourslash_test

import (
"testing"

"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/fourslash"
"github.com/microsoft/typescript-go/internal/ls/lsutil"
"github.com/microsoft/typescript-go/internal/testutil"
)

func TestAllowRenameOfImportPath5(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @Filename: /src/example.ts
import styles from './[|styles|].css';
// @Filename: /src/styles.css
body { color: red; }
`

f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
prefs := &lsutil.UserPreferences{
AllowRenameOfImportPath: core.TSTrue,
}
f.VerifyBaselineRename(t, prefs, f.Ranges()[0])
}
11 changes: 7 additions & 4 deletions internal/ls/findallreferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ func (l *LanguageService) getReferencedSymbolsForNode(ctx context.Context, posit
}

if moduleSymbol := checker.GetMergedSymbol(resolvedRef.file.Symbol); moduleSymbol != nil {
return l.getReferencedSymbolsForModule(ctx, program, moduleSymbol /*excludeImportTypeOfExportEquals*/, false, sourceFiles, sourceFilesSet)
return l.getReferencedSymbolsForModule(ctx, program, moduleSymbol /*excludeImportTypeOfExportEquals*/, false, sourceFiles, sourceFilesSet, options)
}

// !!! not implemented
Expand Down Expand Up @@ -913,7 +913,7 @@ func (l *LanguageService) getReferencedSymbolsForNode(ctx context.Context, posit
}

if symbol.Name == ast.InternalSymbolNameExportEquals {
return l.getReferencedSymbolsForModule(ctx, program, symbol.Parent, false /*excludeImportTypeOfExportEquals*/, sourceFiles, sourceFilesSet)
return l.getReferencedSymbolsForModule(ctx, program, symbol.Parent, false /*excludeImportTypeOfExportEquals*/, sourceFiles, sourceFilesSet, options)
}

moduleReferences := l.getReferencedSymbolsForModuleIfDeclaredBySourceFile(ctx, symbol, program, sourceFiles, checker, options, sourceFilesSet) // !!! cancellationToken
Expand Down Expand Up @@ -987,7 +987,7 @@ func (l *LanguageService) getReferencedSymbolsForModuleIfDeclaredBySourceFile(ct
}
exportEquals := symbol.Exports[ast.InternalSymbolNameExportEquals]
// If exportEquals != nil, we're about to add references to `import("mod")` anyway, so don't double-count them.
moduleReferences := l.getReferencedSymbolsForModule(ctx, program, symbol, exportEquals != nil, sourceFiles, sourceFilesSet)
moduleReferences := l.getReferencedSymbolsForModule(ctx, program, symbol, exportEquals != nil, sourceFiles, sourceFilesSet, options)
if exportEquals == nil || exportEquals.Flags&ast.SymbolFlagsAlias == 0 || !sourceFilesSet.Has(moduleSourceFileName) {
return moduleReferences
}
Expand Down Expand Up @@ -1308,7 +1308,7 @@ func getMergedAliasedSymbolOfNamespaceExportDeclaration(node *ast.Node, symbol *
return nil
}

func (l *LanguageService) getReferencedSymbolsForModule(ctx context.Context, program *compiler.Program, symbol *ast.Symbol, excludeImportTypeOfExportEquals bool, sourceFiles []*ast.SourceFile, sourceFilesSet *collections.Set[string]) []*SymbolAndEntries {
func (l *LanguageService) getReferencedSymbolsForModule(ctx context.Context, program *compiler.Program, symbol *ast.Symbol, excludeImportTypeOfExportEquals bool, sourceFiles []*ast.SourceFile, sourceFilesSet *collections.Set[string], options refOptions) []*SymbolAndEntries {
debug.Assert(symbol.ValueDeclaration != nil)

checker, done := program.GetTypeChecker(ctx)
Expand Down Expand Up @@ -1380,6 +1380,9 @@ func (l *LanguageService) getReferencedSymbolsForModule(ctx context.Context, pro
exported := symbol.Exports[ast.InternalSymbolNameExportEquals]
if exported != nil && len(exported.Declarations) > 0 {
for _, decl := range exported.Declarations {
if options.use == referenceUseRename && ast.IsSourceFile(decl) {
continue
}
Comment on lines +1383 to +1385
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested the same patch in Strada and renaming the from "./x/package.json" resulted in the file itself being renamed too. That's definitely a desirable outcome but I'm not exactly sure what's the codepath leading to that filename rename - given it works... I wasn't exactly inclined to dig deeper.

The alternative fix would be to handle this in the loop here:

for _, entry := range entries {
uri := l.getFileNameOfEntry(entry)
if l.UserPreferences().AllowRenameOfImportPath != core.TSTrue && entry.node != nil && ast.IsStringLiteralLike(entry.node) && ast.TryGetImportFromModuleSpecifier(entry.node) != nil {
continue
}
textEdit := &lsproto.TextEdit{
Range: l.getRangeOfEntry(entry),
NewText: l.getTextForRename(data.OriginalNode, entry, params.NewName, ch, quotePreference),
}
changes[uri] = append(changes[uri], textEdit)
}

The crash happens in getTextForRename

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to think a bit more about all this, but I think this fix is ok. We're missing support for file rename in Corsa right now (Strada's getEditsForFileRename), planning on porting that next. But I think the right behavior would be to return just a RenameFile in this case and then compute the edits in response to a willRenameFiles request from the client. So the fix looks right, but we don't have a good way right now to properly test this. I think I will leave this PR open and revisit once file rename is ported, if that's ok.

sourceFile := ast.GetSourceFileOfNode(decl)
if sourceFilesSet.Has(sourceFile.FileName()) {
var node *ast.Node
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// === findRenameLocations ===
// === /src/example.ts ===
// import brushPackageJson from '[|./visx-brush//*RENAME*/package.jsonRENAME|]';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// === findRenameLocations ===
// === /src/example.ts ===
// import stuff from '[|.//*RENAME*/stuff.ctsRENAME|]';

// === /src/stuff.cts ===
// [|exportRENAME|] = { name: "stuff" };
//
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// === findRenameLocations ===
// === /src/example.ts ===
// import styles from './/*RENAME*/styles.css';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// === findRenameLocations ===
// === /src/example.ts ===
// import styles from './/*RENAME*/styles.css';
Loading