Skip to content

Commit 6baeaa8

Browse files
author
Sagi
committed
optimizing experimentalDecorators feature
1 parent 9a8e575 commit 6baeaa8

File tree

6 files changed

+72
-10
lines changed

6 files changed

+72
-10
lines changed

README.md

+54-4
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ All you need to do is specify the list of extern files after the `externs` optio
5555
tscc app.ts --module commonjs --externs externs/app-extern.d.ts...
5656
```
5757

58-
You can also specify the files in a `ts.config` file.<br/>
59-
use the `project` option to locate the ts.config file:<br/>
58+
You can also specify the files in a `tsconfig.json` file.<br/>
59+
use the `project` option to locate the tsconfig.json file:<br/>
6060
```js
6161
tscc --project [project specific directory]
6262
```
63-
and declare the options in the `ts.config` file:
63+
and declare the options in the `tsconfig.json` file:
6464
```js
6565
{
6666
"compilerOptions": {
@@ -130,6 +130,56 @@ var EventType = {
130130
mousedown: 1
131131
};
132132
```
133+
Ignore experimentalDecorators warning message
134+
135+
### experimentalDecorators and ignoreDecoratorsWarning
136+
In case you annotate your class/methods/params with decorators without enabling the `experimentalDecorators` option,
137+
`TypeScript` will emit all the code that enables this feature, but will output a warning message to enable this option.
138+
```js
139+
function f() {
140+
console.log("f(): evaluated");
141+
return function (target, propertyKey: string, descriptor: PropertyDescriptor) {
142+
console.log("f(): called");
143+
}
144+
}
145+
146+
class C {
147+
@f()
148+
method() {}
149+
}
150+
```
151+
The output will be:
152+
> Experimental support for decorators is a feature that is subject to change in a future release.<br/>
153+
> Set the 'experimentalDecorators' option to remove this warning.
154+
155+
`typescript-closure-compiler` changes this behaviour and omits all decorators relevant code when the `experimentalDecorators` is not enabled, thus ensuring that the generated javascript will not include unnecessary code.<br/>
156+
In addition `typescript-closure-compiler` enables you to use the `ignoreDecoratorsWarning` option in order to ignore the warning message.<br/>
157+
These two options enables you to write your code once using decorations, but to omit the decorations related code using configuration, much like choosing the verbosity of a logger using configuration.<br/><br/>
158+
A reasonable scenario would be to decorate your class/methods/params with decorators for debug purposes but to omit this code in the final release.<br/>
159+
All you have to do is create two tsconfig.json files one for debug and one for release.<br/>
160+
The release file should include the `ignoreDecoratorsWarning`.
161+
The debug file should include the `experimentalDecorators`.
162+
163+
#### release
164+
```js
165+
{
166+
"compilerOptions": {
167+
  "ignoreDecoratorsWarning": true
168+
}
169+
"files": [
170+
]
171+
}
172+
```
173+
#### debug
174+
```js
175+
{
176+
"compilerOptions": {
177+
"experimentalDecorators": true
178+
}
179+
"files": [
180+
]
181+
}
182+
```
133183

134184
### Usage Examples
135185

@@ -170,4 +220,4 @@ jake build
170220

171221
## License
172222

173-
Like the TypeScript compiler itself, this code is licensed under the [Apache License 2.0](http://typescript.codeplex.com/license).
223+
Like the TypeScript compiler itself, this code is licensed under the [Apache License 2.0](http://typescript.codeplex.com/license).

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "typescript-closure-compiler",
33
"author": "Sagi Fogel",
4-
"version": "1.8.3",
4+
"version": "1.8.4",
55
"description": "Patches the TypeScript compiler to generate JSDoc annotations",
66
"dependencies": {
77
},

src/compiler/checker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace ts {
4747
const emptyArray: any[] = [];
4848
const emptySymbols: SymbolTable = {};
4949

50-
const compilerOptions = host.getCompilerOptions();
50+
const compilerOptions = <ExtendedCompilerOptions>host.getCompilerOptions();
5151
const languageVersion = compilerOptions.target || ScriptTarget.ES3;
5252
const modulekind = getEmitModuleKind(compilerOptions);
5353
const allowSyntheticDefaultImports = typeof compilerOptions.allowSyntheticDefaultImports !== "undefined" ? compilerOptions.allowSyntheticDefaultImports : modulekind === ModuleKind.System;
@@ -12851,7 +12851,7 @@ namespace ts {
1285112851
return;
1285212852
}
1285312853

12854-
if (!compilerOptions.experimentalDecorators) {
12854+
if (!compilerOptions.experimentalDecorators && !compilerOptions.ignoreDecoratorsWarning) {
1285512855
error(node, Diagnostics.Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_the_experimentalDecorators_option_to_remove_this_warning);
1285612856
}
1285712857

src/compiler/commandLineParser.ts

+4
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ namespace ts {
333333
{
334334
name: "emitOneSideEnums",
335335
type: "boolean"
336+
},
337+
{
338+
name: "ignoreDecoratorsWarning",
339+
type: "boolean"
336340
}
337341
];
338342

src/compiler/emitter.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ namespace ts {
4242
externsOutFile?: string
4343
emitAnnotations: boolean;
4444
emitOneSideEnums: boolean;
45+
ignoreDecoratorsWarning: boolean;
4546
}
4647

4748
export interface ExtendedParsedCommandLine extends ParsedCommandLine {
@@ -7540,6 +7541,9 @@ const _super = (function (geti, seti) {
75407541
if (decoratedClassAlias !== undefined) {
75417542
write(` = ${decoratedClassAlias}`);
75427543
}
7544+
else {
7545+
forceWriteLine();
7546+
}
75437547

75447548
write(" = __decorate([");
75457549
increaseIndent();
@@ -9616,7 +9620,7 @@ const _super = (function (geti, seti) {
96169620
decorateEmitted = true;
96179621
}
96189622

9619-
if (!paramEmitted && node.flags & NodeFlags.HasParamDecorators) {
9623+
if (compilerOptions.experimentalDecorators && !paramEmitted && node.flags & NodeFlags.HasParamDecorators) {
96209624
emitParamDecorateAnnotation();
96219625
writeLines(paramHelper);
96229626
paramEmitted = true;

typescript.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -27491,7 +27491,7 @@ ts.createTypeChecker = function (host, produceDiagnostics) {
2749127491
if (!ts.nodeCanBeDecorated(node)) {
2749227492
return;
2749327493
}
27494-
if (!compilerOptions.experimentalDecorators) {
27494+
if (!compilerOptions.experimentalDecorators && !compilerOptions.ignoreDecoratorsWarning) {
2749527495
error(node, ts.Diagnostics.Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_the_experimentalDecorators_option_to_remove_this_warning);
2749627496
}
2749727497
if (compilerOptions.emitDecoratorMetadata) {
@@ -41620,7 +41620,7 @@ ts.emitFiles = function (typeChecker, resolver, host, targetSourceFile) {
4162041620
}
4162141621
decorateEmitted = true;
4162241622
}
41623-
if (!paramEmitted && node.flags & 16777216 /* HasParamDecorators */) {
41623+
if (compilerOptions.experimentalDecorators && !paramEmitted && node.flags & 16777216 /* HasParamDecorators */) {
4162441624
emitParamDecorateAnnotation();
4162541625
writeLines(paramHelper);
4162641626
paramEmitted = true;
@@ -43720,6 +43720,10 @@ ts.optionDeclarations = [
4372043720
{
4372143721
name: "emitOneSideEnums",
4372243722
type: "boolean"
43723+
},
43724+
{
43725+
name: "ignoreDecoratorsWarning",
43726+
type: "boolean"
4372343727
}
4372443728
];
4372543729
/* @internal */

0 commit comments

Comments
 (0)