Skip to content

Commit 9ec05e4

Browse files
authored
Finish implementing sourceFileMayBeEmitted, JSON emit (#1245)
1 parent 6a14142 commit 9ec05e4

File tree

71 files changed

+278
-388
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+278
-388
lines changed

internal/compiler/emitter.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/microsoft/typescript-go/internal/transformers/jsxtransforms"
1818
"github.com/microsoft/typescript-go/internal/transformers/moduletransforms"
1919
"github.com/microsoft/typescript-go/internal/transformers/tstransforms"
20+
"github.com/microsoft/typescript-go/internal/tsoptions"
2021
"github.com/microsoft/typescript-go/internal/tspath"
2122
)
2223

@@ -368,14 +369,29 @@ func (e *emitter) getSourceMappingURL(mapOptions *core.CompilerOptions, sourceMa
368369
return stringutil.EncodeURI(sourceMapFile)
369370
}
370371

371-
func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host printer.EmitHost, forceDtsEmit bool) bool {
372-
// !!! Js files are emitted only if option is enabled
372+
type SourceFileMayBeEmittedHost interface {
373+
Options() *core.CompilerOptions
374+
GetOutputAndProjectReference(path tspath.Path) *tsoptions.OutputDtsAndProjectReference
375+
IsSourceFileFromExternalLibrary(file *ast.SourceFile) bool
376+
GetCurrentDirectory() string
377+
UseCaseSensitiveFileNames() bool
378+
}
379+
380+
func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host SourceFileMayBeEmittedHost, forceDtsEmit bool) bool {
381+
// TODO: move this to outputpaths?
382+
383+
options := host.Options()
384+
// Js files are emitted only if option is enabled
385+
if options.NoEmitForJsFiles.IsTrue() && ast.IsSourceFileJS(sourceFile) {
386+
return false
387+
}
373388

374389
// Declaration files are not emitted
375390
if sourceFile.IsDeclarationFile {
376391
return false
377392
}
378393

394+
// Source file from node_modules are not emitted
379395
if host.IsSourceFileFromExternalLibrary(sourceFile) {
380396
return false
381397
}
@@ -385,6 +401,7 @@ func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host printer.EmitHost, f
385401
return true
386402
}
387403

404+
// Check other conditions for file emit
388405
// Source files from referenced projects are not emitted
389406
if host.GetOutputAndProjectReference(sourceFile.Path()) != nil {
390407
return false
@@ -395,8 +412,24 @@ func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host printer.EmitHost, f
395412
return true
396413
}
397414

398-
// !!! Should JSON input files be emitted
399-
return false
415+
// Json file is not emitted if outDir is not specified
416+
if options.OutDir == "" {
417+
return false
418+
}
419+
420+
// Otherwise if rootDir or composite config file, we know common sourceDir and can check if file would be emitted in same location
421+
if options.RootDir != "" || (options.Composite.IsTrue() && options.ConfigFilePath != "") {
422+
commonDir := tspath.GetNormalizedAbsolutePath(outputpaths.GetCommonSourceDirectory(options, func() []string { return nil }, host.GetCurrentDirectory(), host.UseCaseSensitiveFileNames()), host.GetCurrentDirectory())
423+
outputPath := outputpaths.GetSourceFilePathInNewDirWorker(sourceFile.FileName(), options.OutDir, host.GetCurrentDirectory(), commonDir, host.UseCaseSensitiveFileNames())
424+
if tspath.ComparePaths(sourceFile.FileName(), outputPath, tspath.ComparePathsOptions{
425+
UseCaseSensitiveFileNames: host.UseCaseSensitiveFileNames(),
426+
CurrentDirectory: host.GetCurrentDirectory(),
427+
}) == 0 {
428+
return false
429+
}
430+
}
431+
432+
return true
400433
}
401434

402435
func getSourceFilesToEmit(host printer.EmitHost, targetSourceFile *ast.SourceFile, forceDtsEmit bool) []*ast.SourceFile {

internal/outputpaths/outputpaths.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func GetDeclarationEmitOutputFilePath(file string, options *core.CompilerOptions
123123

124124
var path string
125125
if outputDir != nil {
126-
path = getSourceFilePathInNewDirWorker(file, *outputDir, host.GetCurrentDirectory(), host.CommonSourceDirectory(), host.UseCaseSensitiveFileNames())
126+
path = GetSourceFilePathInNewDirWorker(file, *outputDir, host.GetCurrentDirectory(), host.CommonSourceDirectory(), host.UseCaseSensitiveFileNames())
127127
} else {
128128
path = file
129129
}
@@ -154,7 +154,7 @@ func getOutputPathWithoutChangingExtension(inputFileName string, outputDirectory
154154
return inputFileName
155155
}
156156

157-
func getSourceFilePathInNewDirWorker(fileName string, newDirPath string, currentDirectory string, commonSourceDirectory string, useCaseSensitiveFileNames bool) string {
157+
func GetSourceFilePathInNewDirWorker(fileName string, newDirPath string, currentDirectory string, commonSourceDirectory string, useCaseSensitiveFileNames bool) string {
158158
sourceFilePath := tspath.GetNormalizedAbsolutePath(fileName, currentDirectory)
159159
commonDir := tspath.GetCanonicalFileName(commonSourceDirectory, useCaseSensitiveFileNames)
160160
canonFile := tspath.GetCanonicalFileName(sourceFilePath, useCaseSensitiveFileNames)

internal/printer/printer.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3175,7 +3175,10 @@ func (p *Printer) emitEmptyStatement(node *ast.EmptyStatement, isEmbeddedStateme
31753175
func (p *Printer) emitExpressionStatement(node *ast.ExpressionStatement) {
31763176
state := p.enterNode(node.AsNode())
31773177

3178-
if isImmediatelyInvokedFunctionExpressionOrArrowFunction(node.Expression) {
3178+
if p.currentSourceFile != nil && p.currentSourceFile.ScriptKind == core.ScriptKindJSON {
3179+
// !!! In strada, this was handled by an undefined parenthesizerRule, so this is a hack.
3180+
p.emitExpression(node.Expression, ast.OperatorPrecedenceComma)
3181+
} else if isImmediatelyInvokedFunctionExpressionOrArrowFunction(node.Expression) {
31793182
// !!! introduce parentheses around callee
31803183
p.emitExpression(node.Expression, ast.OperatorPrecedenceParentheses)
31813184
} else {

testdata/baselines/reference/submodule/compiler/isolatedModules_resolveJsonModule_strict_outDir_commonJs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import * as j from "./j.json";
77
{}
88

99

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

testdata/baselines/reference/submodule/compiler/isolatedModules_resolveJsonModule_strict_outDir_commonJs.js.diff

Lines changed: 0 additions & 11 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/jsonFileImportChecksCallCorrectlyTwice.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ function fn(arg: Foo[]) { }
2121
]
2222
}
2323

24+
//// [data.json]
25+
{
26+
"foo": [
27+
{
28+
"bool": true,
29+
"str": "123"
30+
}
31+
]
32+
}
2433
//// [index.js]
2534
"use strict";
2635
var __importDefault = (this && this.__importDefault) || function (mod) {

testdata/baselines/reference/submodule/compiler/jsonFileImportChecksCallCorrectlyTwice.js.diff

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
--- old.jsonFileImportChecksCallCorrectlyTwice.js
22
+++ new.jsonFileImportChecksCallCorrectlyTwice.js
3-
@@= skipped -20, +20 lines =@@
4-
]
5-
}
6-
7-
-//// [data.json]
8-
-{
9-
- "foo": [
10-
- {
11-
- "bool": true,
12-
- "str": "123"
13-
- }
14-
- ]
15-
-}
16-
//// [index.js]
17-
"use strict";
18-
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
@@= skipped -35, +35 lines =@@
194
return (mod && mod.__esModule) ? mod : { "default": mod };
205
};
216
Object.defineProperty(exports, "__esModule", { value: true });

testdata/baselines/reference/submodule/compiler/moduleResolutionWithSuffixes_one_jsonModule.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
import foo from "./foo.json";
1414
console.log(foo.ios);
1515

16+
//// [/bin/foo.ios.json]
17+
{
18+
"ios": "platform ios"
19+
}
1620
//// [/bin/index.js]
1721
"use strict";
1822
var __importDefault = (this && this.__importDefault) || function (mod) {

testdata/baselines/reference/submodule/compiler/moduleResolutionWithSuffixes_one_jsonModule.js.diff

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
--- old.moduleResolutionWithSuffixes_one_jsonModule.js
22
+++ new.moduleResolutionWithSuffixes_one_jsonModule.js
3-
@@= skipped -12, +12 lines =@@
4-
import foo from "./foo.json";
5-
console.log(foo.ios);
6-
7-
-//// [/bin/foo.ios.json]
8-
-{
9-
- "ios": "platform ios"
10-
-}
11-
//// [/bin/index.js]
12-
"use strict";
13-
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
@@= skipped -22, +22 lines =@@
144
return (mod && mod.__esModule) ? mod : { "default": mod };
155
};
166
Object.defineProperty(exports, "__esModule", { value: true });

testdata/baselines/reference/submodule/compiler/requireOfJsonFile.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ if (x) {
1515
"b": "hello"
1616
}
1717

18+
//// [out/b.json]
19+
{
20+
"a": true,
21+
"b": "hello"
22+
}
1823
//// [out/file1.js]
1924
"use strict";
2025
Object.defineProperty(exports, "__esModule", { value: true });

0 commit comments

Comments
 (0)