Skip to content
Merged
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
24 changes: 19 additions & 5 deletions internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -2787,20 +2787,34 @@ func GetPragmaArgument(pragma *Pragma, name string) string {
// The variable must not be exported and must not have a type annotation, even a jsdoc one.
// The initializer must be a call to `require` with a string literal or a string literal-like argument.
func IsVariableDeclarationInitializedToRequire(node *Node) bool {
if !IsInJSFile(node) {
return false
}
if node.Kind == KindBindingElement {
node = node.Parent.Parent
}
return isVariableDeclarationInitializedWithRequireHelper(node, false /*allowAccessedRequire*/)
}

func IsVariableDeclarationInitializedToBareOrAccessedRequire(node *Node) bool {
return isVariableDeclarationInitializedWithRequireHelper(node, true /*allowAccessedRequire*/)
}

func isVariableDeclarationInitializedWithRequireHelper(node *Node, allowAccessedRequire bool) bool {
if !IsInJSFile(node) {
return false
}
if node.Kind != KindVariableDeclaration {
return false
}
initializer := node.Initializer()
if initializer == nil {
return false
}
if allowAccessedRequire {
initializer = GetLeftmostAccessExpression(initializer)
}

return node.Parent.Parent.ModifierFlags()&ModifierFlagsExport == 0 &&
node.Initializer() != nil &&
node.Type() == nil &&
IsRequireCall(node.Initializer(), true /*requireStringLiteralLikeArgument*/)
IsRequireCall(initializer, true /*requireStringLiteralLikeArgument*/)
}

func IsModuleExportsAccessExpression(node *Node) bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package fourslash_test

import (
"testing"

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

func TestDocumentHighlightNestedRequireDestructureNoCrash1(t *testing.T) {
fourslash.SkipIfFailing(t)
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @allowJs: true
// @Filename: /bar.js
const { a: { b } } = require('./foo');
/**/b;`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
f.VerifyBaselineDocumentHighlights(t, nil /*preferences*/, "")
}
2 changes: 1 addition & 1 deletion internal/ls/findallreferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -1885,7 +1885,7 @@ func (state *refState) getReferencesAtLocation(sourceFile *ast.SourceFile, posit

// Use the parent symbol if the location is commonjs require syntax on javascript files only.
if ast.IsInJSFile(referenceLocation) && referenceLocation.Parent.Kind == ast.KindBindingElement &&
ast.IsVariableDeclarationInitializedToRequire(referenceLocation.Parent.Parent.Parent) {
ast.IsVariableDeclarationInitializedToBareOrAccessedRequire(referenceLocation.Parent.Parent.Parent) {
referenceSymbol = referenceLocation.Parent.Symbol()
// The parent will not have a symbol if it's an ObjectBindingPattern (when destructuring is used). In
// this case, just skip it, since the bound identifiers are not an alias of the import.
Expand Down
2 changes: 1 addition & 1 deletion internal/ls/importTracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ func isNodeImport(node *ast.Node) bool {
debug.Assert(parent.Name() == node)
return true
case ast.KindBindingElement:
return ast.IsInJSFile(node) && ast.IsVariableDeclarationInitializedToRequire(parent.Parent.Parent)
return ast.IsInJSFile(node) && ast.IsVariableDeclarationInitializedToBareOrAccessedRequire(parent.Parent.Parent)
}
return false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// === documentHighlights ===
// === /bar.js ===
// const { a: { [|b|] } } = require('./foo');
// /*HIGHLIGHTS*/[|b|];