Skip to content

Finish implementing sourceFileMayBeEmitted, JSON emit #1245

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
41 changes: 37 additions & 4 deletions internal/compiler/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/microsoft/typescript-go/internal/stringutil"
"github.com/microsoft/typescript-go/internal/transformers"
"github.com/microsoft/typescript-go/internal/transformers/declarations"
"github.com/microsoft/typescript-go/internal/tsoptions"
"github.com/microsoft/typescript-go/internal/tspath"
)

Expand Down Expand Up @@ -296,14 +297,29 @@ func (e *emitter) getSourceMappingURL(mapOptions *core.CompilerOptions, sourceMa
return stringutil.EncodeURI(sourceMapFile)
}

func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host printer.EmitHost, forceDtsEmit bool) bool {
// !!! Js files are emitted only if option is enabled
type SourceFileMayBeEmittedHost interface {
Options() *core.CompilerOptions
GetOutputAndProjectReference(path tspath.Path) *tsoptions.OutputDtsAndProjectReference
IsSourceFileFromExternalLibrary(file *ast.SourceFile) bool
GetCurrentDirectory() string
UseCaseSensitiveFileNames() bool
}

func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host SourceFileMayBeEmittedHost, forceDtsEmit bool) bool {
// TODO: move this to outputpaths?

options := host.Options()
// Js files are emitted only if option is enabled
if options.NoEmitForJsFiles.IsTrue() && ast.IsSourceFileJS(sourceFile) {
return false
}

// Declaration files are not emitted
if sourceFile.IsDeclarationFile {
return false
}

// Source file from node_modules are not emitted
if host.IsSourceFileFromExternalLibrary(sourceFile) {
return false
}
Expand All @@ -313,6 +329,7 @@ func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host printer.EmitHost, f
return true
}

// Check other conditions for file emit
// Source files from referenced projects are not emitted
if host.GetOutputAndProjectReference(sourceFile.Path()) != nil {
return false
Expand All @@ -323,8 +340,24 @@ func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host printer.EmitHost, f
return true
}

// !!! Should JSON input files be emitted
return false
// Json file is not emitted if outDir is not specified
if options.OutDir == "" {
return false
}

// Otherwise if rootDir or composite config file, we know common sourceDir and can check if file would be emitted in same location
if options.RootDir != "" || (options.Composite.IsTrue() && options.ConfigFilePath != "") {
commonDir := tspath.GetNormalizedAbsolutePath(outputpaths.GetCommonSourceDirectory(options, func() []string { return nil }, host.GetCurrentDirectory(), host.UseCaseSensitiveFileNames()), host.GetCurrentDirectory())
outputPath := outputpaths.GetSourceFilePathInNewDirWorker(sourceFile.FileName(), options.OutDir, host.GetCurrentDirectory(), commonDir, host.UseCaseSensitiveFileNames())
if tspath.ComparePaths(sourceFile.FileName(), outputPath, tspath.ComparePathsOptions{
UseCaseSensitiveFileNames: host.UseCaseSensitiveFileNames(),
CurrentDirectory: host.GetCurrentDirectory(),
}) == 0 {
return false
}
}

return true
}

func getSourceFilesToEmit(host printer.EmitHost, targetSourceFile *ast.SourceFile, forceDtsEmit bool) []*ast.SourceFile {
Expand Down
4 changes: 2 additions & 2 deletions internal/outputpaths/outputpaths.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func GetDeclarationEmitOutputFilePath(file string, options *core.CompilerOptions

var path string
if outputDir != nil {
path = getSourceFilePathInNewDirWorker(file, *outputDir, host.GetCurrentDirectory(), host.CommonSourceDirectory(), host.UseCaseSensitiveFileNames())
path = GetSourceFilePathInNewDirWorker(file, *outputDir, host.GetCurrentDirectory(), host.CommonSourceDirectory(), host.UseCaseSensitiveFileNames())
} else {
path = file
}
Expand Down Expand Up @@ -154,7 +154,7 @@ func getOutputPathWithoutChangingExtension(inputFileName string, outputDirectory
return inputFileName
}

func getSourceFilePathInNewDirWorker(fileName string, newDirPath string, currentDirectory string, commonSourceDirectory string, useCaseSensitiveFileNames bool) string {
func GetSourceFilePathInNewDirWorker(fileName string, newDirPath string, currentDirectory string, commonSourceDirectory string, useCaseSensitiveFileNames bool) string {
sourceFilePath := tspath.GetNormalizedAbsolutePath(fileName, currentDirectory)
commonDir := tspath.GetCanonicalFileName(commonSourceDirectory, useCaseSensitiveFileNames)
canonFile := tspath.GetCanonicalFileName(sourceFilePath, useCaseSensitiveFileNames)
Expand Down
5 changes: 4 additions & 1 deletion internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3175,7 +3175,10 @@ func (p *Printer) emitEmptyStatement(node *ast.EmptyStatement, isEmbeddedStateme
func (p *Printer) emitExpressionStatement(node *ast.ExpressionStatement) {
state := p.enterNode(node.AsNode())

if isImmediatelyInvokedFunctionExpressionOrArrowFunction(node.Expression) {
if p.currentSourceFile != nil && p.currentSourceFile.ScriptKind == core.ScriptKindJSON {
Copy link
Preview

Copilot AI Jun 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSON-specific branch uses a hack comment referencing an undefined parenthesizerRule. Consider refactoring this block to either integrate a proper parenthesizing solution for JSON files or add a more descriptive comment on why the current approach is necessary.

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, it's that simple

// !!! In strada, this was handled by an undefined parenthesizerRule, so this is a hack.
p.emitExpression(node.Expression, ast.OperatorPrecedenceComma)
} else if isImmediatelyInvokedFunctionExpressionOrArrowFunction(node.Expression) {
// !!! introduce parentheses around callee
p.emitExpression(node.Expression, ast.OperatorPrecedenceParentheses)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import * as j from "./j.json";
{}


//// [j.json]
{}
//// [a.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ function fn(arg: Foo[]) { }
]
}

//// [data.json]
{
"foo": [
{
"bool": true,
"str": "123"
}
]
}
//// [index.js]
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
--- old.jsonFileImportChecksCallCorrectlyTwice.js
+++ new.jsonFileImportChecksCallCorrectlyTwice.js
@@= skipped -20, +20 lines =@@
]
}

-//// [data.json]
-{
- "foo": [
- {
- "bool": true,
- "str": "123"
- }
- ]
-}
//// [index.js]
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
@@= skipped -35, +35 lines =@@
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
import foo from "./foo.json";
console.log(foo.ios);

//// [/bin/foo.ios.json]
{
"ios": "platform ios"
}
//// [/bin/index.js]
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
--- old.moduleResolutionWithSuffixes_one_jsonModule.js
+++ new.moduleResolutionWithSuffixes_one_jsonModule.js
@@= skipped -12, +12 lines =@@
import foo from "./foo.json";
console.log(foo.ios);

-//// [/bin/foo.ios.json]
-{
- "ios": "platform ios"
-}
//// [/bin/index.js]
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
@@= skipped -22, +22 lines =@@
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ if (x) {
"b": "hello"
}

//// [out/b.json]
{
"a": true,
"b": "hello"
}
//// [out/file1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
--- old.requireOfJsonFile.js
+++ new.requireOfJsonFile.js
@@= skipped -14, +14 lines =@@
"b": "hello"
}

-//// [out/b.json]
-{
- "a": true,
- "b": "hello"
-}
@@= skipped -22, +22 lines =@@
//// [out/file1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ booleanLiteral = g[0];
//// [g.json]
[true, false]

//// [out/b.json]
{
"a": true,
"b": "hello",
"c": null,
"d": false
}
//// [out/c.json]
["a", null, "string"]
//// [out/d.json]
"dConfig"
//// [out/e.json]
-10
//// [out/f.json]
[-10, 30]
//// [out/g.json]
[true, false]
//// [out/file1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
--- old.requireOfJsonFileTypes.js
+++ new.requireOfJsonFileTypes.js
@@= skipped -44, +44 lines =@@
//// [g.json]
[true, false]

-//// [out/b.json]
-{
- "a": true,
- "b": "hello",
- "c": null,
- "d": false
-}
-//// [out/c.json]
-["a", null, "string"]
-//// [out/d.json]
-"dConfig"
-//// [out/e.json]
--10
-//// [out/f.json]
-[-10, 30]
-//// [out/g.json]
-[true, false]
@@= skipped -64, +64 lines =@@
//// [out/file1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ if (x) {
"b": "hello"
}

//// [b.json]
{
"a": true,
"b": "hello"
}
//// [file1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
--- old.requireOfJsonFileWithAlwaysStrictWithoutErrors.js
+++ new.requireOfJsonFileWithAlwaysStrictWithoutErrors.js
@@= skipped -14, +14 lines =@@
"b": "hello"
}

-//// [b.json]
-{
- "a": true,
- "b": "hello"
-}
@@= skipped -22, +22 lines =@@
//// [file1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ if (x) {
"b": "hello"
}

//// [out/b.json]
{
"a": true,
"b": "hello"
}
//// [out/file1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
--- old.requireOfJsonFileWithAmd.js
+++ new.requireOfJsonFileWithAmd.js
@@= skipped -14, +14 lines =@@
@@= skipped -20, +20 lines =@@
"b": "hello"
}

-//// [out/b.json]
-{
- "a": true,
- "b": "hello"
-}
//// [out/file1.js]
-define(["require", "exports", "./b", "./b.json"], function (require, exports, b1, b2) {
- "use strict";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ if (x) {
[a]: 10
}

//// [b.json]
{
[a]: 10
}
//// [file1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
--- old.requireOfJsonFileWithComputedPropertyName.js
+++ new.requireOfJsonFileWithComputedPropertyName.js
@@= skipped -12, +12 lines =@@
[a]: 10
}

-//// [b.json]
-{
- [a]: 10
-}
@@= skipped -19, +19 lines =@@
//// [file1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ if (x) {
"b": "hello"
}

//// [out/b.json]
{
"a": true,
"b": "hello"
}
//// [out/file1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
--- old.requireOfJsonFileWithDeclaration.js
+++ new.requireOfJsonFileWithDeclaration.js
@@= skipped -14, +14 lines =@@
"b": "hello"
}

-//// [out/b.json]
-{
- "a": true,
- "b": "hello"
-}
@@= skipped -22, +22 lines =@@
//// [out/file1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ if (x) {
{
}

//// [out/b.json]
{}
//// [out/file1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Expand Down
Loading