Skip to content
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions internal/compiler/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func getScriptTransformers(emitContext *printer.EmitContext, host printer.EmitHo
tx = append(tx, downleveler)
}

tx = append(tx, estransforms.NewUseStrictTransformer(&opts))

// transform module syntax
tx = append(tx, getModuleTransformer(&opts))

Expand Down
1 change: 1 addition & 0 deletions internal/execute/tsctests/tscbuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,7 @@ func TestBuildProgramUpdates(t *testing.T) {
sys.writeFileNoError("/user/username/projects/project/project2.tsconfig.json", stringtestutil.Dedent(`
{
"extends": "./alpha.tsconfig.json",
"files": ["other.ts"]
}`), false)
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/printer/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func (f *NodeFactory) SplitStandardPrologue(source []*ast.Statement) (prologue [
return source[:i], source[i:]
}
}
return nil, source
return source, nil
}

// Splits a slice of statements into two parts: custom prologue statements (e.g., with `EFCustomPrologue` set) and the rest of the statements
Expand Down
4 changes: 2 additions & 2 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1558,10 +1558,10 @@ func (p *Printer) emitFunctionBody(body *ast.Block) {

if p.shouldEmitBlockFunctionBodyOnSingleLine(body) && statementOffset == 0 && pos == p.writer.GetTextPos() {
p.decreaseIndent()
p.emitList((*Printer).emitStatement, body.AsNode(), body.Statements, LFSingleLineFunctionBodyStatements)
p.emitListRange((*Printer).emitStatement, body.AsNode(), body.Statements, LFSingleLineFunctionBodyStatements, statementOffset, -1)
p.increaseIndent()
} else {
p.emitList((*Printer).emitStatement, body.AsNode(), body.Statements, LFMultiLineFunctionBodyStatements)
p.emitListRange((*Printer).emitStatement, body.AsNode(), body.Statements, LFMultiLineFunctionBodyStatements, statementOffset, -1)
}

p.emitDetachedCommentsAfterStatementList(body.AsNode(), body.Statements.Loc, detachedState)
Expand Down
55 changes: 55 additions & 0 deletions internal/transformers/estransforms/usestrict.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package estransforms

import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/transformers"
)

func NewUseStrictTransformer(opts *transformers.TransformOptions) *transformers.Transformer {
tx := &useStrictTransformer{
compilerOptions: opts.CompilerOptions,
getEmitModuleFormatOfFile: opts.GetEmitModuleFormatOfFile,
}
return tx.NewTransformer(tx.visit, opts.Context)
}

type useStrictTransformer struct {
transformers.Transformer
compilerOptions *core.CompilerOptions
getEmitModuleFormatOfFile func(file ast.HasFileName) core.ModuleKind
}

func (tx *useStrictTransformer) visit(node *ast.Node) *ast.Node {
if node.Kind != ast.KindSourceFile {
return node
}
return tx.visitSourceFile(node.AsSourceFile())
}

func (tx *useStrictTransformer) visitSourceFile(node *ast.SourceFile) *ast.Node {
if node.ScriptKind == core.ScriptKindJSON {
return node.AsNode()
}

if tx.compilerOptions.GetEmitModuleKind() == core.ModuleKindPreserve {
return node.AsNode()
}

isExternalModule := ast.IsExternalModule(node)
format := tx.getEmitModuleFormatOfFile(node)

if isExternalModule && format >= core.ModuleKindES2015 {
return node.AsNode()
}

if isExternalModule ||
tx.compilerOptions.AlwaysStrict.DefaultIfUnknown(tx.compilerOptions.Strict).IsTrue() {
statements := tx.Factory().EnsureUseStrict(node.Statements.Nodes)
statementList := tx.Factory().NewNodeList(statements)
statementList.Loc = node.Statements.Loc
return tx.Factory().UpdateSourceFile(node, statementList, node.EndOfFileToken).AsSourceFile().AsNode()
}

return node.AsNode()
}
6 changes: 0 additions & 6 deletions internal/transformers/moduletransforms/commonjsmodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,6 @@ func (tx *CommonJSModuleTransformer) transformCommonJSModule(node *ast.SourceFil
prologue, rest := tx.Factory().SplitStandardPrologue(node.Statements.Nodes)
statements := slices.Clone(prologue)

// ensure "use strict" if not present
if ast.IsExternalModule(tx.currentSourceFile) ||
tx.compilerOptions.AlwaysStrict.DefaultIfUnknown(tx.compilerOptions.Strict).IsTrue() {
statements = tx.Factory().EnsureUseStrict(statements)
}

// emit custom prologues from other transformations
custom, rest := tx.Factory().SplitCustomPrologue(rest)
statements = append(statements, core.FirstResult(tx.topLevelVisitor.VisitSlice(custom))...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function Test() {


//// [jsxNestedIndentation.js]
"use strict";
function Test() {
return React.createElement(Child, null,
React.createElement(Child, null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function calculatePropertyName(index) {


//// [elementAccessExpressionInJS.js]
"use strict";
if (module[calculatePropertyName(1)]) {
}
function calculatePropertyName(index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ function foo() {
//// [abstractIdentifierNameStrict.js]
var abstract = true;
function foo() {
"use strict";
"use strict";
var abstract = true;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ f.register(s, null);
f.unregister(s);

//// [acceptSymbolAsWeakType.js]
"use strict";
const s = Symbol('s');
const ws = new WeakSet([s]);
ws.add(s);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ e as ErrAlias<string>;


//// [aliasInstantiationExpressionGenericIntersectionNoCrash1.js]
"use strict";
class ErrImpl {
e;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
--- old.aliasInstantiationExpressionGenericIntersectionNoCrash1.js
+++ new.aliasInstantiationExpressionGenericIntersectionNoCrash1.js
@@= skipped -13, +13 lines =@@


@@= skipped -15, +15 lines =@@
//// [aliasInstantiationExpressionGenericIntersectionNoCrash1.js]
-"use strict";
"use strict";
class ErrImpl {
+ e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ wat as Wat<string>;


//// [aliasInstantiationExpressionGenericIntersectionNoCrash2.js]
"use strict";
wat;

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ let check3: test3 = "y";


//// [aliasOfGenericFunctionWithRestBehavedSameAsUnaliased.js]
"use strict";
// the type printback for every `test` below should be "y"
let check = "y";
let check1 = "y";
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function f() {
}

//// [alwaysStrict.js]
"use strict";
function f() {
var arguments = [];
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function f() {
}

//// [alwaysStrictES6.js]
"use strict";
function f() {
var arguments = [];
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module M {
}

//// [alwaysStrictModule.js]
"use strict";
var M;
(function (M) {
function f() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function foo<T>(arr: T[], depth: number) {
}

//// [arrayFakeFlatNoCrashInferenceDeclarations.js]
"use strict";
function foo(arr, depth) {
return flat(arr, depth);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
--- old.arrayFakeFlatNoCrashInferenceDeclarations.js
+++ new.arrayFakeFlatNoCrashInferenceDeclarations.js
@@= skipped -17, +17 lines =@@
}

//// [arrayFakeFlatNoCrashInferenceDeclarations.js]
-"use strict";
@@= skipped -21, +21 lines =@@
function foo(arr, depth) {
return flat(arr, depth);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function foo<T>(arr: T[], depth: number) {
}

//// [arrayFlatNoCrashInference.js]
"use strict";
function foo(arr, depth) {
return arr.flat(depth);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function foo<T>(arr: T[], depth: number) {
}

//// [arrayFlatNoCrashInferenceDeclarations.js]
"use strict";
function foo(arr, depth) {
return arr.flat(depth);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ animalOrUndef.canMeow; // since is cat, should not be an error


//// [assertionFunctionsCanNarrowByDiscriminant.js]
"use strict";
const animal = { type: 'cat', canMeow: true };
assertEqual(animal.type, 'cat');
animal.canMeow; // since is cat, should not be an error
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ getValue2<number> = () => 1234;


//// [assignmentToInstantiationExpression.js]
"use strict";
let obj = {};
obj.fn = () => 1234;
let getValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
--- old.assignmentToInstantiationExpression.js
+++ new.assignmentToInstantiationExpression.js
@@= skipped -13, +13 lines =@@


@@= skipped -15, +15 lines =@@
//// [assignmentToInstantiationExpression.js]
-"use strict";
"use strict";
let obj = {};
-(obj.fn) = () => 1234;
+obj.fn = () => 1234;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const increment2: (


//// [asyncFunctionContextuallyTypedReturns.js]
"use strict";
f(v => v ? [0] : Promise.reject());
f(async v => v ? [0] : Promise.reject());
g(v => v ? "contextuallyTypable" : Promise.reject());
Expand Down
Loading
Loading