Skip to content

Commit e1a1dad

Browse files
committed
Add addOptimizationHints compiler option
1 parent 148bfb1 commit e1a1dad

17 files changed

+325
-5
lines changed

src/compiler/commandLineParser.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,13 @@ namespace ts {
986986
category: Diagnostics.Advanced_Options,
987987
description: Diagnostics.Resolve_keyof_to_string_valued_property_names_only_no_numbers_or_symbols,
988988
},
989+
{
990+
name: "addOptimizationHints",
991+
type: "boolean",
992+
affectsEmit: true,
993+
category: Diagnostics.Advanced_Options,
994+
description: Diagnostics.Resolve_keyof_to_string_valued_property_names_only_no_numbers_or_symbols,
995+
},
989996
{
990997
// A list of plugins to load in the language service
991998
name: "plugins",

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4400,6 +4400,10 @@
44004400
"category": "Error",
44014401
"code": 6233
44024402
},
4403+
"Emit code hints for optimizers and minifiers.": {
4404+
"category": "Message",
4405+
"code": 6234
4406+
},
44034407

44044408
"Projects to reference": {
44054409
"category": "Message",

src/compiler/transformers/ts.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,17 @@ namespace ts {
585585
return parameter.decorators !== undefined && parameter.decorators.length > 0;
586586
}
587587

588+
function shouldWrapClassWithIIFE(facts: ClassFacts) {
589+
if (compilerOptions.addOptimizationHints === false) return false;
590+
if (facts & ClassFacts.HasAnyDecorators) return true;
591+
if (facts & ClassFacts.HasStaticInitializedProperties) {
592+
if (languageVersion < ScriptTarget.ESNext) return true;
593+
else if (languageVersion === ScriptTarget.ESNext && !compilerOptions.useDefineForClassFields) return true;
594+
}
595+
596+
return false;
597+
}
598+
588599
function getClassFacts(node: ClassDeclaration, staticProperties: readonly PropertyDeclaration[]) {
589600
let facts = ClassFacts.None;
590601
if (some(staticProperties)) facts |= ClassFacts.HasStaticInitializedProperties;
@@ -595,11 +606,7 @@ namespace ts {
595606
if (isExportOfNamespace(node)) facts |= ClassFacts.IsExportOfNamespace;
596607
else if (isDefaultExternalModuleExport(node)) facts |= ClassFacts.IsDefaultExternalExport;
597608
else if (isNamedExternalModuleExport(node)) facts |= ClassFacts.IsNamedExternalExport;
598-
if (facts & ClassFacts.HasAnyDecorators) facts |= ClassFacts.UseImmediatelyInvokedFunctionExpression;
599-
if (facts & ClassFacts.HasStaticInitializedProperties) {
600-
if (languageVersion < ScriptTarget.ESNext) facts |= ClassFacts.UseImmediatelyInvokedFunctionExpression;
601-
else if (languageVersion === ScriptTarget.ESNext && !compilerOptions.useDefineForClassFields) facts |= ClassFacts.UseImmediatelyInvokedFunctionExpression;
602-
}
609+
if (shouldWrapClassWithIIFE(facts)) facts |= ClassFacts.UseImmediatelyInvokedFunctionExpression;
603610
return facts;
604611
}
605612

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5133,6 +5133,7 @@ namespace ts {
51335133
export type CompilerOptionsValue = string | number | boolean | (string | number)[] | string[] | MapLike<string[]> | PluginImport[] | ProjectReference[] | null | undefined;
51345134

51355135
export interface CompilerOptions {
5136+
addOptimizationHints?: boolean;
51365137
/*@internal*/ all?: boolean;
51375138
allowJs?: boolean;
51385139
/*@internal*/ allowNonTsExtensions?: boolean;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
tests/cases/compiler/addOptimizationHints.ts(13,2): error TS2304: Cannot find name 'decorate'.
2+
3+
4+
==== tests/cases/compiler/addOptimizationHints.ts (1 errors) ====
5+
class HasNoStatics {
6+
value: unknown;
7+
method() {}
8+
}
9+
10+
class HasStatic {
11+
static value = 0;
12+
static someMethod() {}
13+
14+
method() {}
15+
}
16+
17+
@decorate
18+
~~~~~~~~
19+
!!! error TS2304: Cannot find name 'decorate'.
20+
class HasDecorator {
21+
method() {}
22+
}
23+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [addOptimizationHints.ts]
2+
class HasNoStatics {
3+
value: unknown;
4+
method() {}
5+
}
6+
7+
class HasStatic {
8+
static value = 0;
9+
static someMethod() {}
10+
11+
method() {}
12+
}
13+
14+
@decorate
15+
class HasDecorator {
16+
method() {}
17+
}
18+
19+
20+
//// [addOptimizationHints.js]
21+
class HasNoStatics {
22+
method() { }
23+
}
24+
let HasStatic = /** @class */ (() => {
25+
class HasStatic {
26+
static someMethod() { }
27+
method() { }
28+
}
29+
HasStatic.value = 0;
30+
return HasStatic;
31+
})();
32+
let HasDecorator = /** @class */ (() => {
33+
let HasDecorator = class HasDecorator {
34+
method() { }
35+
};
36+
HasDecorator = __decorate([
37+
decorate
38+
], HasDecorator);
39+
return HasDecorator;
40+
})();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== tests/cases/compiler/addOptimizationHints.ts ===
2+
class HasNoStatics {
3+
>HasNoStatics : Symbol(HasNoStatics, Decl(addOptimizationHints.ts, 0, 0))
4+
5+
value: unknown;
6+
>value : Symbol(HasNoStatics.value, Decl(addOptimizationHints.ts, 0, 20))
7+
8+
method() {}
9+
>method : Symbol(HasNoStatics.method, Decl(addOptimizationHints.ts, 1, 17))
10+
}
11+
12+
class HasStatic {
13+
>HasStatic : Symbol(HasStatic, Decl(addOptimizationHints.ts, 3, 1))
14+
15+
static value = 0;
16+
>value : Symbol(HasStatic.value, Decl(addOptimizationHints.ts, 5, 17))
17+
18+
static someMethod() {}
19+
>someMethod : Symbol(HasStatic.someMethod, Decl(addOptimizationHints.ts, 6, 19))
20+
21+
method() {}
22+
>method : Symbol(HasStatic.method, Decl(addOptimizationHints.ts, 7, 24))
23+
}
24+
25+
@decorate
26+
class HasDecorator {
27+
>HasDecorator : Symbol(HasDecorator, Decl(addOptimizationHints.ts, 10, 1))
28+
29+
method() {}
30+
>method : Symbol(HasDecorator.method, Decl(addOptimizationHints.ts, 13, 20))
31+
}
32+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
=== tests/cases/compiler/addOptimizationHints.ts ===
2+
class HasNoStatics {
3+
>HasNoStatics : HasNoStatics
4+
5+
value: unknown;
6+
>value : unknown
7+
8+
method() {}
9+
>method : () => void
10+
}
11+
12+
class HasStatic {
13+
>HasStatic : HasStatic
14+
15+
static value = 0;
16+
>value : number
17+
>0 : 0
18+
19+
static someMethod() {}
20+
>someMethod : () => void
21+
22+
method() {}
23+
>method : () => void
24+
}
25+
26+
@decorate
27+
>decorate : any
28+
29+
class HasDecorator {
30+
>HasDecorator : HasDecorator
31+
32+
method() {}
33+
>method : () => void
34+
}
35+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
tests/cases/compiler/addOptimizationHintsFalse.ts(13,2): error TS2304: Cannot find name 'decorate'.
2+
3+
4+
==== tests/cases/compiler/addOptimizationHintsFalse.ts (1 errors) ====
5+
class HasNoStatics {
6+
value: unknown;
7+
method() {}
8+
}
9+
10+
class HasStatic {
11+
static value = 0;
12+
static someMethod() {}
13+
14+
method() {}
15+
}
16+
17+
@decorate
18+
~~~~~~~~
19+
!!! error TS2304: Cannot find name 'decorate'.
20+
class HasDecorator {
21+
method() {}
22+
}
23+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//// [addOptimizationHintsFalse.ts]
2+
class HasNoStatics {
3+
value: unknown;
4+
method() {}
5+
}
6+
7+
class HasStatic {
8+
static value = 0;
9+
static someMethod() {}
10+
11+
method() {}
12+
}
13+
14+
@decorate
15+
class HasDecorator {
16+
method() {}
17+
}
18+
19+
20+
//// [addOptimizationHintsFalse.js]
21+
class HasNoStatics {
22+
method() { }
23+
}
24+
class HasStatic {
25+
static someMethod() { }
26+
method() { }
27+
}
28+
HasStatic.value = 0;
29+
let HasDecorator = class HasDecorator {
30+
method() { }
31+
};
32+
HasDecorator = __decorate([
33+
decorate
34+
], HasDecorator);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== tests/cases/compiler/addOptimizationHintsFalse.ts ===
2+
class HasNoStatics {
3+
>HasNoStatics : Symbol(HasNoStatics, Decl(addOptimizationHintsFalse.ts, 0, 0))
4+
5+
value: unknown;
6+
>value : Symbol(HasNoStatics.value, Decl(addOptimizationHintsFalse.ts, 0, 20))
7+
8+
method() {}
9+
>method : Symbol(HasNoStatics.method, Decl(addOptimizationHintsFalse.ts, 1, 17))
10+
}
11+
12+
class HasStatic {
13+
>HasStatic : Symbol(HasStatic, Decl(addOptimizationHintsFalse.ts, 3, 1))
14+
15+
static value = 0;
16+
>value : Symbol(HasStatic.value, Decl(addOptimizationHintsFalse.ts, 5, 17))
17+
18+
static someMethod() {}
19+
>someMethod : Symbol(HasStatic.someMethod, Decl(addOptimizationHintsFalse.ts, 6, 19))
20+
21+
method() {}
22+
>method : Symbol(HasStatic.method, Decl(addOptimizationHintsFalse.ts, 7, 24))
23+
}
24+
25+
@decorate
26+
class HasDecorator {
27+
>HasDecorator : Symbol(HasDecorator, Decl(addOptimizationHintsFalse.ts, 10, 1))
28+
29+
method() {}
30+
>method : Symbol(HasDecorator.method, Decl(addOptimizationHintsFalse.ts, 13, 20))
31+
}
32+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
=== tests/cases/compiler/addOptimizationHintsFalse.ts ===
2+
class HasNoStatics {
3+
>HasNoStatics : HasNoStatics
4+
5+
value: unknown;
6+
>value : unknown
7+
8+
method() {}
9+
>method : () => void
10+
}
11+
12+
class HasStatic {
13+
>HasStatic : HasStatic
14+
15+
static value = 0;
16+
>value : number
17+
>0 : 0
18+
19+
static someMethod() {}
20+
>someMethod : () => void
21+
22+
method() {}
23+
>method : () => void
24+
}
25+
26+
@decorate
27+
>decorate : any
28+
29+
class HasDecorator {
30+
>HasDecorator : HasDecorator
31+
32+
method() {}
33+
>method : () => void
34+
}
35+

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2651,6 +2651,7 @@ declare namespace ts {
26512651
}
26522652
export type CompilerOptionsValue = string | number | boolean | (string | number)[] | string[] | MapLike<string[]> | PluginImport[] | ProjectReference[] | null | undefined;
26532653
export interface CompilerOptions {
2654+
addOptimizationHints?: boolean;
26542655
allowJs?: boolean;
26552656
allowSyntheticDefaultImports?: boolean;
26562657
allowUmdGlobalAccess?: boolean;

tests/baselines/reference/api/typescript.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2651,6 +2651,7 @@ declare namespace ts {
26512651
}
26522652
export type CompilerOptionsValue = string | number | boolean | (string | number)[] | string[] | MapLike<string[]> | PluginImport[] | ProjectReference[] | null | undefined;
26532653
export interface CompilerOptions {
2654+
addOptimizationHints?: boolean;
26542655
allowJs?: boolean;
26552656
allowSyntheticDefaultImports?: boolean;
26562657
allowUmdGlobalAccess?: boolean;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"addOptimizationHints": true
4+
}
5+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// @target: ES2015
2+
// @experimentalDecorators: true
3+
// @noemithelpers: true
4+
// @addOptimizationHints: true
5+
class HasNoStatics {
6+
value: unknown;
7+
method() {}
8+
}
9+
10+
class HasStatic {
11+
static value = 0;
12+
static someMethod() {}
13+
14+
method() {}
15+
}
16+
17+
@decorate
18+
class HasDecorator {
19+
method() {}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// @target: ES2015
2+
// @experimentalDecorators: true
3+
// @noemithelpers: true
4+
// @addOptimizationHints: false
5+
class HasNoStatics {
6+
value: unknown;
7+
method() {}
8+
}
9+
10+
class HasStatic {
11+
static value = 0;
12+
static someMethod() {}
13+
14+
method() {}
15+
}
16+
17+
@decorate
18+
class HasDecorator {
19+
method() {}
20+
}

0 commit comments

Comments
 (0)