Skip to content

Commit f822b4d

Browse files
authored
Further test and fix compiler options (#855)
1 parent c76b10e commit f822b4d

11 files changed

+103
-21
lines changed

internal/binder/binder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func BenchmarkBind(b *testing.B) {
2727
sourceFiles[i] = parser.ParseSourceFile(fileName, path, sourceText, core.ScriptTargetESNext, scanner.JSDocParsingModeParseAll)
2828
}
2929

30-
compilerOptions := &core.CompilerOptions{Target: core.ScriptTargetESNext, ModuleKind: core.ModuleKindNodeNext}
30+
compilerOptions := &core.CompilerOptions{Target: core.ScriptTargetESNext, Module: core.ModuleKindNodeNext}
3131
sourceAffecting := compilerOptions.SourceFileAffecting()
3232

3333
// The above parses do a lot of work; ensure GC is finished before we start collecting performance data.

internal/core/compileroptions.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ type CompilerOptions struct {
5656
Lib []string `json:"lib,omitzero"`
5757
Locale string `json:"locale,omitzero"`
5858
MapRoot string `json:"mapRoot,omitzero"`
59-
ModuleKind ModuleKind `json:"module,omitzero"`
59+
Module ModuleKind `json:"module,omitzero"`
6060
ModuleResolution ModuleResolutionKind `json:"moduleResolution,omitzero"`
6161
ModuleSuffixes []string `json:"moduleSuffixes,omitzero"`
62-
ModuleDetection ModuleDetectionKind `json:"moduleDetectionKind,omitzero"`
62+
ModuleDetection ModuleDetectionKind `json:"moduleDetection,omitzero"`
6363
NewLine NewLineKind `json:"newLine,omitzero"`
6464
NoEmit Tristate `json:"noEmit,omitzero"`
6565
NoCheck Tristate `json:"noCheck,omitzero"`
@@ -153,8 +153,8 @@ func (options *CompilerOptions) GetEmitScriptTarget() ScriptTarget {
153153
}
154154

155155
func (options *CompilerOptions) GetEmitModuleKind() ModuleKind {
156-
if options.ModuleKind != ModuleKindNone {
157-
return options.ModuleKind
156+
if options.Module != ModuleKindNone {
157+
return options.Module
158158
}
159159
if options.Target >= ScriptTargetES2015 {
160160
return ModuleKindES2015

internal/transformers/commonjsmodule_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ exports.a = a;`,
10191019
t.Parallel()
10201020

10211021
compilerOptions := rec.options
1022-
compilerOptions.ModuleKind = core.ModuleKindCommonJS
1022+
compilerOptions.Module = core.ModuleKindCommonJS
10231023
sourceFileAffecting := compilerOptions.SourceFileAffecting()
10241024

10251025
file := parsetestutil.ParseTypeScript(rec.input, rec.jsx)

internal/transformers/esmodule.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func (tx *ESModuleTransformer) visitExportDeclaration(node *ast.ExportDeclaratio
197197
}
198198

199199
updatedModuleSpecifier := rewriteModuleSpecifier(tx.emitContext, node.ModuleSpecifier, tx.compilerOptions)
200-
if tx.compilerOptions.ModuleKind > core.ModuleKindES2015 || node.ExportClause == nil || !ast.IsNamespaceExport(node.ExportClause) {
200+
if tx.compilerOptions.Module > core.ModuleKindES2015 || node.ExportClause == nil || !ast.IsNamespaceExport(node.ExportClause) {
201201
// Either ill-formed or don't need to be transformed.
202202
return tx.factory.UpdateExportDeclaration(
203203
node,

internal/transformers/esmodule_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,31 @@ func TestESModuleTransformer(t *testing.T) {
5858
output: `import { createRequire as _createRequire } from "module";
5959
const __require = _createRequire(import.meta.url);
6060
const x = __require("other");`,
61-
options: core.CompilerOptions{ModuleKind: core.ModuleKindNode16},
61+
options: core.CompilerOptions{Module: core.ModuleKindNode16},
6262
},
6363
{
6464
title: "ImportEqualsDeclaration#3",
6565
input: `import x = require("./other.ts")`,
6666
output: `import { createRequire as _createRequire } from "module";
6767
const __require = _createRequire(import.meta.url);
6868
const x = __require("./other.js");`,
69-
options: core.CompilerOptions{ModuleKind: core.ModuleKindNode16, RewriteRelativeImportExtensions: core.TSTrue},
69+
options: core.CompilerOptions{Module: core.ModuleKindNode16, RewriteRelativeImportExtensions: core.TSTrue},
7070
},
7171
{
7272
title: "ImportEqualsDeclaration#4",
7373
input: `import x = require("./other.tsx")`,
7474
output: `import { createRequire as _createRequire } from "module";
7575
const __require = _createRequire(import.meta.url);
7676
const x = __require("./other.js");`,
77-
options: core.CompilerOptions{ModuleKind: core.ModuleKindNode16, RewriteRelativeImportExtensions: core.TSTrue},
77+
options: core.CompilerOptions{Module: core.ModuleKindNode16, RewriteRelativeImportExtensions: core.TSTrue},
7878
},
7979
{
8080
title: "ImportEqualsDeclaration#5",
8181
input: `import x = require("./other.tsx")`,
8282
output: `import { createRequire as _createRequire } from "module";
8383
const __require = _createRequire(import.meta.url);
8484
const x = __require("./other.jsx");`,
85-
options: core.CompilerOptions{ModuleKind: core.ModuleKindNode16, RewriteRelativeImportExtensions: core.TSTrue, Jsx: core.JsxEmitPreserve},
85+
options: core.CompilerOptions{Module: core.ModuleKindNode16, RewriteRelativeImportExtensions: core.TSTrue, Jsx: core.JsxEmitPreserve},
8686
},
8787
{
8888
title: "ImportEqualsDeclaration#6",
@@ -91,7 +91,7 @@ const x = __require("./other.jsx");`,
9191
const __require = _createRequire(import.meta.url);
9292
const x = __require("other");
9393
export { x };`,
94-
options: core.CompilerOptions{ModuleKind: core.ModuleKindNode16},
94+
options: core.CompilerOptions{Module: core.ModuleKindNode16},
9595
},
9696

9797
// ExportAssignment
@@ -104,7 +104,7 @@ export { x };`,
104104
title: "ExportAssignment#2",
105105
input: `export = x`,
106106
output: `module.exports = x;`,
107-
options: core.CompilerOptions{ModuleKind: core.ModuleKindPreserve},
107+
options: core.CompilerOptions{Module: core.ModuleKindPreserve},
108108
},
109109

110110
// ExportDeclaration
@@ -123,7 +123,7 @@ export { x };`,
123123
title: "ExportDeclaration#3",
124124
input: `export * as x from "other";`,
125125
output: `export * as x from "other";`,
126-
options: core.CompilerOptions{ModuleKind: core.ModuleKindESNext},
126+
options: core.CompilerOptions{Module: core.ModuleKindESNext},
127127
},
128128
{
129129
title: "ExportDeclaration#4",
@@ -201,7 +201,7 @@ import(x);`,
201201
output: `import { __rewriteRelativeImportExtension } from "tslib";
202202
export {};
203203
import(__rewriteRelativeImportExtension(x));`,
204-
options: core.CompilerOptions{ModuleKind: core.ModuleKindESNext, RewriteRelativeImportExtensions: core.TSTrue, ImportHelpers: core.TSTrue},
204+
options: core.CompilerOptions{Module: core.ModuleKindESNext, RewriteRelativeImportExtensions: core.TSTrue, ImportHelpers: core.TSTrue},
205205
},
206206
{
207207
title: "CallExpression#7",
@@ -212,7 +212,7 @@ var __rewriteRelativeImportExtension;`,
212212
export {};
213213
import(__rewriteRelativeImportExtension_1(x));
214214
var __rewriteRelativeImportExtension;`,
215-
options: core.CompilerOptions{ModuleKind: core.ModuleKindESNext, RewriteRelativeImportExtensions: core.TSTrue, ImportHelpers: core.TSTrue},
215+
options: core.CompilerOptions{Module: core.ModuleKindESNext, RewriteRelativeImportExtensions: core.TSTrue, ImportHelpers: core.TSTrue},
216216
},
217217
}
218218
for _, rec := range data {

internal/tsoptions/decls_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package tsoptions_test
2+
3+
import (
4+
"reflect"
5+
"strings"
6+
"testing"
7+
8+
"github.com/microsoft/typescript-go/internal/core"
9+
"github.com/microsoft/typescript-go/internal/tsoptions"
10+
)
11+
12+
func TestCompilerOptionsDeclaration(t *testing.T) {
13+
t.Parallel()
14+
15+
decls := make(map[string]*tsoptions.CommandLineOption)
16+
17+
for _, decl := range tsoptions.OptionsDeclarations {
18+
decls[strings.ToLower(decl.Name)] = decl
19+
}
20+
21+
internalOptions := []string{
22+
"allowNonTsExtensions",
23+
"build",
24+
"configFilePath",
25+
"noDtsResolution",
26+
"noEmitForJsFiles",
27+
"pathsBasePath",
28+
"suppressOutputPathCheck",
29+
"tscBuild",
30+
}
31+
32+
internalOptionsMap := make(map[string]string)
33+
for _, opt := range internalOptions {
34+
internalOptionsMap[strings.ToLower(opt)] = opt
35+
}
36+
37+
compilerOptionsType := reflect.TypeFor[core.CompilerOptions]()
38+
for i := range compilerOptionsType.NumField() {
39+
field := compilerOptionsType.Field(i)
40+
lowerName := strings.ToLower(field.Name)
41+
42+
decl := decls[lowerName]
43+
if decl == nil {
44+
if name, ok := internalOptionsMap[lowerName]; ok {
45+
checkCompilerOptionJsonTagName(t, field, name)
46+
continue
47+
}
48+
t.Errorf("CompilerOptions.%s has no options declaration", field.Name)
49+
continue
50+
}
51+
delete(decls, lowerName)
52+
53+
checkCompilerOptionJsonTagName(t, field, decl.Name)
54+
}
55+
56+
skippedOptions := []string{
57+
"charset",
58+
"noImplicitUseStrict",
59+
"noStrictGenericChecks",
60+
"plugins",
61+
"preserveValueImports",
62+
"suppressExcessPropertyErrors",
63+
"suppressImplicitAnyIndexErrors",
64+
}
65+
66+
for _, opt := range skippedOptions {
67+
delete(decls, strings.ToLower(opt))
68+
}
69+
70+
for _, decl := range decls {
71+
t.Errorf("Option declaration %s is not present in CompilerOptions", decl.Name)
72+
}
73+
}
74+
75+
func checkCompilerOptionJsonTagName(t *testing.T, field reflect.StructField, name string) {
76+
t.Helper()
77+
want := name + ",omitzero"
78+
got := field.Tag.Get("json")
79+
if got != want {
80+
t.Errorf("Field %s has json tag %s, but the option declaration has name %s", field.Name, got, want)
81+
}
82+
}

internal/tsoptions/parsinghelpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func parseCompilerOptions(key string, value any, allOptions *core.CompilerOption
259259
case "mapRoot":
260260
allOptions.MapRoot = parseString(value)
261261
case "module":
262-
allOptions.ModuleKind = value.(core.ModuleKind)
262+
allOptions.Module = value.(core.ModuleKind)
263263
case "moduleDetectionKind":
264264
allOptions.ModuleDetection = value.(core.ModuleDetectionKind)
265265
case "moduleResolution":

internal/tsoptions/tsconfigparsing_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ func TestParseSrcCompiler(t *testing.T) {
732732
opts := parseConfigFileContent.CompilerOptions()
733733
assert.DeepEqual(t, opts, &core.CompilerOptions{
734734
Lib: []string{"lib.es2020.d.ts"},
735-
ModuleKind: core.ModuleKindNodeNext,
735+
Module: core.ModuleKindNodeNext,
736736
ModuleResolution: core.ModuleResolutionKindNodeNext,
737737
NewLine: core.NewLineKindLF,
738738
OutDir: tspath.NormalizeSlashes(filepath.Join(repo.TypeScriptSubmodulePath, "built", "local")),

testdata/baselines/reference/config/tsconfigParsing/parses tsconfig with compilerOptions, files, include, and exclude with json api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ CompilerOptions::
3939
"jsx": 3,
4040
"module": 99,
4141
"moduleResolution": 100,
42-
"moduleDetectionKind": 1,
42+
"moduleDetection": 1,
4343
"noImplicitAny": true,
4444
"outDir": "/apath/dist",
4545
"paths": {

testdata/baselines/reference/config/tsconfigParsing/parses tsconfig with compilerOptions, files, include, and exclude with jsonSourceFile api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ CompilerOptions::
3939
"jsx": 3,
4040
"module": 99,
4141
"moduleResolution": 100,
42-
"moduleDetectionKind": 1,
42+
"moduleDetection": 1,
4343
"noImplicitAny": true,
4444
"outDir": "/apath/dist",
4545
"paths": {

testdata/baselines/reference/tsc/commandLine/Parse-enum-type-options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ParsedCommandLine::{
1111
"jsx": 3,
1212
"module": 199,
1313
"moduleResolution": 99,
14-
"moduleDetectionKind": 1,
14+
"moduleDetection": 1,
1515
"newLine": 1,
1616
"target": 99
1717
},

0 commit comments

Comments
 (0)