Skip to content

Commit cfe3307

Browse files
committed
Merge branch 'main' into tsconfigParsingImplementation
2 parents 20ff599 + fcb4ab1 commit cfe3307

24 files changed

+944
-94
lines changed

.github/workflows/ci.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,23 @@ jobs:
7373
# Installing gotestsum is super slow on Windows.
7474
if: ${{ matrix.os != 'windows-latest' }}
7575

76-
- run: npx hereby test:all ${RACE_FLAG:+"$RACE_FLAG"} ${NOEMBED_FLAG:+"$NOEMBED_FLAG"}
76+
- name: Tests
77+
id: test
78+
run: npx hereby test:all ${RACE_FLAG:+"$RACE_FLAG"} ${NOEMBED_FLAG:+"$NOEMBED_FLAG"}
7779
env:
7880
RACE_FLAG: ${{ (matrix.race && '--race') || '' }}
7981
NOEMBED_FLAG: ${{ (matrix.noembed && '--noembed') || '' }}
8082

8183
- run: git add .
8284
- run: git diff --staged --exit-code --stat
8385

86+
- name: Print baseline diff on failure
87+
if: ${{ failure() && steps.test.conclusion == 'failure' }}
88+
run: |
89+
npx hereby baseline-accept
90+
git add testdata/baselines/reference
91+
git diff --staged --exit-code
92+
8493
lint:
8594
runs-on: ubuntu-latest
8695
steps:

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,12 @@ go.work.sum
173173
# Benchmarking results
174174
*.txt
175175

176+
# Re-add baseline references
177+
!testdata/baselines/reference/**/*.txt
178+
176179
# Local baselines
177180
testdata/baselines/local
181+
testdata/baselines/tmp
178182

179183
custom-gcl
180184
custom-gcl.exe

cmd/tsgo/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/microsoft/typescript-go/internal/bundled"
1616
ts "github.com/microsoft/typescript-go/internal/compiler"
1717
"github.com/microsoft/typescript-go/internal/core"
18+
"github.com/microsoft/typescript-go/internal/diagnosticwriter"
1819
"github.com/microsoft/typescript-go/internal/scanner"
1920
"github.com/microsoft/typescript-go/internal/tspath"
2021
"github.com/microsoft/typescript-go/internal/vfs"
@@ -92,6 +93,7 @@ func main() {
9293
fmt.Fprintf(os.Stderr, "Error: The directory %v does not exist.\n", normalizedRootPath)
9394
os.Exit(1)
9495
}
96+
compilerOptions.ConfigFilePath = normalizedRootPath // This matters for type reference directive resolution
9597

9698
programOptions := ts.ProgramOptions{
9799
RootPath: normalizedRootPath,
@@ -140,13 +142,13 @@ func main() {
140142
UseCaseSensitiveFileNames: useCaseSensitiveFileNames,
141143
}
142144
if pretty {
143-
formatOpts := ts.DiagnosticsFormattingOptions{
145+
formatOpts := diagnosticwriter.FormattingOptions{
144146
NewLine: "\n",
145147
ComparePathsOptions: comparePathOptions,
146148
}
147-
ts.FormatDiagnosticsWithColorAndContext(os.Stdout, diagnostics, &formatOpts)
149+
diagnosticwriter.FormatDiagnosticsWithColorAndContext(os.Stdout, diagnostics, &formatOpts)
148150
fmt.Fprintln(os.Stdout)
149-
ts.WriteErrorSummaryText(os.Stdout, diagnostics, &formatOpts)
151+
diagnosticwriter.WriteErrorSummaryText(os.Stdout, diagnostics, &formatOpts)
150152
} else {
151153
for _, diagnostic := range diagnostics {
152154
printDiagnostic(diagnostic, 0, comparePathOptions)

internal/compiler/program.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ func (p *Program) startParseTask(fileName string, wg *core.WorkGroup, isLib bool
275275
filesToParse = append(filesToParse, toParse{p: resolvedPath})
276276
}
277277

278+
for _, ref := range file.TypeReferenceDirectives {
279+
resolved := p.resolver.ResolveTypeReferenceDirective(ref.FileName, file.FileName(), core.ModuleKindNodeNext, nil)
280+
if resolved.IsResolved() {
281+
filesToParse = append(filesToParse, toParse{p: resolved.ResolvedFileName})
282+
}
283+
}
284+
278285
if p.compilerOptions.NoLib != core.TSTrue {
279286
for _, lib := range file.LibReferenceDirectives {
280287
name, ok := tsoptions.GetLibFileName(lib.FileName)

internal/core/compileroptions.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type CompilerOptions struct {
2020
EmitDeclarationOnly Tristate `json:"emitDeclarationOnly"`
2121
EmitBOM Tristate `json:"emitBOM"`
2222
DownlevelIteration Tristate `json:"downlevelIteration"`
23+
Declaration Tristate `json:"declaration"`
2324
DeclarationDir string `json:"declarationDir"`
2425
DeclarationMap Tristate `json:"declarationMap"`
2526
ESModuleInterop Tristate `json:"esModuleInterop"`
@@ -35,6 +36,7 @@ type CompilerOptions struct {
3536
ModuleDetection ModuleDetectionKind `json:"moduleDetectionKind"`
3637
NewLine NewLineKind `json:"newLine"`
3738
NoEmit Tristate `json:"noEmit"`
39+
NoErrorTruncation Tristate `json:"noErrorTruncation"`
3840
NoFallthroughCasesInSwitch Tristate `json:"noFallthroughCasesInSwitch"`
3941
NoImplicitAny Tristate `json:"noImplicitAny"`
4042
NoImplicitThis Tristate `json:"noImplicitThis"`
@@ -49,6 +51,7 @@ type CompilerOptions struct {
4951
ResolvePackageJsonExports Tristate `json:"resolvePackageJsonExports"`
5052
ResolvePackageJsonImports Tristate `json:"resolvePackageJsonImports"`
5153
RewriteRelativeImportExtensions Tristate `json:"rewriteRelativeImportExtensions"`
54+
SkipLibCheck Tristate `json:"skipLibCheck"`
5255
Strict Tristate `json:"strict"`
5356
StrictBindCallApply Tristate `json:"strictBindCallApply"`
5457
StrictBuiltinIteratorReturn Tristate `json:"strictBuiltinIteratorReturn"`
@@ -63,7 +66,6 @@ type CompilerOptions struct {
6366
UseUnknownInCatchVariables Tristate `json:"useUnknownInCatchVariables"`
6467
VerbatimModuleSyntax Tristate `json:"verbatimModuleSyntax"`
6568
MaxNodeModuleJsDepth Tristate `json:"maxNodeModuleJsDepth"`
66-
SkipLibCheck Tristate `json:"skipLibCheck"`
6769

6870
// Internal fields
6971
ConfigFilePath string `json:"configFilePath"`
@@ -257,8 +259,9 @@ func (m ModuleResolutionKind) String() string {
257259
type NewLineKind int32
258260

259261
const (
260-
NewLineKindCRLF NewLineKind = 0
261-
NewLineKindLF NewLineKind = 1
262+
NewLineKindNone NewLineKind = 0
263+
NewLineKindCRLF NewLineKind = 1
264+
NewLineKindLF NewLineKind = 2
262265
)
263266

264267
func (newLine NewLineKind) GetNewLineCharacter() string {

internal/compiler/error_reporting.go renamed to internal/diagnosticwriter/diagnosticwriter.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package compiler
1+
package diagnosticwriter
22

33
import (
44
"fmt"
@@ -15,7 +15,7 @@ import (
1515
"github.com/microsoft/typescript-go/internal/tspath"
1616
)
1717

18-
type DiagnosticsFormattingOptions struct {
18+
type FormattingOptions struct {
1919
tspath.ComparePathsOptions
2020
NewLine string
2121
}
@@ -35,7 +35,7 @@ const (
3535
ellipsis = "..."
3636
)
3737

38-
func FormatDiagnosticsWithColorAndContext(output io.Writer, diags []*ast.Diagnostic, formatOpts *DiagnosticsFormattingOptions) {
38+
func FormatDiagnosticsWithColorAndContext(output io.Writer, diags []*ast.Diagnostic, formatOpts *FormattingOptions) {
3939
if len(diags) == 0 {
4040
return
4141
}
@@ -80,7 +80,7 @@ func FormatDiagnosticsWithColorAndContext(output io.Writer, diags []*ast.Diagnos
8080
}
8181
}
8282

83-
func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, length int, squiggleColor string, formatOpts *DiagnosticsFormattingOptions) {
83+
func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, length int, squiggleColor string, formatOpts *FormattingOptions) {
8484
firstLine, firstLineChar := scanner.GetLineAndCharacterOfPosition(sourceFile, start)
8585
lastLine, lastLineChar := scanner.GetLineAndCharacterOfPosition(sourceFile, start+length)
8686

@@ -196,7 +196,7 @@ func writeWithStyleAndReset(output io.Writer, text string, formatStyle string) {
196196
fmt.Fprint(output, resetEscapeSequence)
197197
}
198198

199-
func WriteLocation(output io.Writer, file *ast.SourceFile, pos int, formatOpts *DiagnosticsFormattingOptions, writeWithStyleAndReset FormattedWriter) {
199+
func WriteLocation(output io.Writer, file *ast.SourceFile, pos int, formatOpts *FormattingOptions, writeWithStyleAndReset FormattedWriter) {
200200
firstLine, firstChar := scanner.GetLineAndCharacterOfPosition(file, pos)
201201
var relativeFileName string
202202
if formatOpts != nil {
@@ -221,7 +221,7 @@ type ErrorSummary struct {
221221
SortedFileList []*ast.SourceFile
222222
}
223223

224-
func WriteErrorSummaryText(output io.Writer, allDiagnostics []*ast.Diagnostic, formatOpts *DiagnosticsFormattingOptions) {
224+
func WriteErrorSummaryText(output io.Writer, allDiagnostics []*ast.Diagnostic, formatOpts *FormattingOptions) {
225225
// Roughly corresponds to 'getErrorSummaryText' from watch.ts
226226

227227
errorSummary := getErrorSummary(allDiagnostics)
@@ -299,7 +299,7 @@ func getErrorSummary(diags []*ast.Diagnostic) *ErrorSummary {
299299
}
300300
}
301301

302-
func writeTabularErrorsDisplay(output io.Writer, errorSummary *ErrorSummary, formatOpts *DiagnosticsFormattingOptions) {
302+
func writeTabularErrorsDisplay(output io.Writer, errorSummary *ErrorSummary, formatOpts *FormattingOptions) {
303303
sortedFiles := errorSummary.SortedFileList
304304

305305
maxErrors := 0
@@ -330,7 +330,7 @@ func writeTabularErrorsDisplay(output io.Writer, errorSummary *ErrorSummary, for
330330
}
331331
}
332332

333-
func prettyPathForFileError(file *ast.SourceFile, fileErrors []*ast.Diagnostic, formatOpts *DiagnosticsFormattingOptions) string {
333+
func prettyPathForFileError(file *ast.SourceFile, fileErrors []*ast.Diagnostic, formatOpts *FormattingOptions) string {
334334
line, _ := scanner.GetLineAndCharacterOfPosition(file, fileErrors[0].Loc().Pos())
335335
fileName := file.FileName()
336336
if tspath.PathIsAbsolute(fileName) && tspath.PathIsAbsolute(formatOpts.CurrentDirectory) {
@@ -344,21 +344,21 @@ func prettyPathForFileError(file *ast.SourceFile, fileErrors []*ast.Diagnostic,
344344
)
345345
}
346346

347-
func WriteFormatDiagnostics(output io.Writer, diagnostics []*ast.Diagnostic, formatOpts *DiagnosticsFormattingOptions) {
347+
func WriteFormatDiagnostics(output io.Writer, diagnostics []*ast.Diagnostic, formatOpts *FormattingOptions) {
348348
for _, diagnostic := range diagnostics {
349349
WriteFormatDiagnostic(output, diagnostic, formatOpts)
350350
}
351351
}
352352

353-
func WriteFormatDiagnostic(output io.Writer, diagnostic *ast.Diagnostic, formatOpts *DiagnosticsFormattingOptions) {
353+
func WriteFormatDiagnostic(output io.Writer, diagnostic *ast.Diagnostic, formatOpts *FormattingOptions) {
354354
if diagnostic.File() != nil {
355355
line, character := scanner.GetLineAndCharacterOfPosition(diagnostic.File(), diagnostic.Loc().Pos())
356356
fileName := diagnostic.File().FileName()
357357
relativeFileName := tspath.ConvertToRelativePath(fileName, formatOpts.ComparePathsOptions)
358358
fmt.Fprintf(output, "%s(%d,%d): ", relativeFileName, line+1, character+1)
359359
}
360360

361-
fmt.Fprintf(output, "%s TS%d: ", diagnostic.Category().String(), diagnostic.Code())
361+
fmt.Fprintf(output, "%s TS%d: ", diagnostic.Category().Name(), diagnostic.Code())
362362
WriteFlattenedDiagnosticMessage(output, diagnostic, formatOpts.NewLine)
363363
fmt.Fprint(output, formatOpts.NewLine)
364364
}

internal/testutil/baseline/baseline.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,35 @@ type Options struct {
1616
const NoContent = "<no content>"
1717

1818
func Run(t *testing.T, fileName string, actual string, opts Options) {
19-
writeComparison(t, actual, fileName, opts)
19+
writeComparison(t, actual, fileName, false /*useSubmodule*/, opts)
2020
}
2121

22-
func writeComparison(t *testing.T, actual string, relativeFileName string, opts Options) {
22+
func RunAgainstSubmodule(t *testing.T, fileName string, actual string, opts Options) {
23+
writeComparison(t, actual, fileName, true /*useSubmodule*/, opts)
24+
}
25+
26+
func writeComparison(t *testing.T, actual string, relativeFileName string, useSubmodule bool, opts Options) {
2327
if actual == "" {
24-
panic("The generated content was \"\". Return 'baseline.NoContent' if no baselining is required.")
28+
panic("the generated content was \"\". Return 'baseline.NoContent' if no baselining is required.")
2529
}
2630
var (
2731
localFileName string
2832
referenceFileName string
2933
)
3034

31-
localFileName = localPath(relativeFileName, opts.Subfolder)
32-
referenceFileName = referencePath(relativeFileName, opts.Subfolder)
33-
expected := getExpectedContent(relativeFileName, opts)
35+
if useSubmodule {
36+
localFileName = submoduleLocalPath(relativeFileName, opts.Subfolder)
37+
referenceFileName = submoduleReferencePath(relativeFileName, opts.Subfolder)
38+
} else {
39+
localFileName = localPath(relativeFileName, opts.Subfolder)
40+
referenceFileName = referencePath(relativeFileName, opts.Subfolder)
41+
}
42+
43+
expected := NoContent
44+
if content, err := os.ReadFile(referenceFileName); err == nil {
45+
expected = string(content)
46+
}
47+
3448
if _, err := os.Stat(localFileName); err == nil {
3549
if err := os.Remove(localFileName); err != nil {
3650
t.Fatal(fmt.Errorf("failed to remove the local baseline file %s: %w", localFileName, err))
@@ -49,26 +63,31 @@ func writeComparison(t *testing.T, actual string, relativeFileName string, opts
4963
}
5064

5165
if _, err := os.Stat(referenceFileName); err != nil {
52-
t.Errorf("New baseline created at %s.", localFileName)
66+
if useSubmodule {
67+
t.Errorf("the baseline file %s does not exist in the TypeScript submodule", referenceFileName)
68+
} else {
69+
t.Errorf("new baseline created at %s.", localFileName)
70+
}
71+
} else if useSubmodule {
72+
t.Errorf("the baseline file %s does not match the reference in the TypeScript submodule", relativeFileName)
5373
} else {
54-
t.Errorf("The baseline file %s has changed. (Run `hereby baseline-accept` if the new baseline is correct.)", relativeFileName)
74+
t.Errorf("the baseline file %s has changed. (Run `hereby baseline-accept` if the new baseline is correct.)", relativeFileName)
5575
}
5676
}
5777
}
5878

59-
func getExpectedContent(relativeFileName string, opts Options) string {
60-
refFileName := referencePath(relativeFileName, opts.Subfolder)
61-
expected := NoContent
62-
if content, err := os.ReadFile(refFileName); err == nil {
63-
expected = string(content)
64-
}
65-
return expected
66-
}
67-
6879
func localPath(fileName string, subfolder string) string {
6980
return filepath.Join(repo.TestDataPath, "baselines", "local", subfolder, fileName)
7081
}
7182

83+
func submoduleLocalPath(fileName string, subfolder string) string {
84+
return filepath.Join(repo.TestDataPath, "baselines", "tmp", subfolder, fileName)
85+
}
86+
7287
func referencePath(fileName string, subfolder string) string {
7388
return filepath.Join(repo.TestDataPath, "baselines", "reference", subfolder, fileName)
7489
}
90+
91+
func submoduleReferencePath(fileName string, subfolder string) string {
92+
return filepath.Join(repo.TypeScriptSubmodulePath, "tests", "baselines", "reference", subfolder, fileName)
93+
}

0 commit comments

Comments
 (0)