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
3 changes: 0 additions & 3 deletions pkg/ast/parseoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ func isFileProbablyExternalModule(sourceFile *SourceFile) *Node {
}

func isAnExternalModuleIndicatorNode(node *Node) bool {
if node.Flags&NodeFlagsReparsed != 0 {
return false
}
return HasSyntacticModifier(node, ModifierFlagsExport) ||
IsImportEqualsDeclaration(node) && IsExternalModuleReference(node.AsImportEqualsDeclaration().ModuleReference) ||
IsImportDeclaration(node) || IsExportAssignment(node) || IsExportDeclaration(node)
Expand Down
4 changes: 0 additions & 4 deletions pkg/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -1391,10 +1391,6 @@ func GetNameOfDeclaration(declaration *Node) *Node {
return nil
}

func GetImportClauseOfDeclaration(declaration *Declaration) *ImportClause {
return declaration.ImportClause().AsImportClause()
}

func GetNonAssignedNameOfDeclaration(declaration *Node) *Node {
// !!!
switch declaration.Kind {
Expand Down
57 changes: 57 additions & 0 deletions pkg/astnav/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,3 +672,60 @@ func shouldSkipChild(node *ast.Node) bool {
ast.IsJSDocLinkLike(node) ||
ast.IsJSDocTag(node)
}

// FindChildOfKind searches for a child node or token of the specified kind within a containing node.
// This function scans through both AST nodes and intervening tokens to find the first match.
func FindChildOfKind(containingNode *ast.Node, kind ast.Kind, sourceFile *ast.SourceFile) *ast.Node {
lastNodePos := containingNode.Pos()
scan := scanner.GetScannerForSourceFile(sourceFile, lastNodePos)

var foundChild *ast.Node
visitNode := func(node *ast.Node) bool {
if node == nil || node.Flags&ast.NodeFlagsReparsed != 0 {
return false
}
// Look for child in preceding tokens.
startPos := lastNodePos
for startPos < node.Pos() {
tokenKind := scan.Token()
tokenFullStart := scan.TokenFullStart()
tokenEnd := scan.TokenEnd()
token := sourceFile.GetOrCreateToken(tokenKind, tokenFullStart, tokenEnd, containingNode)
if tokenKind == kind {
foundChild = token
return true
}
startPos = tokenEnd
scan.Scan()
}
if node.Kind == kind {
foundChild = node
return true
}

lastNodePos = node.End()
scan.ResetPos(lastNodePos)
return false
}

ast.ForEachChildAndJSDoc(containingNode, sourceFile, visitNode)

if foundChild != nil {
return foundChild
}

// Look for child in trailing tokens.
startPos := lastNodePos
for startPos < containingNode.End() {
tokenKind := scan.Token()
tokenFullStart := scan.TokenFullStart()
tokenEnd := scan.TokenEnd()
token := sourceFile.GetOrCreateToken(tokenKind, tokenFullStart, tokenEnd, containingNode)
if tokenKind == kind {
return token
}
startPos = tokenEnd
scan.Scan()
}
return nil
}
4 changes: 2 additions & 2 deletions pkg/binder/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func GetSymbolNameForPrivateIdentifier(containingClassSymbol *ast.Symbol, descri

func (b *Binder) declareModuleMember(node *ast.Node, symbolFlags ast.SymbolFlags, symbolExcludes ast.SymbolFlags) *ast.Symbol {
container := b.container
if node.Kind == ast.KindCommonJSExport {
if ast.IsCommonJSExport(node) {
container = b.file.AsNode()
}
hasExportModifier := ast.GetCombinedModifierFlags(node)&ast.ModifierFlagsExport != 0 || ast.IsImplicitlyExportedJSTypeAlias(node)
Expand All @@ -402,7 +402,7 @@ func (b *Binder) declareModuleMember(node *ast.Node, symbolFlags ast.SymbolFlags
// during global merging in the checker. Why? The only case when ambient module is permitted inside another module is module augmentation
// and this case is specially handled. Module augmentations should only be merged with original module definition
// and should never be merged directly with other augmentation, and the latter case would be possible if automatic merge is allowed.
if !ast.IsAmbientModule(node) && (hasExportModifier || container.Flags&ast.NodeFlagsExportContext != 0) {
if !ast.IsAmbientModule(node) && (hasExportModifier || ast.IsCommonJSExport(node) || container.Flags&ast.NodeFlagsExportContext != 0) {
if !ast.IsLocalsContainer(container) || (ast.HasSyntacticModifier(node, ast.ModifierFlagsDefault) && b.getDeclarationName(node) == ast.InternalSymbolNameMissing) || ast.IsCommonJSExport(node) {
return b.declareSymbol(ast.GetExports(container.Symbol()), container.Symbol(), node, symbolFlags, symbolExcludes)
// No local symbol for an unnamed default!
Expand Down
4 changes: 2 additions & 2 deletions pkg/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2859,7 +2859,7 @@ func (c *Checker) getDeprecatedSuggestionNode(node *ast.Node) *ast.Node {
case ast.KindTaggedTemplateExpression:
return c.getDeprecatedSuggestionNode(node.AsTaggedTemplateExpression().Tag)
case ast.KindJsxOpeningElement, ast.KindJsxSelfClosingElement:
return c.getDeprecatedSuggestionNode(getTagNameOfNode(node))
return c.getDeprecatedSuggestionNode(node.TagName())
case ast.KindElementAccessExpression:
return node.AsElementAccessExpression().ArgumentExpression
case ast.KindPropertyAccessExpression:
Expand Down Expand Up @@ -30342,7 +30342,7 @@ func (c *Checker) hasContextualTypeWithNoGenericTypes(node *ast.Node, checkMode
// If check mode has `CheckMode.RestBindingElement`, we skip binding pattern contextual types,
// as we want the type of a rest element to be generic when possible.
if (ast.IsIdentifier(node) || ast.IsPropertyAccessExpression(node) || ast.IsElementAccessExpression(node)) &&
!((ast.IsJsxOpeningElement(node.Parent) || ast.IsJsxSelfClosingElement(node.Parent)) && getTagNameOfNode(node.Parent) == node) {
!((ast.IsJsxOpeningElement(node.Parent) || ast.IsJsxSelfClosingElement(node.Parent)) && node.Parent.TagName() == node) {
contextualType := c.getContextualType(node, core.IfElse(checkMode&CheckModeRestBindingElement != 0, ContextFlagsSkipBindingPatterns, ContextFlagsNone))
if contextualType != nil {
return !c.isGenericType(contextualType)
Expand Down
12 changes: 12 additions & 0 deletions pkg/checker/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,15 @@ func (c *Checker) GetIndexSignaturesAtLocation(node *ast.Node) []*ast.Node {
func (c *Checker) GetResolvedSymbol(node *ast.Node) *ast.Symbol {
return c.getResolvedSymbol(node)
}

func (c *Checker) GetJsxNamespace(location *ast.Node) string {
return c.getJsxNamespace(location)
}

func (c *Checker) ResolveName(name string, location *ast.Node, meaning ast.SymbolFlags, excludeGlobals bool) *ast.Symbol {
return c.resolveName(location, name, meaning, nil, true, excludeGlobals)
}

func (c *Checker) GetSymbolFlags(symbol *ast.Symbol) ast.SymbolFlags {
return c.getSymbolFlags(symbol)
}
4 changes: 2 additions & 2 deletions pkg/checker/grammarchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,8 +798,8 @@ func (c *Checker) checkGrammarArrowFunction(node *ast.Node, file *ast.SourceFile
}

equalsGreaterThanToken := arrowFunc.EqualsGreaterThanToken
startLine, _ := scanner.GetECMALineAndCharacterOfPosition(file, equalsGreaterThanToken.Pos())
endLine, _ := scanner.GetECMALineAndCharacterOfPosition(file, equalsGreaterThanToken.End())
startLine := scanner.GetECMALineOfPosition(file, equalsGreaterThanToken.Pos())
endLine := scanner.GetECMALineOfPosition(file, equalsGreaterThanToken.End())
return startLine != endLine && c.grammarErrorOnNode(equalsGreaterThanToken, diagnostics.Line_terminator_not_permitted_before_arrow)
}

Expand Down
11 changes: 7 additions & 4 deletions pkg/checker/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,13 @@ func IsKnownSymbol(symbol *ast.Symbol) bool {
return isLateBoundName(symbol.Name)
}

func IsPrivateIdentifierSymbol(symbol *ast.Symbol) bool {
if symbol == nil {
return false
}
return strings.HasPrefix(symbol.Name, ast.InternalSymbolNamePrefix+"#")
}

func isLateBoundName(name string) bool {
return len(name) >= 2 && name[0] == '\xfe' && name[1] == '@'
}
Expand Down Expand Up @@ -1061,10 +1068,6 @@ func isNonNullAccess(node *ast.Node) bool {
return ast.IsAccessExpression(node) && ast.IsNonNullExpression(node.Expression())
}

func getTagNameOfNode(node *ast.Node) *ast.Node {
return node.TagName()
}

func getBindingElementPropertyName(node *ast.Node) *ast.Node {
return node.PropertyNameOrName()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/compiler/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ func (p *Program) getDiagnosticsWithPrecedingDirectives(sourceFile *ast.SourceFi
// Build map of directives by line number
directivesByLine := make(map[int]ast.CommentDirective)
for _, directive := range sourceFile.CommentDirectives {
line, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, directive.Loc.Pos())
line := scanner.GetECMALineOfPosition(sourceFile, directive.Loc.Pos())
directivesByLine[line] = directive
}
lineStarts := scanner.GetECMALineStarts(sourceFile)
Expand Down
4 changes: 2 additions & 2 deletions pkg/diagnosticwriter/diagnosticwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, l
lastLineChar++ // When length is zero, squiggle the character right after the start position.
}

lastLineOfFile, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, len(sourceFile.Text()))
lastLineOfFile := scanner.GetECMALineOfPosition(sourceFile, len(sourceFile.Text()))

hasMoreThanFiveLines := lastLine-firstLine >= 4
gutterWidth := len(strconv.Itoa(lastLine + 1))
Expand Down Expand Up @@ -357,7 +357,7 @@ func prettyPathForFileError(file *ast.SourceFile, fileErrors []*ast.Diagnostic,
if file == nil || len(fileErrors) == 0 {
return ""
}
line, _ := scanner.GetECMALineAndCharacterOfPosition(file, fileErrors[0].Loc().Pos())
line := scanner.GetECMALineOfPosition(file, fileErrors[0].Loc().Pos())
fileName := file.FileName()
if tspath.PathIsAbsolute(fileName) && tspath.PathIsAbsolute(formatOpts.CurrentDirectory) {
fileName = tspath.ConvertToRelativePath(file.FileName(), formatOpts.ComparePathsOptions)
Expand Down
4 changes: 3 additions & 1 deletion pkg/execute/tsc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/buke/typescript-go-internal/pkg/ast"
"github.com/buke/typescript-go-internal/pkg/collections"
"github.com/buke/typescript-go-internal/pkg/compiler"
"github.com/buke/typescript-go-internal/pkg/core"
"github.com/buke/typescript-go-internal/pkg/diagnostics"
Expand Down Expand Up @@ -113,7 +114,8 @@ func tscCompilation(sys tsc.System, commandLine *tsoptions.ParsedCommandLine, te
}

if commandLine.CompilerOptions().Init.IsTrue() {
return tsc.CommandLineResult{Status: tsc.ExitStatusNotImplemented}
tsc.WriteConfigFile(sys, reportDiagnostic, commandLine.Raw.(*collections.OrderedMap[string, any]))
return tsc.CommandLineResult{Status: tsc.ExitStatusSuccess}
}

if commandLine.CompilerOptions().Version.IsTrue() {
Expand Down
Loading