Skip to content

Introduces new flag for optimization hints #38602

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

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 7 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,13 @@ namespace ts {
category: Diagnostics.Advanced_Options,
description: Diagnostics.Resolve_keyof_to_string_valued_property_names_only_no_numbers_or_symbols,
},
{
name: "addOptimizationHints",
type: "boolean",
affectsEmit: true,
category: Diagnostics.Advanced_Options,
description: Diagnostics.Resolve_keyof_to_string_valued_property_names_only_no_numbers_or_symbols,
},
{
// A list of plugins to load in the language service
name: "plugins",
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4400,6 +4400,10 @@
"category": "Error",
"code": 6233
},
"Emit code hints for optimizers and minifiers.": {
"category": "Message",
"code": 6234
},

"Projects to reference": {
"category": "Message",
Expand Down
14 changes: 13 additions & 1 deletion src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ namespace ts {
IsNamedExternalExport = 1 << 4,
IsDefaultExternalExport = 1 << 5,
IsDerivedClass = 1 << 6,
UseImmediatelyInvokedFunctionExpression = 1 << 7,

HasAnyDecorators = HasConstructorDecorators | HasMemberDecorators,
NeedsName = HasStaticInitializedProperties | HasMemberDecorators,
UseImmediatelyInvokedFunctionExpression = HasAnyDecorators | HasStaticInitializedProperties,
IsExported = IsExportOfNamespace | IsDefaultExternalExport | IsNamedExternalExport,
}

Expand Down Expand Up @@ -585,6 +585,17 @@ namespace ts {
return parameter.decorators !== undefined && parameter.decorators.length > 0;
}

function shouldWrapClassWithIIFE(facts: ClassFacts) {
if (compilerOptions.addOptimizationHints === false) return false;
if (facts & ClassFacts.HasAnyDecorators) return true;
if (facts & ClassFacts.HasStaticInitializedProperties) {
if (languageVersion < ScriptTarget.ESNext) return true;
else if (languageVersion === ScriptTarget.ESNext && !compilerOptions.useDefineForClassFields) return true;
}

return false;
}

function getClassFacts(node: ClassDeclaration, staticProperties: readonly PropertyDeclaration[]) {
let facts = ClassFacts.None;
if (some(staticProperties)) facts |= ClassFacts.HasStaticInitializedProperties;
Expand All @@ -595,6 +606,7 @@ namespace ts {
if (isExportOfNamespace(node)) facts |= ClassFacts.IsExportOfNamespace;
else if (isDefaultExternalModuleExport(node)) facts |= ClassFacts.IsDefaultExternalExport;
else if (isNamedExternalModuleExport(node)) facts |= ClassFacts.IsNamedExternalExport;
if (shouldWrapClassWithIIFE(facts)) facts |= ClassFacts.UseImmediatelyInvokedFunctionExpression;
return facts;
}

Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5133,6 +5133,7 @@ namespace ts {
export type CompilerOptionsValue = string | number | boolean | (string | number)[] | string[] | MapLike<string[]> | PluginImport[] | ProjectReference[] | null | undefined;

export interface CompilerOptions {
addOptimizationHints?: boolean;
/*@internal*/ all?: boolean;
allowJs?: boolean;
/*@internal*/ allowNonTsExtensions?: boolean;
Expand Down
23 changes: 23 additions & 0 deletions tests/baselines/reference/addOptimizationHints.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
tests/cases/compiler/addOptimizationHints.ts(13,2): error TS2304: Cannot find name 'decorate'.


==== tests/cases/compiler/addOptimizationHints.ts (1 errors) ====
class HasNoStatics {
value: unknown;
method() {}
}

class HasStatic {
static value = 0;
static someMethod() {}

method() {}
}

@decorate
~~~~~~~~
!!! error TS2304: Cannot find name 'decorate'.
class HasDecorator {
method() {}
}

40 changes: 40 additions & 0 deletions tests/baselines/reference/addOptimizationHints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//// [addOptimizationHints.ts]
class HasNoStatics {
value: unknown;
method() {}
}

class HasStatic {
static value = 0;
static someMethod() {}

method() {}
}

@decorate
class HasDecorator {
method() {}
}


//// [addOptimizationHints.js]
class HasNoStatics {
method() { }
}
let HasStatic = /** @class */ (() => {
class HasStatic {
static someMethod() { }
method() { }
}
HasStatic.value = 0;
return HasStatic;
})();
let HasDecorator = /** @class */ (() => {
let HasDecorator = class HasDecorator {
method() { }
};
HasDecorator = __decorate([
decorate
], HasDecorator);
return HasDecorator;
})();
32 changes: 32 additions & 0 deletions tests/baselines/reference/addOptimizationHints.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== tests/cases/compiler/addOptimizationHints.ts ===
class HasNoStatics {
>HasNoStatics : Symbol(HasNoStatics, Decl(addOptimizationHints.ts, 0, 0))

value: unknown;
>value : Symbol(HasNoStatics.value, Decl(addOptimizationHints.ts, 0, 20))

method() {}
>method : Symbol(HasNoStatics.method, Decl(addOptimizationHints.ts, 1, 17))
}

class HasStatic {
>HasStatic : Symbol(HasStatic, Decl(addOptimizationHints.ts, 3, 1))

static value = 0;
>value : Symbol(HasStatic.value, Decl(addOptimizationHints.ts, 5, 17))

static someMethod() {}
>someMethod : Symbol(HasStatic.someMethod, Decl(addOptimizationHints.ts, 6, 19))

method() {}
>method : Symbol(HasStatic.method, Decl(addOptimizationHints.ts, 7, 24))
}

@decorate
class HasDecorator {
>HasDecorator : Symbol(HasDecorator, Decl(addOptimizationHints.ts, 10, 1))

method() {}
>method : Symbol(HasDecorator.method, Decl(addOptimizationHints.ts, 13, 20))
}

35 changes: 35 additions & 0 deletions tests/baselines/reference/addOptimizationHints.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
=== tests/cases/compiler/addOptimizationHints.ts ===
class HasNoStatics {
>HasNoStatics : HasNoStatics

value: unknown;
>value : unknown

method() {}
>method : () => void
}

class HasStatic {
>HasStatic : HasStatic

static value = 0;
>value : number
>0 : 0

static someMethod() {}
>someMethod : () => void

method() {}
>method : () => void
}

@decorate
>decorate : any

class HasDecorator {
>HasDecorator : HasDecorator

method() {}
>method : () => void
}

23 changes: 23 additions & 0 deletions tests/baselines/reference/addOptimizationHintsFalse.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
tests/cases/compiler/addOptimizationHintsFalse.ts(13,2): error TS2304: Cannot find name 'decorate'.


==== tests/cases/compiler/addOptimizationHintsFalse.ts (1 errors) ====
class HasNoStatics {
value: unknown;
method() {}
}

class HasStatic {
static value = 0;
static someMethod() {}

method() {}
}

@decorate
~~~~~~~~
!!! error TS2304: Cannot find name 'decorate'.
class HasDecorator {
method() {}
}

34 changes: 34 additions & 0 deletions tests/baselines/reference/addOptimizationHintsFalse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [addOptimizationHintsFalse.ts]
class HasNoStatics {
value: unknown;
method() {}
}

class HasStatic {
static value = 0;
static someMethod() {}

method() {}
}

@decorate
class HasDecorator {
method() {}
}


//// [addOptimizationHintsFalse.js]
class HasNoStatics {
method() { }
}
class HasStatic {
static someMethod() { }
method() { }
}
HasStatic.value = 0;
let HasDecorator = class HasDecorator {
method() { }
};
HasDecorator = __decorate([
decorate
], HasDecorator);
32 changes: 32 additions & 0 deletions tests/baselines/reference/addOptimizationHintsFalse.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== tests/cases/compiler/addOptimizationHintsFalse.ts ===
class HasNoStatics {
>HasNoStatics : Symbol(HasNoStatics, Decl(addOptimizationHintsFalse.ts, 0, 0))

value: unknown;
>value : Symbol(HasNoStatics.value, Decl(addOptimizationHintsFalse.ts, 0, 20))

method() {}
>method : Symbol(HasNoStatics.method, Decl(addOptimizationHintsFalse.ts, 1, 17))
}

class HasStatic {
>HasStatic : Symbol(HasStatic, Decl(addOptimizationHintsFalse.ts, 3, 1))

static value = 0;
>value : Symbol(HasStatic.value, Decl(addOptimizationHintsFalse.ts, 5, 17))

static someMethod() {}
>someMethod : Symbol(HasStatic.someMethod, Decl(addOptimizationHintsFalse.ts, 6, 19))

method() {}
>method : Symbol(HasStatic.method, Decl(addOptimizationHintsFalse.ts, 7, 24))
}

@decorate
class HasDecorator {
>HasDecorator : Symbol(HasDecorator, Decl(addOptimizationHintsFalse.ts, 10, 1))

method() {}
>method : Symbol(HasDecorator.method, Decl(addOptimizationHintsFalse.ts, 13, 20))
}

35 changes: 35 additions & 0 deletions tests/baselines/reference/addOptimizationHintsFalse.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
=== tests/cases/compiler/addOptimizationHintsFalse.ts ===
class HasNoStatics {
>HasNoStatics : HasNoStatics

value: unknown;
>value : unknown

method() {}
>method : () => void
}

class HasStatic {
>HasStatic : HasStatic

static value = 0;
>value : number
>0 : 0

static someMethod() {}
>someMethod : () => void

method() {}
>method : () => void
}

@decorate
>decorate : any

class HasDecorator {
>HasDecorator : HasDecorator

method() {}
>method : () => void
}

1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2651,6 +2651,7 @@ declare namespace ts {
}
export type CompilerOptionsValue = string | number | boolean | (string | number)[] | string[] | MapLike<string[]> | PluginImport[] | ProjectReference[] | null | undefined;
export interface CompilerOptions {
addOptimizationHints?: boolean;
allowJs?: boolean;
allowSyntheticDefaultImports?: boolean;
allowUmdGlobalAccess?: boolean;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2651,6 +2651,7 @@ declare namespace ts {
}
export type CompilerOptionsValue = string | number | boolean | (string | number)[] | string[] | MapLike<string[]> | PluginImport[] | ProjectReference[] | null | undefined;
export interface CompilerOptions {
addOptimizationHints?: boolean;
allowJs?: boolean;
allowSyntheticDefaultImports?: boolean;
allowUmdGlobalAccess?: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"addOptimizationHints": true
}
}
17 changes: 7 additions & 10 deletions tests/baselines/reference/thisInClassBodyStaticESNext.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ class Foo {

//// [thisInClassBodyStaticESNext.js]
// all are allowed with es-compliant class field emit
let Foo = /** @class */ (() => {
class Foo {
x = this;
static t = this;
static at = () => this;
static ft = function () { return this; };
static mt() { return this; }
}
return Foo;
})();
class Foo {
x = this;
static t = this;
static at = () => this;
static ft = function () { return this; };
static mt() { return this; }
}
20 changes: 20 additions & 0 deletions tests/cases/compiler/addOptimizationHints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @target: ES2015
// @experimentalDecorators: true
// @noemithelpers: true
// @addOptimizationHints: true
class HasNoStatics {
value: unknown;
method() {}
}

class HasStatic {
static value = 0;
static someMethod() {}

method() {}
}

@decorate
class HasDecorator {
method() {}
}
Loading