Skip to content

Further test and fix compiler options #855

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

Merged
merged 2 commits into from
May 8, 2025
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
2 changes: 1 addition & 1 deletion internal/binder/binder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func BenchmarkBind(b *testing.B) {
sourceFiles[i] = parser.ParseSourceFile(fileName, path, sourceText, core.ScriptTargetESNext, scanner.JSDocParsingModeParseAll)
}

compilerOptions := &core.CompilerOptions{Target: core.ScriptTargetESNext, ModuleKind: core.ModuleKindNodeNext}
compilerOptions := &core.CompilerOptions{Target: core.ScriptTargetESNext, Module: core.ModuleKindNodeNext}
sourceAffecting := compilerOptions.SourceFileAffecting()

// The above parses do a lot of work; ensure GC is finished before we start collecting performance data.
Expand Down
8 changes: 4 additions & 4 deletions internal/core/compileroptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ type CompilerOptions struct {
Lib []string `json:"lib,omitzero"`
Locale string `json:"locale,omitzero"`
MapRoot string `json:"mapRoot,omitzero"`
ModuleKind ModuleKind `json:"module,omitzero"`
Module ModuleKind `json:"module,omitzero"`
ModuleResolution ModuleResolutionKind `json:"moduleResolution,omitzero"`
ModuleSuffixes []string `json:"moduleSuffixes,omitzero"`
ModuleDetection ModuleDetectionKind `json:"moduleDetectionKind,omitzero"`
ModuleDetection ModuleDetectionKind `json:"moduleDetection,omitzero"`
NewLine NewLineKind `json:"newLine,omitzero"`
NoEmit Tristate `json:"noEmit,omitzero"`
NoCheck Tristate `json:"noCheck,omitzero"`
Expand Down Expand Up @@ -153,8 +153,8 @@ func (options *CompilerOptions) GetEmitScriptTarget() ScriptTarget {
}

func (options *CompilerOptions) GetEmitModuleKind() ModuleKind {
if options.ModuleKind != ModuleKindNone {
return options.ModuleKind
if options.Module != ModuleKindNone {
return options.Module
}
if options.Target >= ScriptTargetES2015 {
return ModuleKindES2015
Expand Down
2 changes: 1 addition & 1 deletion internal/transformers/commonjsmodule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ exports.a = a;`,
t.Parallel()

compilerOptions := rec.options
compilerOptions.ModuleKind = core.ModuleKindCommonJS
compilerOptions.Module = core.ModuleKindCommonJS
sourceFileAffecting := compilerOptions.SourceFileAffecting()

file := parsetestutil.ParseTypeScript(rec.input, rec.jsx)
Expand Down
2 changes: 1 addition & 1 deletion internal/transformers/esmodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (tx *ESModuleTransformer) visitExportDeclaration(node *ast.ExportDeclaratio
}

updatedModuleSpecifier := rewriteModuleSpecifier(tx.emitContext, node.ModuleSpecifier, tx.compilerOptions)
if tx.compilerOptions.ModuleKind > core.ModuleKindES2015 || node.ExportClause == nil || !ast.IsNamespaceExport(node.ExportClause) {
if tx.compilerOptions.Module > core.ModuleKindES2015 || node.ExportClause == nil || !ast.IsNamespaceExport(node.ExportClause) {
// Either ill-formed or don't need to be transformed.
return tx.factory.UpdateExportDeclaration(
node,
Expand Down
18 changes: 9 additions & 9 deletions internal/transformers/esmodule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,31 @@ func TestESModuleTransformer(t *testing.T) {
output: `import { createRequire as _createRequire } from "module";
const __require = _createRequire(import.meta.url);
const x = __require("other");`,
options: core.CompilerOptions{ModuleKind: core.ModuleKindNode16},
options: core.CompilerOptions{Module: core.ModuleKindNode16},
},
{
title: "ImportEqualsDeclaration#3",
input: `import x = require("./other.ts")`,
output: `import { createRequire as _createRequire } from "module";
const __require = _createRequire(import.meta.url);
const x = __require("./other.js");`,
options: core.CompilerOptions{ModuleKind: core.ModuleKindNode16, RewriteRelativeImportExtensions: core.TSTrue},
options: core.CompilerOptions{Module: core.ModuleKindNode16, RewriteRelativeImportExtensions: core.TSTrue},
},
{
title: "ImportEqualsDeclaration#4",
input: `import x = require("./other.tsx")`,
output: `import { createRequire as _createRequire } from "module";
const __require = _createRequire(import.meta.url);
const x = __require("./other.js");`,
options: core.CompilerOptions{ModuleKind: core.ModuleKindNode16, RewriteRelativeImportExtensions: core.TSTrue},
options: core.CompilerOptions{Module: core.ModuleKindNode16, RewriteRelativeImportExtensions: core.TSTrue},
},
{
title: "ImportEqualsDeclaration#5",
input: `import x = require("./other.tsx")`,
output: `import { createRequire as _createRequire } from "module";
const __require = _createRequire(import.meta.url);
const x = __require("./other.jsx");`,
options: core.CompilerOptions{ModuleKind: core.ModuleKindNode16, RewriteRelativeImportExtensions: core.TSTrue, Jsx: core.JsxEmitPreserve},
options: core.CompilerOptions{Module: core.ModuleKindNode16, RewriteRelativeImportExtensions: core.TSTrue, Jsx: core.JsxEmitPreserve},
},
{
title: "ImportEqualsDeclaration#6",
Expand All @@ -91,7 +91,7 @@ const x = __require("./other.jsx");`,
const __require = _createRequire(import.meta.url);
const x = __require("other");
export { x };`,
options: core.CompilerOptions{ModuleKind: core.ModuleKindNode16},
options: core.CompilerOptions{Module: core.ModuleKindNode16},
},

// ExportAssignment
Expand All @@ -104,7 +104,7 @@ export { x };`,
title: "ExportAssignment#2",
input: `export = x`,
output: `module.exports = x;`,
options: core.CompilerOptions{ModuleKind: core.ModuleKindPreserve},
options: core.CompilerOptions{Module: core.ModuleKindPreserve},
},

// ExportDeclaration
Expand All @@ -123,7 +123,7 @@ export { x };`,
title: "ExportDeclaration#3",
input: `export * as x from "other";`,
output: `export * as x from "other";`,
options: core.CompilerOptions{ModuleKind: core.ModuleKindESNext},
options: core.CompilerOptions{Module: core.ModuleKindESNext},
},
{
title: "ExportDeclaration#4",
Expand Down Expand Up @@ -201,7 +201,7 @@ import(x);`,
output: `import { __rewriteRelativeImportExtension } from "tslib";
export {};
import(__rewriteRelativeImportExtension(x));`,
options: core.CompilerOptions{ModuleKind: core.ModuleKindESNext, RewriteRelativeImportExtensions: core.TSTrue, ImportHelpers: core.TSTrue},
options: core.CompilerOptions{Module: core.ModuleKindESNext, RewriteRelativeImportExtensions: core.TSTrue, ImportHelpers: core.TSTrue},
},
{
title: "CallExpression#7",
Expand All @@ -212,7 +212,7 @@ var __rewriteRelativeImportExtension;`,
export {};
import(__rewriteRelativeImportExtension_1(x));
var __rewriteRelativeImportExtension;`,
options: core.CompilerOptions{ModuleKind: core.ModuleKindESNext, RewriteRelativeImportExtensions: core.TSTrue, ImportHelpers: core.TSTrue},
options: core.CompilerOptions{Module: core.ModuleKindESNext, RewriteRelativeImportExtensions: core.TSTrue, ImportHelpers: core.TSTrue},
},
}
for _, rec := range data {
Expand Down
82 changes: 82 additions & 0 deletions internal/tsoptions/decls_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package tsoptions_test

import (
"reflect"
"strings"
"testing"

"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/tsoptions"
)

func TestCompilerOptionsDeclaration(t *testing.T) {
t.Parallel()

decls := make(map[string]*tsoptions.CommandLineOption)

for _, decl := range tsoptions.OptionsDeclarations {
decls[strings.ToLower(decl.Name)] = decl
}

internalOptions := []string{
"allowNonTsExtensions",
"build",
"configFilePath",
"noDtsResolution",
"noEmitForJsFiles",
"pathsBasePath",
"suppressOutputPathCheck",
"tscBuild",
}

internalOptionsMap := make(map[string]string)
for _, opt := range internalOptions {
internalOptionsMap[strings.ToLower(opt)] = opt
}

compilerOptionsType := reflect.TypeFor[core.CompilerOptions]()
for i := range compilerOptionsType.NumField() {
field := compilerOptionsType.Field(i)
lowerName := strings.ToLower(field.Name)

decl := decls[lowerName]
if decl == nil {
if name, ok := internalOptionsMap[lowerName]; ok {
checkCompilerOptionJsonTagName(t, field, name)
continue
}
t.Errorf("CompilerOptions.%s has no options declaration", field.Name)
continue
}
delete(decls, lowerName)

checkCompilerOptionJsonTagName(t, field, decl.Name)
}

skippedOptions := []string{
"charset",
"noImplicitUseStrict",
"noStrictGenericChecks",
"plugins",
"preserveValueImports",
"suppressExcessPropertyErrors",
"suppressImplicitAnyIndexErrors",
}

for _, opt := range skippedOptions {
delete(decls, strings.ToLower(opt))
}

for _, decl := range decls {
t.Errorf("Option declaration %s is not present in CompilerOptions", decl.Name)
}
}

func checkCompilerOptionJsonTagName(t *testing.T, field reflect.StructField, name string) {
t.Helper()
want := name + ",omitzero"
got := field.Tag.Get("json")
if got != want {
t.Errorf("Field %s has json tag %s, but the option declaration has name %s", field.Name, got, want)
}
}
2 changes: 1 addition & 1 deletion internal/tsoptions/parsinghelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func parseCompilerOptions(key string, value any, allOptions *core.CompilerOption
case "mapRoot":
allOptions.MapRoot = parseString(value)
case "module":
allOptions.ModuleKind = value.(core.ModuleKind)
allOptions.Module = value.(core.ModuleKind)
case "moduleDetectionKind":
allOptions.ModuleDetection = value.(core.ModuleDetectionKind)
case "moduleResolution":
Expand Down
2 changes: 1 addition & 1 deletion internal/tsoptions/tsconfigparsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ func TestParseSrcCompiler(t *testing.T) {
opts := parseConfigFileContent.CompilerOptions()
assert.DeepEqual(t, opts, &core.CompilerOptions{
Lib: []string{"lib.es2020.d.ts"},
ModuleKind: core.ModuleKindNodeNext,
Module: core.ModuleKindNodeNext,
ModuleResolution: core.ModuleResolutionKindNodeNext,
NewLine: core.NewLineKindLF,
OutDir: tspath.NormalizeSlashes(filepath.Join(repo.TypeScriptSubmodulePath, "built", "local")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ CompilerOptions::
"jsx": 3,
"module": 99,
"moduleResolution": 100,
"moduleDetectionKind": 1,
"moduleDetection": 1,
"noImplicitAny": true,
"outDir": "/apath/dist",
"paths": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ CompilerOptions::
"jsx": 3,
"module": 99,
"moduleResolution": 100,
"moduleDetectionKind": 1,
"moduleDetection": 1,
"noImplicitAny": true,
"outDir": "/apath/dist",
"paths": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ParsedCommandLine::{
"jsx": 3,
"module": 199,
"moduleResolution": 99,
"moduleDetectionKind": 1,
"moduleDetection": 1,
"newLine": 1,
"target": 99
},
Expand Down