forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprefix-functions_spec.ts
182 lines (156 loc) · 5.37 KB
/
prefix-functions_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// tslint:disable-next-line:no-implicit-dependencies
import { tags } from '@angular-devkit/core';
import { transformJavascript } from '../helpers/transform-javascript';
import { getPrefixFunctionsTransformer } from './prefix-functions';
const transform = (content: string) => transformJavascript(
{ content, getTransforms: [getPrefixFunctionsTransformer] }).content;
describe('prefix-functions', () => {
const emptyImportsComment = '/** PURE_IMPORTS_START PURE_IMPORTS_END */';
const clazz = 'var Clazz = (function () { function Clazz() { } return Clazz; }());';
describe('pure imports', () => {
it('adds import list', () => {
const input = tags.stripIndent`
import { Injectable } from '@angular/core';
import { Something } from './relative/pure_import';
var foo = Injectable;
var bar = Something;
`;
const output = tags.stripIndent`
/** PURE_IMPORTS_START _angular_core,_relative_pure_import PURE_IMPORTS_END */
${input}
`;
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
it('adds import list even with no imports', () => {
const input = tags.stripIndent`
var foo = 42;
`;
const output = tags.stripIndent`
${emptyImportsComment}
${input}
`;
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
});
describe('pure functions', () => {
it('adds comment to new calls', () => {
const input = tags.stripIndent`
var newClazz = new Clazz();
`;
const output = tags.stripIndent`
${emptyImportsComment}
var newClazz = /*@__PURE__*/ new Clazz();
`;
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
it('adds comment to function calls', () => {
const input = tags.stripIndent`
var newClazz = Clazz();
`;
const output = tags.stripIndent`
${emptyImportsComment}
var newClazz = /*@__PURE__*/ Clazz();
`;
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
it('adds comment outside of IIFEs', () => {
const input = tags.stripIndent`
${clazz}
var ClazzTwo = (function () { function Clazz() { } return Clazz; })();
`;
const output = tags.stripIndent`
${emptyImportsComment}
var Clazz = /*@__PURE__*/ (function () { function Clazz() { } return Clazz; }());
var ClazzTwo = /*@__PURE__*/ (function () { function Clazz() { } return Clazz; })();
`;
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
it('doesn\'t add comment when inside function declarations or expressions', () => {
const input = tags.stripIndent`
function funcDecl() {
var newClazz = Clazz();
var newClazzTwo = new Clazz();
}
var funcExpr = function () {
var newClazz = Clazz();
var newClazzTwo = new Clazz();
};
`;
const output = tags.stripIndent`
${emptyImportsComment}
${input}
`;
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
it('doesn\'t add comment to downlevel namespaces', () => {
const input = tags.stripIndent`
function MyFunction() { }
(function (MyFunction) {
function subFunction() { }
MyFunction.subFunction = subFunction;
})(MyFunction || (MyFunctionn = {}));
export { MyFunction };
`;
const output = tags.stripIndent`
${emptyImportsComment}
${input}
`;
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
it('doesn\'t add comment when inside class', () => {
const input = tags.stripIndent`
class Foo {
constructor(e) {
super(e);
}
method() {
var newClazz = new Clazz();
}
}
`;
const output = tags.stripIndent`
${emptyImportsComment}
${input}
`;
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
it('doesn\'t add comment when inside arrow function', () => {
const input = tags.stripIndent`
export const subscribeToArray = (array) => (subscriber) => {
for (let i = 0, len = array.length; i < len && !subscriber.closed; i++) {
subscriber.next(array[i]);
}
if (!subscriber.closed) {
subscriber.complete();
}
};
`;
const output = tags.stripIndent`
${emptyImportsComment}
${input}
`;
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
it('doesn\'t add comment when inside object literal method', () => {
const input = tags.stripIndent`
const literal = {
method() {
var newClazz = new Clazz();
}
};
`;
const output = tags.stripIndent`
${emptyImportsComment}
${input}
`;
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
});
});