Skip to content

Commit 1613691

Browse files
committed
update README.md
1 parent d6b75f3 commit 1613691

File tree

6 files changed

+421
-10
lines changed

6 files changed

+421
-10
lines changed

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ Jakefile.js
1414
.settings/
1515
.travis.yml
1616
.vscode/
17-
.idea/
17+
.idea/
18+
/tools

README.md

Lines changed: 183 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,14 @@ npm install typescript-plus
3131
tsc-plus [input files] [options]
3232
```
3333

34-
35-
## Documentation
36-
3734
To learn how to use the original typescript compiler, please visit the following links:
3835

3936
* [Quick tutorial](http://www.typescriptlang.org/Tutorial)
4037
* [Programming handbook](http://www.typescriptlang.org/Handbook)
4138
* [Language specification](https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md)
4239
* [Homepage](http://www.typescriptlang.org/)
4340

44-
##Extra Options
41+
## Extra Options
4542

4643
| Option | Type | Default| Description |
4744
|:----------------------- |:-------:|:------:| :------------------------------------------------- |
@@ -50,8 +47,6 @@ To learn how to use the original typescript compiler, please visit the following
5047
| defines | Object | | Replace the global variables with the constants defined in the "defines"" object. |
5148

5249

53-
**Note: The "defines" option is only allowed in tsconfig.json, and not through command-line switches.**
54-
5550
Example tsconfig.json file:
5651

5752
```
@@ -65,7 +60,7 @@ Example tsconfig.json file:
6560
"emitReflection": true,
6661
"accessorOptimization": true,
6762
"defines": {
68-
"DEBUG": false, // the value can be boolean, number or string.
63+
"DEBUG": false,
6964
"RELEASE": true
7065
}
7166
@@ -76,4 +71,184 @@ Example tsconfig.json file:
7671
]
7772
}
7873
79-
```
74+
```
75+
76+
## Reflection
77+
78+
Pass `--emitReflection` to the command-line tool or add `"emitReflection": true` to the `compilerOptions` in tsconfig.json file to enable this feature.
79+
80+
TypeScript:
81+
82+
```
83+
namespace ts {
84+
export interface IPerson {
85+
name:string;
86+
}
87+
88+
export class Student implements IPerson {
89+
public name:string = "";
90+
}
91+
}
92+
```
93+
JavaScript:
94+
95+
```
96+
var ts;
97+
(function (ts) {
98+
var Student = (function () {
99+
function Student() {
100+
this.name = "";
101+
}
102+
return Student;
103+
}());
104+
ts.Student = Student;
105+
__reflect(Student.prototype, "ts.Student", ["ts.IPerson"]);
106+
})(ts || (ts = {}));
107+
108+
```
109+
The `__reflect` helper function is just like the `__extends` function, it is emitted only once in one file.
110+
111+
Then you can use the helper funtions in [reflection.ts](tools/reflection.ts) to get the qualified class name of an instance:
112+
113+
```
114+
let student = new ts.Student();
115+
ts.getQualifiedClassName(student); // "ts.Student"
116+
```
117+
or do some type checking:
118+
119+
```
120+
ts.is(student, "ts.Student"); // true
121+
ts.is(student, "ts.IPersion"); // true
122+
```
123+
124+
## Accessor Optimization
125+
126+
Pass `--accessorOptimization` to the command-line tool or add `"accessorOptimization": true` to the `compilerOptions` in tsconfig.json file to enable this feature.
127+
128+
As we know, we can't override the get / set accessors of super class in TypeScript. To solve the problem, we usually forward the call to the set accessor to another method:
129+
130+
TypeScript:
131+
132+
```
133+
class Student {
134+
135+
public _name:string;
136+
137+
protected setName(value:string):void {
138+
this._name = value;
139+
}
140+
141+
public get name():string {
142+
return this._name;
143+
}
144+
145+
public set name(value:string) {
146+
this.setName(value);
147+
}
148+
}
149+
```
150+
It does solve the problem, but also brings a performance issue, that two functions have to be called each time we call a set accessor. With the `--accessorOptimization` switch on, if the accessor contains only one call to another method, the compiler use that method to define accessor directly.
151+
152+
Javascript:
153+
154+
```
155+
var Student = (function () {
156+
function Student() {
157+
}
158+
Student.prototype.setName = function (value) {
159+
this._name = value;
160+
};
161+
Object.defineProperty(Student.prototype, "name", {
162+
get: function () {
163+
return this._name;
164+
},
165+
set: Student.prototype.setName,
166+
enumerable: true,
167+
configurable: true
168+
});
169+
return Student;
170+
}());
171+
```
172+
173+
If you define the `setName()` method after the set accessor, the final result looks like this:
174+
175+
```
176+
var Student = (function () {
177+
function Student() {
178+
}
179+
Object.defineProperty(Student.prototype, "name", {
180+
get: function () {
181+
return this._name;
182+
},
183+
set: setName,
184+
enumerable: true,
185+
configurable: true
186+
});
187+
Student.prototype.setName = setName;
188+
function setName(value) {
189+
this._name = value;
190+
};
191+
return Student;
192+
}());
193+
```
194+
Either way, it works.
195+
196+
## Conditional Compilation
197+
198+
The `defines` option is only allowed in tsconfig.json, and not through command-line switches.
199+
200+
You can use the `defines` option to declare global variables that the compiler will assume to be constants (unless defined in scope). Then all the defined global variables will be replaced with the corresponding constants. For example:
201+
202+
tsconfig.json:
203+
204+
```
205+
{
206+
"compilerOptions": {
207+
"defines": {
208+
"DEBUG": false,
209+
"LANGUAGE": "en_US"
210+
}
211+
}
212+
}
213+
214+
```
215+
TypeScript:
216+
217+
```
218+
declare var DEBUG:boolean;
219+
declare var LANGUAGE:string;
220+
221+
if (DEBUG) {
222+
console.log("DEBUG is true");
223+
}
224+
225+
console.log("The language is : " + LANGUAGE);
226+
227+
function someFunction():void {
228+
let DEBUG = true;
229+
if (DEBUG) {
230+
console.log("DEBUG is true");
231+
}
232+
}
233+
234+
```
235+
JavaScript:
236+
237+
```
238+
if (false) {
239+
console.log("DEBUG is true");
240+
}
241+
242+
console.log("The language is : " + "en_US");
243+
244+
function someFunction() {
245+
var DEBUG = true;
246+
if (DEBUG) {
247+
console.log("DEBUG is true");
248+
}
249+
}
250+
```
251+
As you can see, the second `if(DEBUG)` in `someFunction` is not replaced because it is defined in scope.
252+
253+
Note that the compiler does not dropping the unreachable code, because it is can be easily done by other tools like [UglifyJS](http://lisperator.net/uglifyjs/) or [Google Closure Compiler](https://developers.google.com/closure/compiler/).
254+

src/compiler/emitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5275,7 +5275,7 @@ const _super = (function (geti, seti) {
52755275
}
52765276

52775277
/**
5278-
* If the accessor method contains only one call to another method, use that method as the accessor directly.
5278+
* If the accessor method contains only one call to another method, use that method to define the accessor directly.
52795279
*/
52805280
function emitAccessorOrTargetMethod(accessor:AccessorDeclaration,
52815281
member:ClassElement,

tools/reflection.d.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//////////////////////////////////////////////////////////////////////////////////////
2+
//
3+
// The MIT License (MIT)
4+
//
5+
// Copyright (c) 2015-present, Dom Chen.
6+
// All rights reserved.
7+
//
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy of
9+
// this software and associated documentation files (the "Software"), to deal in the
10+
// Software without restriction, including without limitation the rights to use, copy,
11+
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
12+
// and to permit persons to whom the Software is furnished to do so, subject to the
13+
// following conditions:
14+
//
15+
// The above copyright notice and this permission notice shall be included in all
16+
// copies or substantial portions of the Software.
17+
//
18+
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
//
25+
//////////////////////////////////////////////////////////////////////////////////////
26+
27+
declare namespace ts {
28+
/**
29+
* Returns a reference to the class object of the class specified by the name parameter.
30+
* @param name The name of a class.
31+
*/
32+
function getDefinitionByName(name: string): any;
33+
/**
34+
* Return the fully qualified class name of an object
35+
* @param value The object for which a fully qualified class name is desired. Any JavaScript value may be passed to
36+
* this method including all available JavaScript types, object instances, primitive types such as number, and class objects.
37+
* @returns A string containing the fully qualified class name.
38+
*/
39+
function getQualifiedClassName(value: any): string;
40+
/**
41+
* Returns the fully qualified class name of the base class of the object specified by the value parameter.
42+
* @param value The object for which a parent class is desired. Any JavaScript value may be passed to this method including
43+
* all available JavaScript types, object instances, primitive types such as number, and class objects.
44+
* @returns A fully qualified base class name, or null if none exists.
45+
*/
46+
function getQualifiedSuperclassName(value: any): string;
47+
/**
48+
* Indicates whether an object is a instance of the class or interface specified as the parameter.This method can indicate
49+
* whether an object is a instance of the specific interface even though the interface does not exist at JavaScript runtime.
50+
* @param instance the instance to be checked.
51+
* @param typeName the string value representing a specific class or interface.
52+
* @returns A value of true if the object is a instance of the class or interface specified as the parameter.
53+
* @example
54+
* <pre>
55+
* var instance = new ts.Sprite();
56+
* console.log(ts.is(instance,"ts.Sprite")) // true
57+
* console.log(ts.is(instance,"ts.DisplayObjectContainer")) // true, because ts.DisplayObjectContainer is the superclass of ts.Sprite.
58+
* console.log(ts.is(instance,"ts.Bitmap")) // false, because ts.Bitmap is not the superclass of ts.Sprite.
59+
* </pre>
60+
*/
61+
function is(instance: any, typeName: string): boolean;
62+
}
63+
declare var __global: any;

tools/reflection.min.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//////////////////////////////////////////////////////////////////////////////////////
2+
//
3+
// The MIT License (MIT)
4+
//
5+
// Copyright (c) 2015-present, Dom Chen.
6+
// All rights reserved.
7+
//
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy of
9+
// this software and associated documentation files (the "Software"), to deal in the
10+
// Software without restriction, including without limitation the rights to use, copy,
11+
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
12+
// and to permit persons to whom the Software is furnished to do so, subject to the
13+
// following conditions:
14+
//
15+
// The above copyright notice and this permission notice shall be included in all
16+
// copies or substantial portions of the Software.
17+
//
18+
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
//
25+
//////////////////////////////////////////////////////////////////////////////////////
26+
27+
var ts,__global
28+
!function(t){function e(t){var e,r,o,n,u
29+
if(!t)return null
30+
if(e=l[t])return e
31+
for(r=t.split("."),o=r.length,e=__global,n=0;o>n;n++)if(u=r[n],e=e[u],!e)return null
32+
return l[t]=e,e}function r(t){var e,r,o,n,l=typeof t
33+
return!t||"object"!=l&&!t.prototype?l:(e=t.prototype?t.prototype:Object.getPrototypeOf(t),e.hasOwnProperty("__class__")?e.__class__:(r=(""+e.constructor).trim(),o=r.indexOf("("),n=r.substring(9,o),Object.defineProperty(e,"__class__",{value:n,enumerable:!1,writable:!0}),n))}function o(t){var e,o,n
34+
return!t||"object"!=typeof t&&!t.prototype?null:(e=t.prototype?t.prototype:Object.getPrototypeOf(t),(o=Object.getPrototypeOf(e))?(n=r(o.constructor),n?n:null):null)}function n(t,e){var r,o
35+
return t&&"object"==typeof t?(r=Object.getPrototypeOf(t),o=r?r.__types__:null,o?-1!==o.indexOf(e):!1):!1}var l={}
36+
t.getDefinitionByName=e,t.getQualifiedClassName=r,t.getQualifiedSuperclassName=o,t.is=n}(ts||(ts={})),__global=this.__global||this
37+

0 commit comments

Comments
 (0)