Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 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
46 changes: 43 additions & 3 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4377,22 +4377,60 @@ func (p *Printer) emitHelpers(node *ast.Node) bool {
return helpersEmitted
}

func (p *Printer) emitPrologueDirectivesAndDetachedComments(node *ast.SourceFile, shouldEmitDetached bool) (index int, state *commentState) {
index = 0
state = nil

// When there are prologue directives and the first is synthesized, emit them first,
// then emit detached comments, then emit helpers. This matches TypeScript's behavior
// where writeFile calls emitPrologueDirectivesIfNeeded before emitSourceFile.
if shouldEmitDetached && len(node.Statements.Nodes) > 0 && ast.IsPrologueDirective(node.Statements.Nodes[0]) {
// Emit synthesized prologue directives first (without their comments since they're synthesized)
index = p.emitPrologueDirectives(node.Statements)
// Add a newline after prologue directives before detached comments
if !p.writer.IsAtStartOfLine() {
p.writeLine()
}
// Then emit detached comments (copyright headers) that come after prologues but before other code
// We need to save the state for emitting trailing comments later
emitFlags := p.emitContext.EmitFlags(node.AsNode())
containerPos := p.containerPos
containerEnd := p.containerEnd
declarationListContainerEnd := p.declarationListContainerEnd
p.emitDetachedCommentsAndUpdateCommentsInfo(node.Statements.Loc)
state = &commentState{emitFlags, node.Statements.Loc, containerPos, containerEnd, declarationListContainerEnd}
} else if shouldEmitDetached {
// No prologue directives, just emit detached comments normally
state = p.emitDetachedCommentsBeforeStatementList(node.AsNode(), node.Statements.Loc)
}

return index, state
}

func (p *Printer) emitSourceFile(node *ast.SourceFile) {
savedCurrentSourceFile := p.currentSourceFile
savedCommentsDisabled := p.commentsDisabled
p.currentSourceFile = node

p.writeLine()

state := p.emitDetachedCommentsBeforeStatementList(node.AsNode(), node.Statements.Loc)
shouldEmitDetached := p.shouldEmitDetachedComments(node.AsNode())

p.pushNameGenerationScope(node.AsNode())
p.generateAllNames(node.Statements)

index := 0
var state *commentState
if node.ScriptKind != core.ScriptKindJSON {
p.emitShebangIfNeeded(node)
index = p.emitPrologueDirectives(node.Statements)

index, state = p.emitPrologueDirectivesAndDetachedComments(node, shouldEmitDetached)
p.emitHelpers(node.AsNode())

if !shouldEmitDetached {
index = p.emitPrologueDirectives(node.Statements)
}

if node.IsDeclarationFile {
p.emitTripleSlashDirectives(node)
}
Expand All @@ -4408,7 +4446,9 @@ func (p *Printer) emitSourceFile(node *ast.SourceFile) {
-1, /*count*/
)
p.popNameGenerationScope(node.AsNode())
p.emitDetachedCommentsAfterStatementList(node.AsNode(), node.Statements.Loc, state)
if shouldEmitDetached {
p.emitDetachedCommentsAfterStatementList(node.AsNode(), node.Statements.Loc, state)
}
p.currentSourceFile = savedCurrentSourceFile
p.commentsDisabled = savedCommentsDisabled
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ function test(param) {

//// [emitEndOfFileJSDocComments2.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/** @typedef {number} A */
Object.defineProperty(exports, "__esModule", { value: true });
/**
* JSDoc comment for function
* @param {string} param - A string parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ watchMain();

//// [APISample_Watch.js]
"use strict";
/*
* Note: This test is a public API sample. The sample sources can be found
* at: https://github.com/Microsoft/TypeScript-wiki/blob/master/Using-the-Compiler-API.md#writing-an-incremental-program-watcher
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
const formatHost = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
--- old.APISample_Watch.js
+++ new.APISample_Watch.js
@@= skipped -84, +84 lines =@@

//// [APISample_Watch.js]
"use strict";
-/*
- * Note: This test is a public API sample. The sample sources can be found
- * at: https://github.com/Microsoft/TypeScript-wiki/blob/master/Using-the-Compiler-API.md#writing-an-incremental-program-watcher
- * Please log a "breaking change" issue for any API breaking change affecting this issue
- */
@@= skipped -90, +90 lines =@@
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
Object.defineProperty(exports, "__esModule", { value: true });
-var ts = require("typescript");
+const ts = require("typescript");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ watchMain();

//// [APISample_WatchWithDefaults.js]
"use strict";
/*
* Note: This test is a public API sample. This uses default sys interface without having to pass anything
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
function watchMain() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
--- old.APISample_WatchWithDefaults.js
+++ new.APISample_WatchWithDefaults.js
@@= skipped -57, +57 lines =@@

//// [APISample_WatchWithDefaults.js]
"use strict";
-/*
- * Note: This test is a public API sample. This uses default sys interface without having to pass anything
- * Please log a "breaking change" issue for any API breaking change affecting this issue
- */
@@= skipped -62, +62 lines =@@
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
Object.defineProperty(exports, "__esModule", { value: true });
-var ts = require("typescript");
+const ts = require("typescript");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ watchMain();

//// [APISample_WatchWithOwnWatchHost.js]
"use strict";
/*
* Note: This test is a public API sample. This sample verifies creating abstract builder to watch list of root files
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
function watchMain() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
--- old.APISample_WatchWithOwnWatchHost.js
+++ new.APISample_WatchWithOwnWatchHost.js
@@= skipped -64, +64 lines =@@

//// [APISample_WatchWithOwnWatchHost.js]
"use strict";
-/*
- * Note: This test is a public API sample. This sample verifies creating abstract builder to watch list of root files
- * Please log a "breaking change" issue for any API breaking change affecting this issue
- */
@@= skipped -69, +69 lines =@@
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
Object.defineProperty(exports, "__esModule", { value: true });
-var ts = require("typescript");
+const ts = require("typescript");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ compile(process.argv.slice(2), {

//// [APISample_compile.js]
"use strict";
/*
* Note: This test is a public API sample. The sample sources can be found
* at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.compile = compile;
const ts = require("typescript");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
--- old.APISample_compile.js
+++ new.APISample_compile.js
@@= skipped -47, +47 lines =@@

//// [APISample_compile.js]
"use strict";
-/*
- * Note: This test is a public API sample. The sample sources can be found
- * at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler
- * Please log a "breaking change" issue for any API breaking change affecting this issue
- */
@@= skipped -54, +54 lines =@@
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.compile = compile;
-var ts = require("typescript");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ function getSomeOtherTags(node: ts.Node) {

//// [APISample_jsdoc.js]
"use strict";
/*
* Note: This test is a public API sample. The original sources can be found
* at: https://github.com/YousefED/typescript-json-schema
* https://github.com/vega/ts-json-schema-generator
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
// excerpted from https://github.com/YousefED/typescript-json-schema
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
--- old.APISample_jsdoc.js
+++ new.APISample_jsdoc.js
@@= skipped -122, +122 lines =@@

//// [APISample_jsdoc.js]
"use strict";
-/*
- * Note: This test is a public API sample. The original sources can be found
- * at: https://github.com/YousefED/typescript-json-schema
- * https://github.com/vega/ts-json-schema-generator
- * Please log a "breaking change" issue for any API breaking change affecting this issue
- */
@@= skipped -129, +129 lines =@@
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
Object.defineProperty(exports, "__esModule", { value: true });
-var ts = require("typescript");
+const ts = require("typescript");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ fileNames.forEach(fileName => {

//// [APISample_linter.js]
"use strict";
/*
* Note: This test is a public API sample. The sample sources can be found
* at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#traversing-the-ast-with-a-little-linter
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.delint = delint;
const ts = require("typescript");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
--- old.APISample_linter.js
+++ new.APISample_linter.js
@@= skipped -73, +73 lines =@@

//// [APISample_linter.js]
"use strict";
-/*
- * Note: This test is a public API sample. The sample sources can be found
- * at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#traversing-the-ast-with-a-little-linter
- * Please log a "breaking change" issue for any API breaking change affecting this issue
- */
@@= skipped -80, +80 lines =@@
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.delint = delint;
-var ts = require("typescript");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export function createProgram(rootFiles: string[], compilerOptionsJson: string):

//// [APISample_parseConfig.js]
"use strict";
/*
* Note: This test is a public API sample. The sample sources can be found
* at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.createProgram = createProgram;
const ts = require("typescript");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
--- old.APISample_parseConfig.js
+++ new.APISample_parseConfig.js
@@= skipped -45, +45 lines =@@

//// [APISample_parseConfig.js]
"use strict";
-/*
- * Note: This test is a public API sample. The sample sources can be found
- * at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler
- * Please log a "breaking change" issue for any API breaking change affecting this issue
- */
@@= skipped -52, +52 lines =@@
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.createProgram = createProgram;
-var ts = require("typescript");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ console.log(JSON.stringify(result));

//// [APISample_transform.js]
"use strict";
/*
* Note: This test is a public API sample. The sample sources can be found
* at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-simple-transform-function
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
const source = "let x: string = 'string'";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
--- old.APISample_transform.js
+++ new.APISample_transform.js
@@= skipped -25, +25 lines =@@

//// [APISample_transform.js]
"use strict";
-/*
- * Note: This test is a public API sample. The sample sources can be found
- * at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-simple-transform-function
- * Please log a "breaking change" issue for any API breaking change affecting this issue
- */
@@= skipped -31, +31 lines =@@
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
Object.defineProperty(exports, "__esModule", { value: true });
-var ts = require("typescript");
+const ts = require("typescript");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ watch(currentDirectoryFiles, { module: ts.ModuleKind.CommonJS });

//// [APISample_watcher.js]
"use strict";
/*
* Note: This test is a public API sample. The sample sources can be found
* at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#incremental-build-support-using-the-language-services
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
function watch(rootFileNames, options) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
--- old.APISample_watcher.js
+++ new.APISample_watcher.js
@@= skipped -120, +120 lines =@@

//// [APISample_watcher.js]
"use strict";
-/*
- * Note: This test is a public API sample. The sample sources can be found
- * at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#incremental-build-support-using-the-language-services
- * Please log a "breaking change" issue for any API breaking change affecting this issue
- */
@@= skipped -126, +126 lines =@@
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
Object.defineProperty(exports, "__esModule", { value: true });
-var ts = require("typescript");
+const ts = require("typescript");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ m1.f();

//// [amdDependencyComment1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
///<amd-dependency path='bar'/>
Object.defineProperty(exports, "__esModule", { value: true });
const m1 = require("m2");
m1.f();
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
--- old.amdDependencyComment1.js
+++ new.amdDependencyComment1.js
@@= skipped -7, +7 lines =@@

//// [amdDependencyComment1.js]
@@= skipped -9, +9 lines =@@
"use strict";
-///<amd-dependency path='bar'/>
///<amd-dependency path='bar'/>
Object.defineProperty(exports, "__esModule", { value: true });
-var m1 = require("m2");
+///<amd-dependency path='bar'/>
+const m1 = require("m2");
m1.f();
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ m1.f();

//// [amdDependencyCommentName1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
///<amd-dependency path='bar' name='b'/>
Object.defineProperty(exports, "__esModule", { value: true });
const m1 = require("m2");
m1.f();
Loading