Skip to content

Commit 07a5e04

Browse files
gabrittojakebailey
andauthored
Add symbol baseliner (#191)
Co-authored-by: Jake Bailey <[email protected]>
1 parent 2f46c27 commit 07a5e04

File tree

11 files changed

+346
-40
lines changed

11 files changed

+346
-40
lines changed

internal/ast/utilities.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ func IsDeclaration(node *Node) bool {
11231123

11241124
// True if `name` is the name of a declaration node
11251125
func IsDeclarationName(name *Node) bool {
1126-
return !IsSourceFile(name) && !IsBindingPattern(name) && IsDeclaration(name.Parent)
1126+
return !IsSourceFile(name) && !IsBindingPattern(name) && IsDeclaration(name.Parent) && name.Parent.Name() == name
11271127
}
11281128

11291129
// Like 'isDeclarationName', but returns true for LHS of `import { x as y }` or `export { x as y }`.

internal/compiler/checker.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8073,7 +8073,7 @@ func (c *Checker) isInPropertyInitializerOrClassStaticBlock(node *ast.Node) bool
80738073
}
80748074
return ast.FindAncestorQuit
80758075
default:
8076-
if isExpressionNode(node) {
8076+
if IsExpressionNode(node) {
80778077
return ast.FindAncestorFalse
80788078
}
80798079
return ast.FindAncestorQuit
@@ -22736,6 +22736,14 @@ func (c *Checker) getActualTypeVariable(t *Type) *Type {
2273622736
return t
2273722737
}
2273822738

22739+
func (c *Checker) GetSymbolAtLocation(node *ast.Node) *ast.Symbol {
22740+
// !!!
22741+
// const node = getParseTreeNode(nodeIn);
22742+
22743+
// set ignoreErrors: true because any lookups invoked by the API shouldn't cause any new errors
22744+
return c.getSymbolAtLocation(node, true /*ignoreErrors*/)
22745+
}
22746+
2273922747
func (c *Checker) getSymbolAtLocation(node *ast.Node, ignoreErrors bool) *ast.Symbol {
2274022748
if ast.IsSourceFile(node) {
2274122749
if ast.IsExternalModule(node.AsSourceFile()) {
@@ -22963,7 +22971,7 @@ func (c *Checker) getSymbolOfNameOrPropertyAccessExpression(name *ast.Node) *ast
2296322971
}
2296422972
}
2296522973

22966-
if isExpressionNode(name) {
22974+
if IsExpressionNode(name) {
2296722975
if ast.NodeIsMissing(name) {
2296822976
// Missing entity name.
2296922977
return nil
@@ -23066,7 +23074,7 @@ func (c *Checker) getTypeOfNode(node *ast.Node) *Type {
2306623074
return typeFromTypeNode
2306723075
}
2306823076

23069-
if isExpressionNode(node) {
23077+
if IsExpressionNode(node) {
2307023078
return c.getRegularTypeOfExpression(node)
2307123079
}
2307223080

internal/compiler/checker_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ foo.bar;`
3333
}
3434
p := NewProgram(opts)
3535
p.bindSourceFiles()
36-
c := p.getTypeChecker()
36+
c := p.GetTypeChecker()
3737
file := p.filesByPath["/foo.ts"]
3838
interfaceId := file.Statements.Nodes[0].Name()
3939
varId := file.Statements.Nodes[1].AsVariableStatement().DeclarationList.AsVariableDeclarationList().Declarations.Nodes[0].Name()
4040
propAccess := file.Statements.Nodes[2].AsExpressionStatement().Expression
4141
nodes := []*ast.Node{interfaceId, varId, propAccess}
4242
for _, node := range nodes {
43-
symbol := c.getSymbolAtLocation(node, true /*ignoreErrors*/)
43+
symbol := c.GetSymbolAtLocation(node)
4444
if symbol == nil {
4545
t.Fatalf("Expected symbol to be non-nil")
4646
}

internal/compiler/grammarchecks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (c *Checker) checkGrammarPrivateIdentifierExpression(privId *ast.PrivateIde
9797
}
9898

9999
if !ast.IsForInStatement(privId.Parent) {
100-
if !isExpressionNode(privIdAsNode) {
100+
if !IsExpressionNode(privIdAsNode) {
101101
return c.grammarErrorOnNode(privIdAsNode, diagnostics.Private_identifiers_are_only_allowed_in_class_bodies_and_may_only_be_used_as_part_of_a_class_member_declaration_property_access_or_on_the_left_hand_side_of_an_in_expression)
102102
}
103103

internal/compiler/printer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ func (c *Checker) getTypePrecedence(t *Type) ast.TypePrecedence {
2727
return ast.TypePrecedenceNonArray
2828
}
2929

30+
func (c *Checker) SymbolToString(s *ast.Symbol) string {
31+
return c.symbolToString(s)
32+
}
33+
3034
func (c *Checker) symbolToString(s *ast.Symbol) string {
3135
if s.ValueDeclaration != nil {
3236
name := ast.GetNameOfDeclaration(s.ValueDeclaration)
@@ -571,7 +575,7 @@ func (c *Checker) getTextAndTypeOfNode(node *ast.Node) (string, *Type, bool) {
571575
}
572576
}
573577
}
574-
if isExpressionNode(node) && !isRightSideOfQualifiedNameOrPropertyAccess(node) {
578+
if IsExpressionNode(node) && !isRightSideOfQualifiedNameOrPropertyAccess(node) {
575579
return scanner.GetTextOfNode(node), c.getTypeOfExpression(node), false
576580
}
577581
return "", nil, false

internal/compiler/program.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ func (p *Program) GetSemanticDiagnostics(sourceFile *ast.SourceFile) []*ast.Diag
360360
}
361361

362362
func (p *Program) GetGlobalDiagnostics() []*ast.Diagnostic {
363-
return sortAndDeduplicateDiagnostics(p.getTypeChecker().GetGlobalDiagnostics())
363+
return sortAndDeduplicateDiagnostics(p.GetTypeChecker().GetGlobalDiagnostics())
364364
}
365365

366366
func (p *Program) TypeCount() int {
@@ -370,7 +370,7 @@ func (p *Program) TypeCount() int {
370370
return int(p.checker.typeCount)
371371
}
372372

373-
func (p *Program) getTypeChecker() *Checker {
373+
func (p *Program) GetTypeChecker() *Checker {
374374
if p.checker == nil {
375375
p.checker = NewChecker(p)
376376
}
@@ -386,7 +386,7 @@ func (p *Program) getBindDiagnosticsForFile(sourceFile *ast.SourceFile) []*ast.D
386386
}
387387

388388
func (p *Program) getSemanticDiagnosticsForFile(sourceFile *ast.SourceFile) []*ast.Diagnostic {
389-
return core.Concatenate(sourceFile.BindDiagnostics(), p.getTypeChecker().GetDiagnostics(sourceFile))
389+
return core.Concatenate(sourceFile.BindDiagnostics(), p.GetTypeChecker().GetDiagnostics(sourceFile))
390390
}
391391

392392
func (p *Program) getDiagnosticsHelper(sourceFile *ast.SourceFile, ensureBound bool, getDiagnostics func(*ast.SourceFile) []*ast.Diagnostic) []*ast.Diagnostic {
@@ -414,7 +414,7 @@ type NodeCount struct {
414414
func (p *Program) PrintSourceFileWithTypes() {
415415
for _, file := range p.files {
416416
if tspath.GetBaseFileName(file.FileName()) == "main.ts" {
417-
fmt.Print(p.getTypeChecker().sourceFileWithTypes(file))
417+
fmt.Print(p.GetTypeChecker().sourceFileWithTypes(file))
418418
}
419419
}
420420
}
@@ -785,3 +785,8 @@ func (p *Program) Emit(options *EmitOptions) *EmitResult {
785785
}
786786
return result
787787
}
788+
789+
func (p *Program) GetSourceFile(filename string) *ast.SourceFile {
790+
path := tspath.ToPath(filename, p.host.GetCurrentDirectory(), p.host.FS().UseCaseSensitiveFileNames())
791+
return p.filesByPath[path]
792+
}

internal/compiler/utilities.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ func isValidTypeOnlyAliasUseSite(useSite *ast.Node) bool {
993993
ast.IsPartOfTypeQuery(useSite) ||
994994
isIdentifierInNonEmittingHeritageClause(useSite) ||
995995
isPartOfPossiblyValidTypeOrAbstractComputedPropertyName(useSite) ||
996-
!(isExpressionNode(useSite) || isShorthandPropertyNameUseSite(useSite))
996+
!(IsExpressionNode(useSite) || isShorthandPropertyNameUseSite(useSite))
997997
}
998998

999999
func isIdentifierInNonEmittingHeritageClause(node *ast.Node) bool {
@@ -1062,7 +1062,7 @@ func nodeCanBeDecorated(useLegacyDecorators bool, node *ast.Node, parent *ast.No
10621062
return false
10631063
}
10641064

1065-
func isExpressionNode(node *ast.Node) bool {
1065+
func IsExpressionNode(node *ast.Node) bool {
10661066
switch node.Kind {
10671067
case ast.KindSuperKeyword, ast.KindNullKeyword, ast.KindTrueKeyword, ast.KindFalseKeyword, ast.KindRegularExpressionLiteral,
10681068
ast.KindArrayLiteralExpression, ast.KindObjectLiteralExpression, ast.KindPropertyAccessExpression, ast.KindElementAccessExpression,
@@ -1154,7 +1154,7 @@ func isInExpressionContext(node *ast.Node) bool {
11541154
case ast.KindSatisfiesExpression:
11551155
return parent.AsSatisfiesExpression().Expression == node
11561156
default:
1157-
return isExpressionNode(parent)
1157+
return IsExpressionNode(parent)
11581158
}
11591159
}
11601160

internal/testutil/baseline/baseline.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ type Options struct {
1515

1616
const NoContent = "<no content>"
1717

18-
func Run(t testing.TB, fileName string, actual string, opts Options) {
18+
func Run(t *testing.T, fileName string, actual string, opts Options) {
1919
writeComparison(t, actual, fileName, opts)
2020
}
2121

22-
func writeComparison(t testing.TB, actual string, relativeFileName string, opts Options) {
22+
func writeComparison(t *testing.T, actual string, relativeFileName string, opts Options) {
2323
if actual == "" {
2424
panic("The generated content was \"\". Return 'baseline.NoContent' if no baselining is required.")
2525
}

internal/testutil/baseline/error_baseline.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ import (
1919
// IO
2020
const harnessNewLine = "\r\n"
2121

22-
var (
23-
lineDelimiter = regexp.MustCompile("\r?\n")
24-
nonWhitespace = regexp.MustCompile(`\S`)
25-
tsExtension = regexp.MustCompile(`\.tsx?$`)
26-
)
27-
2822
var formatOpts = &compiler.DiagnosticsFormattingOptions{
2923
NewLine: harnessNewLine,
3024
}
@@ -40,7 +34,7 @@ var (
4034
diagnosticsLocationPattern = regexp.MustCompile(`(?i)(lib.*\.d\.ts):\d+:\d+`)
4135
)
4236

43-
func DoErrorBaseline(t testing.TB, baselinePath string, inputFiles []*TestFile, errors []*ast.Diagnostic, pretty bool) {
37+
func DoErrorBaseline(t *testing.T, baselinePath string, inputFiles []*TestFile, errors []*ast.Diagnostic, pretty bool) {
4438
baselinePath = tsExtension.ReplaceAllString(baselinePath, ".errors.txt")
4539
var errorBaseline string
4640
if len(errors) > 0 {
@@ -61,7 +55,7 @@ func minimalDiagnosticsToString(diagnostics []*ast.Diagnostic, pretty bool) stri
6155
return output.String()
6256
}
6357

64-
func getErrorBaseline(t testing.TB, inputFiles []*TestFile, diagnostics []*ast.Diagnostic, pretty bool) string {
58+
func getErrorBaseline(t *testing.T, inputFiles []*TestFile, diagnostics []*ast.Diagnostic, pretty bool) string {
6559
t.Helper()
6660
outputLines := iterateErrorBaseline(t, inputFiles, diagnostics, pretty)
6761

@@ -77,7 +71,7 @@ func getErrorBaseline(t testing.TB, inputFiles []*TestFile, diagnostics []*ast.D
7771
return strings.Join(outputLines, "")
7872
}
7973

80-
func iterateErrorBaseline(t testing.TB, inputFiles []*TestFile, inputDiagnostics []*ast.Diagnostic, pretty bool) []string {
74+
func iterateErrorBaseline(t *testing.T, inputFiles []*TestFile, inputDiagnostics []*ast.Diagnostic, pretty bool) []string {
8175
t.Helper()
8276
diagnostics := slices.Clone(inputDiagnostics)
8377
slices.SortFunc(diagnostics, compiler.CompareDiagnostics)
@@ -225,7 +219,6 @@ func iterateErrorBaseline(t testing.TB, inputFiles []*TestFile, inputDiagnostics
225219
// Verify we didn't miss any errors in this file
226220
assert.Check(t, cmp.Equal(markedErrorCount, len(fileErrors)), "count of errors in "+inputFile.unitName)
227221
_, isDupe := dupeCase[sanitizeTestFilePath(inputFile.unitName)]
228-
checkDuplicatedFileName(inputFile.unitName, dupeCase)
229222
result = append(result, outputLines.String())
230223
if isDupe {
231224
// Case-duplicated files on a case-insensitive build will have errors reported in both the dupe and the original
@@ -253,19 +246,6 @@ func iterateErrorBaseline(t testing.TB, inputFiles []*TestFile, inputDiagnostics
253246
return result
254247
}
255248

256-
func checkDuplicatedFileName(resultName string, dupeCase map[string]int) string {
257-
resultName = sanitizeTestFilePath(resultName)
258-
if _, ok := dupeCase[resultName]; ok {
259-
// A different baseline filename should be manufactured if the names differ only in case, for windows compat
260-
count := 1 + dupeCase[resultName]
261-
dupeCase[resultName] = count
262-
resultName = fmt.Sprintf("%s.dupe%d", resultName, count)
263-
} else {
264-
dupeCase[resultName] = 0
265-
}
266-
return resultName
267-
}
268-
269249
func flattenDiagnosticMessage(d *ast.Diagnostic, newLine string) string {
270250
var output strings.Builder
271251
compiler.WriteFlattenedDiagnosticMessage(&output, d, newLine)

0 commit comments

Comments
 (0)