Skip to content

Implement getJSSyntacticDiagnosticsForFile for TypeScript-only construct detection in JavaScript files #1387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
68c5302
Initial plan
Copilot Jul 11, 2025
e4b5e3c
Add getJSSyntacticDiagnosticsForFile implementation and additionalSyn…
Copilot Jul 11, 2025
194f161
Fix AST node conversion issues and add comprehensive test case
Copilot Jul 11, 2025
466a9f8
Add NodeFlagsReparsed check to bail out early for synthesized type an…
Copilot Jul 11, 2025
3e0d26f
Update baselines after implementing NodeFlagsReparsed check
Copilot Jul 11, 2025
bd6a76d
Move diagnostic calculation to ast package with thread-safe lazy eval…
Copilot Jul 11, 2025
1e6c41e
Refactor JS diagnostic functions: move field order and extract to sep…
Copilot Jul 11, 2025
84ece9d
Address review feedback: format code, fix diagnostics initialization,…
Copilot Jul 11, 2025
617d7ac
Add NodeFlagsReparsed checks to fix false positives on JSDoc type ann…
Copilot Jul 11, 2025
2f7b583
Move JS diagnostics from ast to compiler package and use SyncMap for …
Copilot Jul 11, 2025
d55f804
Fix false positives on JSDoc type arguments in JavaScript files
Copilot Jul 11, 2025
732f699
Refactor JS diagnostics: move to separate file and use visitor pattern
Copilot Jul 11, 2025
eaeddaf
Refactor JS diagnostics to use binder and scanner functions for bette…
Copilot Jul 11, 2025
0eb0b7f
Merge branch 'main' into copilot/fix-1386
jakebailey Jul 11, 2025
0305435
Fix JS diagnostic positioning and detection issues
Copilot Jul 11, 2025
457a6d3
Fix JS diagnostic positioning for type parameters to match TypeScript…
Copilot Jul 11, 2025
88f567f
Remove isTypeScriptOnlyModifier function and fix modifier checking logic
Copilot Jul 11, 2025
47234a3
Add missing question mark modifier check for method and property decl…
Copilot Jul 11, 2025
5d296fa
Remove duplicate JS diagnostic cases and fix token text detection
Copilot Jul 11, 2025
bd46d8a
Manual tweak
jakebailey Jul 11, 2025
241e1c7
Merge branch 'main' into copilot/fix-1386
jakebailey Jul 11, 2025
8389be3
Extract JS diagnostics calculation into helper function
Copilot Jul 11, 2025
94d8aed
Fix JS diagnostics caching to avoid unnecessary AST walks
Copilot Jul 11, 2025
4894614
Move GetErrorRangeForNode to scanner
jakebailey Jul 11, 2025
563ca59
Remove old todo
jakebailey Jul 11, 2025
a460d7b
Merge branch 'main' into copilot/fix-1386
jakebailey Jul 11, 2025
1719219
Move to parser
jakebailey Jul 11, 2025
4873b39
Fix binding issue
jakebailey Jul 11, 2025
ec7ddc5
Remove cache
jakebailey Jul 11, 2025
e67cd95
No need for empty slice anymore
jakebailey Jul 11, 2025
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
9 changes: 9 additions & 0 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -10049,6 +10049,7 @@ type SourceFile struct {
// Fields set by parser
diagnostics []*Diagnostic
jsdocDiagnostics []*Diagnostic
jsSyntacticDiagnostics []*Diagnostic
LanguageVariant core.LanguageVariant
ScriptKind core.ScriptKind
IsDeclarationFile bool
Expand Down Expand Up @@ -10147,6 +10148,14 @@ func (node *SourceFile) SetJSDocDiagnostics(diags []*Diagnostic) {
node.jsdocDiagnostics = diags
}

func (node *SourceFile) JSSyntacticDiagnostics() []*Diagnostic {
return node.jsSyntacticDiagnostics
}

func (node *SourceFile) SetJSSyntacticDiagnostics(diags []*Diagnostic) {
node.jsSyntacticDiagnostics = diags
}

func (node *SourceFile) JSDocCache() map[*Node][]*Node {
return node.jsdocCache
}
Expand Down
82 changes: 1 addition & 81 deletions internal/binder/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2758,7 +2758,7 @@ func (b *Binder) errorOrSuggestionOnRange(isError bool, startNode *ast.Node, end
// If so, the node _must_ be in the current file (as that's the only way anything could have traversed to it to yield it as the error node)
// This version of `createDiagnosticForNode` uses the binder's context to account for this, and always yields correct diagnostics even in these situations.
func (b *Binder) createDiagnosticForNode(node *ast.Node, message *diagnostics.Message, args ...any) *ast.Diagnostic {
return ast.NewDiagnostic(b.file, GetErrorRangeForNode(b.file, node), message, args...)
return ast.NewDiagnostic(b.file, scanner.GetErrorRangeForNode(b.file, node), message, args...)
}

func (b *Binder) addDiagnostic(diagnostic *ast.Diagnostic) {
Expand Down Expand Up @@ -2855,83 +2855,3 @@ func isAssignmentDeclaration(decl *ast.Node) bool {
func isEffectiveModuleDeclaration(node *ast.Node) bool {
return ast.IsModuleDeclaration(node) || ast.IsIdentifier(node)
}

func getErrorRangeForArrowFunction(sourceFile *ast.SourceFile, node *ast.Node) core.TextRange {
pos := scanner.SkipTrivia(sourceFile.Text(), node.Pos())
body := node.AsArrowFunction().Body
if body != nil && body.Kind == ast.KindBlock {
startLine, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, body.Pos())
endLine, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, body.End())
if startLine < endLine {
// The arrow function spans multiple lines,
// make the error span be the first line, inclusive.
return core.NewTextRange(pos, scanner.GetEndLinePosition(sourceFile, startLine))
}
}
return core.NewTextRange(pos, node.End())
}

func GetErrorRangeForNode(sourceFile *ast.SourceFile, node *ast.Node) core.TextRange {
errorNode := node
switch node.Kind {
case ast.KindSourceFile:
pos := scanner.SkipTrivia(sourceFile.Text(), 0)
if pos == len(sourceFile.Text()) {
return core.NewTextRange(0, 0)
}
return scanner.GetRangeOfTokenAtPosition(sourceFile, pos)
// This list is a work in progress. Add missing node kinds to improve their error spans
case ast.KindFunctionDeclaration, ast.KindMethodDeclaration:
if node.Flags&ast.NodeFlagsReparsed != 0 {
errorNode = node
break
}
fallthrough
case ast.KindVariableDeclaration, ast.KindBindingElement, ast.KindClassDeclaration, ast.KindClassExpression, ast.KindInterfaceDeclaration,
ast.KindModuleDeclaration, ast.KindEnumDeclaration, ast.KindEnumMember, ast.KindFunctionExpression,
ast.KindGetAccessor, ast.KindSetAccessor, ast.KindTypeAliasDeclaration, ast.KindJSTypeAliasDeclaration, ast.KindPropertyDeclaration,
ast.KindPropertySignature, ast.KindNamespaceImport:
errorNode = ast.GetNameOfDeclaration(node)
case ast.KindArrowFunction:
return getErrorRangeForArrowFunction(sourceFile, node)
case ast.KindCaseClause, ast.KindDefaultClause:
start := scanner.SkipTrivia(sourceFile.Text(), node.Pos())
end := node.End()
statements := node.AsCaseOrDefaultClause().Statements.Nodes
if len(statements) != 0 {
end = statements[0].Pos()
}
return core.NewTextRange(start, end)
case ast.KindReturnStatement, ast.KindYieldExpression:
pos := scanner.SkipTrivia(sourceFile.Text(), node.Pos())
return scanner.GetRangeOfTokenAtPosition(sourceFile, pos)
case ast.KindSatisfiesExpression:
pos := scanner.SkipTrivia(sourceFile.Text(), node.AsSatisfiesExpression().Expression.End())
return scanner.GetRangeOfTokenAtPosition(sourceFile, pos)
case ast.KindConstructor:
if node.Flags&ast.NodeFlagsReparsed != 0 {
errorNode = node
break
}
scanner := scanner.GetScannerForSourceFile(sourceFile, node.Pos())
start := scanner.TokenStart()
for scanner.Token() != ast.KindConstructorKeyword && scanner.Token() != ast.KindStringLiteral && scanner.Token() != ast.KindEndOfFile {
scanner.Scan()
}
return core.NewTextRange(start, scanner.TokenEnd())
// !!!
// case KindJSDocSatisfiesTag:
// pos := scanner.SkipTrivia(sourceFile.Text(), node.tagName.pos)
// return scanner.GetRangeOfTokenAtPosition(sourceFile, pos)
}
if errorNode == nil {
// If we don't have a better node, then just set the error on the first token of
// construct.
return scanner.GetRangeOfTokenAtPosition(sourceFile, node.Pos())
}
pos := errorNode.Pos()
if !ast.NodeIsMissing(errorNode) && !ast.IsJsxText(errorNode) {
pos = scanner.SkipTrivia(sourceFile.Text(), pos)
}
return core.NewTextRange(pos, errorNode.End())
}
2 changes: 1 addition & 1 deletion internal/checker/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewDiagnosticForNode(node *ast.Node, message *diagnostics.Message, args ...
var loc core.TextRange
if node != nil {
file = ast.GetSourceFileOfNode(node)
loc = binder.GetErrorRangeForNode(file, node)
loc = scanner.GetErrorRangeForNode(file, node)
}
return ast.NewDiagnostic(file, loc, message, args...)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/compiler/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,11 @@ func (p *Program) getOptionsDiagnosticsOfConfigFile() []*ast.Diagnostic {
}

func (p *Program) getSyntacticDiagnosticsForFile(ctx context.Context, sourceFile *ast.SourceFile) []*ast.Diagnostic {
// For JavaScript files, we report semantic errors for using TypeScript-only
// constructs from within a JavaScript file as syntactic errors.
if jsSyntacticDiagnostics := sourceFile.JSSyntacticDiagnostics(); len(jsSyntacticDiagnostics) > 0 {
return slices.Concat(jsSyntacticDiagnostics, sourceFile.Diagnostics())
}
return sourceFile.Diagnostics()
}

Expand Down
Loading
Loading