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
6 changes: 5 additions & 1 deletion internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2353,6 +2353,9 @@ func IsArrayLiteralOrObjectLiteralDestructuringPattern(node *Node) bool {

func accessKind(node *Node) AccessKind {
parent := node.Parent
if parent == nil {
return AccessKindRead
}
switch parent.Kind {
case KindParenthesizedExpression:
return accessKind(parent)
Expand Down Expand Up @@ -2404,8 +2407,9 @@ func accessKind(node *Node) AccessKind {
return AccessKindWrite
}
return AccessKindRead
default:
return AccessKindRead
}
return AccessKindRead
}

func reverseAccessKind(a AccessKind) AccessKind {
Expand Down
4 changes: 2 additions & 2 deletions internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ func IsClassElement(node *Node) bool {
return false
}

func isMethodOrAccessor(node *Node) bool {
func IsMethodOrAccessor(node *Node) bool {
switch node.Kind {
case KindMethodDeclaration, KindGetAccessor, KindSetAccessor:
return true
Expand All @@ -580,7 +580,7 @@ func isMethodOrAccessor(node *Node) bool {
}

func IsPrivateIdentifierClassElementDeclaration(node *Node) bool {
return (IsPropertyDeclaration(node) || isMethodOrAccessor(node)) && IsPrivateIdentifier(node.Name())
return (IsPropertyDeclaration(node) || IsMethodOrAccessor(node)) && IsPrivateIdentifier(node.Name())
}

func IsObjectLiteralOrClassExpressionMethodOrAccessor(node *Node) bool {
Expand Down
18 changes: 12 additions & 6 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19101,11 +19101,9 @@ func (c *Checker) getSignaturesOfSymbol(symbol *ast.Symbol) []*Signature {
}
// If this is a function or method declaration, get the signature from the @type tag for the sake of optional parameters.
// Exclude contextually-typed kinds because we already apply the @type tag to the context, plus applying it here to the initializer would suppress checks that the two are compatible.
if ast.IsFunctionExpressionOrArrowFunction(decl) || ast.IsObjectLiteralMethod(decl) {
if sig := c.getSignatureOfFullSignatureType(decl); sig != nil {
result = append(result, sig)
continue
}
if sig := c.getSignatureOfFullSignatureType(decl); sig != nil {
result = append(result, sig)
continue
}
result = append(result, c.getSignatureFromDeclaration(decl))
}
Expand All @@ -19123,6 +19121,14 @@ func (c *Checker) getSignatureFromDeclaration(declaration *ast.Node) *Signature
minArgumentCount := 0
hasThisParameter := false
iife := ast.GetImmediatelyInvokedFunctionExpression(declaration)
isUntypedSignatureInJSFile := iife == nil &&
ast.IsInJSFile(declaration) &&
(ast.IsFunctionExpression(declaration) || ast.IsArrowFunction(declaration) || ast.IsMethodOrAccessor(declaration) || ast.IsFunctionDeclaration(declaration) || ast.IsConstructorDeclaration(declaration)) &&
core.Every(declaration.Parameters(), func(param *ast.Node) bool { return param.Type() == nil }) &&
c.getContextualType(declaration, ContextFlagsSignature) == nil
if isUntypedSignatureInJSFile {
flags |= SignatureFlagsIsUntypedSignatureInJSFile
}
for i, param := range declaration.Parameters() {
paramSymbol := param.Symbol()
typeNode := param.Type()
Expand Down Expand Up @@ -19343,7 +19349,7 @@ func (c *Checker) getReturnTypeFromAnnotation(declaration *ast.Node) *Type {
}

func (c *Checker) getSignatureOfFullSignatureType(node *ast.Node) *Signature {
if ast.IsInJSFile(node) && ast.IsFunctionLike(node) && node.FunctionLikeData().FullSignature != nil {
if ast.IsInJSFile(node) && (ast.IsFunctionDeclaration(node) || ast.IsMethodDeclaration(node) || ast.IsFunctionExpressionOrArrowFunction(node)) && node.FunctionLikeData().FullSignature != nil {
return c.getSingleCallSignature(c.getTypeFromTypeNode(node.FunctionLikeData().FullSignature))
}
return nil
Expand Down
21 changes: 14 additions & 7 deletions internal/checker/emitresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,13 +458,20 @@ func (r *emitResolver) IsImplementationOfOverload(node *ast.SignatureDeclaration
// function foo(a: any) { // This is implementation of the overloads
// return a;
// }
return len(signaturesOfSymbol) > 1 ||
// If there is single signature for the symbol, it is overload if that signature isn't coming from the node
// e.g.: function foo(a: string): string;
// function foo(a: any) { // This is implementation of the overloads
// return a;
// }
(len(signaturesOfSymbol) == 1 && signaturesOfSymbol[0].declaration != node)
if len(signaturesOfSymbol) > 1 {
return true
}
// If there is single signature for the symbol, it is overload if that signature isn't coming from the node
// e.g.: function foo(a: string): string;
// function foo(a: any) { // This is implementation of the overloads
// return a;
// }
if len(signaturesOfSymbol) == 1 {
declaration := signaturesOfSymbol[0].declaration
if declaration != node && declaration.Flags&ast.NodeFlagsJSDoc == 0 {
return true
}
}
}
return false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fourslash_test

import (
"testing"

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

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

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @Filename: /a.ts
/// <reference path="[|./b.ts|]" />

const x = 1;

// @filename: b.ts
export type Foo = number;
`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineDocumentHighlights(t, nil /*preferences*/, f.Ranges()[0])
}
9 changes: 4 additions & 5 deletions internal/ls/documenthighlights.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,13 @@ func (l *LanguageService) getSemanticDocumentHighlights(ctx context.Context, pos
if referenceEntries == nil {
return nil
}

var highlights []*lsproto.DocumentHighlight
for _, entry := range referenceEntries {
for _, ref := range entry.references {
if ref.node != nil {
fileName, highlight := l.toDocumentHighlight(ref)
if fileName == sourceFile.FileName() {
highlights = append(highlights, highlight)
}
fileName, highlight := l.toDocumentHighlight(ref)
if fileName == sourceFile.FileName() {
highlights = append(highlights, highlight)
}
}
}
Expand Down
17 changes: 9 additions & 8 deletions internal/ls/findallreferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ func (l *LanguageService) getReferencedSymbolsForNode(ctx context.Context, posit
}

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

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

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

moduleReferences := l.getReferencedSymbolsForModuleIfDeclaredBySourceFile(ctx, symbol, program, sourceFiles, checker, options, sourceFilesSet) // !!! cancellationToken
Expand All @@ -700,7 +700,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 := getReferencedSymbolsForModule(ctx, program, symbol, exportEquals != nil, sourceFiles, sourceFilesSet)
moduleReferences := l.getReferencedSymbolsForModule(ctx, program, symbol, exportEquals != nil, sourceFiles, sourceFilesSet)
if exportEquals == nil || !sourceFilesSet.Has(moduleSourceFileName) {
return moduleReferences
}
Expand Down Expand Up @@ -1021,7 +1021,7 @@ func getMergedAliasedSymbolOfNamespaceExportDeclaration(node *ast.Node, symbol *
return nil
}

func 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]) []*SymbolAndEntries {
debug.Assert(symbol.ValueDeclaration != nil)

checker, done := program.GetTypeChecker(ctx)
Expand Down Expand Up @@ -1062,10 +1062,11 @@ func getReferencedSymbolsForModule(ctx context.Context, program *compiler.Progra
}
return newNodeEntry(rangeNode)
case ModuleReferenceKindReference:
// <reference path> or <reference types>
// We can't easily create a proper range entry here without access to LanguageService,
// but we can create a node-based entry pointing to the source file which will be resolved later
return newNodeEntry(reference.referencingFile.AsNode())
return &referenceEntry{
kind: entryKindRange,
fileName: reference.referencingFile.FileName(),
textRange: l.createLspRangeFromBounds(reference.ref.Pos(), reference.ref.End(), reference.referencingFile),
}
}
return nil
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @param {2} a
*/
export function conflictingParam(a) { return true }
>conflictingParam : (a: 2) => true
>conflictingParam : (a: 1) => true
>a : 2
>true : true

Expand All @@ -15,7 +15,7 @@ export function conflictingParam(a) { return true }
* @return {false}
*/
export function conflictingReturn(b) { return false }
>conflictingReturn : (b: 3) => false
>conflictingReturn : (b: 3) => true
>b : 3
>false : false

Expand All @@ -26,7 +26,7 @@ export function conflictingReturn(b) { return false }
* @return {false}
*/
export function conflictingBoth(d) { return false }
>conflictingBoth : (d: 5) => false
>conflictingBoth : (c: 4) => true
>d : 5
>false : false

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// === documentHighlights ===
// === /a.ts ===
// /// <reference path="/*HIGHLIGHTS*/[|./b.ts|]" />
//
// const x = 1;
//
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
main.js(3,9): error TS2554: Expected 0 arguments, but got 3.
main.js(5,1): error TS2554: Expected 2 arguments, but got 0.
main.js(6,16): error TS2554: Expected 2 arguments, but got 3.
main.js(6,16): error TS2554: Expected 0-2 arguments, but got 3.


==== main.js (3 errors) ====
==== main.js (2 errors) ====
function allRest() { arguments; }
allRest();
allRest(1, 2, 3);
~~~~~~~
!!! error TS2554: Expected 0 arguments, but got 3.
function someRest(x, y) { arguments; }
someRest(); // x and y are still optional because they are in a JS file
~~~~~~~~
!!! error TS2554: Expected 2 arguments, but got 0.
!!! related TS6210 main.js:4:19: An argument for 'x' was not provided.
someRest(1, 2, 3);
~
!!! error TS2554: Expected 2 arguments, but got 3.
!!! error TS2554: Expected 0-2 arguments, but got 3.

/**
* @param {number} x - a thing
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
a.js(9,7): error TS2554: Expected 1 arguments, but got 3.
a.js(9,7): error TS2554: Expected 0-1 arguments, but got 3.


==== a.js (1 errors) ====
Expand All @@ -12,5 +12,5 @@ a.js(9,7): error TS2554: Expected 1 arguments, but got 3.

f2(1, 2, 3);
~~~~
!!! error TS2554: Expected 1 arguments, but got 3.
!!! error TS2554: Expected 0-1 arguments, but got 3.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
a.js(5,6): error TS2554: Expected 1 arguments, but got 3.
a.js(5,6): error TS2554: Expected 0-1 arguments, but got 3.


==== a.js (1 errors) ====
Expand All @@ -8,5 +8,5 @@ a.js(5,6): error TS2554: Expected 1 arguments, but got 3.

f(1, 2, 3);
~~~~
!!! error TS2554: Expected 1 arguments, but got 3.
!!! error TS2554: Expected 0-1 arguments, but got 3.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
index.js(1,25): error TS7006: Parameter 'f' implicitly has an 'any' type.
index.js(13,29): error TS2345: Argument of type 'IArguments' is not assignable to parameter of type '[f: any]'.
index.js(13,29): error TS2345: Argument of type 'IArguments' is not assignable to parameter of type '[f?: any]'.


==== index.js (2 errors) ====
Expand All @@ -19,6 +19,6 @@ index.js(13,29): error TS2345: Argument of type 'IArguments' is not assignable t
const debuglog = function() {
return format.apply(null, arguments);
~~~~~~~~~
!!! error TS2345: Argument of type 'IArguments' is not assignable to parameter of type '[f: any]'.
!!! error TS2345: Argument of type 'IArguments' is not assignable to parameter of type '[f?: any]'.
};

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var k = function (x) { return x }
/** @typedef {(x: 'hi' | 'bye') => 0 | 1 | 2} Argle */
/** @type {Argle} */
function blargle(s) {
>blargle : (s: "bye" | "hi") => 0 | 1 | 2
>blargle : (x: "bye" | "hi") => 0 | 1 | 2
>s : "bye" | "hi"

return 0;
Expand All @@ -57,7 +57,7 @@ function blargle(s) {
var zeroonetwo = blargle('hi')
>zeroonetwo : 0 | 1 | 2
>blargle('hi') : 0 | 1 | 2
>blargle : (s: "bye" | "hi") => 0 | 1 | 2
>blargle : (x: "bye" | "hi") => 0 | 1 | 2
>'hi' : "hi"

/** @typedef {{(s: string): 0 | 1; (b: boolean): 2 | 3 }} Gioconda */
Expand Down
Loading