-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathpure-toplevel-functions_spec.ts
108 lines (92 loc) · 3.41 KB
/
pure-toplevel-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
/**
* @license
* Copyright Google LLC 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
*/
import { transform } from '@babel/core';
import { default as pureTopLevelPlugin } from './pure-toplevel-functions';
// eslint-disable-next-line import/no-extraneous-dependencies
const prettier = require('prettier');
function testCase({ input, expected }: { input: string; expected: string }): void {
const result = transform(input, {
configFile: false,
babelrc: false,
compact: true,
plugins: [pureTopLevelPlugin],
});
if (!result) {
fail('Expected babel to return a transform result.');
} else {
expect(prettier.format(result.code, { parser: 'babel' })).toEqual(
prettier.format(expected, { parser: 'babel' }),
);
}
}
function testCaseNoChange(input: string): void {
testCase({ input, expected: input });
}
describe('pure-toplevel-functions Babel plugin', () => {
it('annotates top-level new expressions', () => {
testCase({
input: 'var result = new SomeClass();',
expected: 'var result = /*#__PURE__*/ new SomeClass();',
});
});
it('annotates top-level function calls', () => {
testCase({
input: 'var result = someCall();',
expected: 'var result = /*#__PURE__*/ someCall();',
});
});
it('annotates top-level IIFE assignments with no arguments', () => {
testCase({
input: 'var SomeClass = (function () { function SomeClass() { } return SomeClass; })();',
expected:
'var SomeClass = /*#__PURE__*/(function () { function SomeClass() { } return SomeClass; })();',
});
});
it('does not annotate top-level IIFE assignments with arguments', () => {
testCaseNoChange(
'var SomeClass = (function () { function SomeClass() { } return SomeClass; })(abc);',
);
});
it('does not annotate call expressions inside function declarations', () => {
testCaseNoChange('function funcDecl() { const result = someFunction(); }');
});
it('does not annotate call expressions inside function expressions', () => {
testCaseNoChange('const foo = function funcDecl() { const result = someFunction(); }');
});
it('does not annotate call expressions inside function expressions', () => {
testCaseNoChange('const foo = () => { const result = someFunction(); }');
});
it('does not annotate new expressions inside function declarations', () => {
testCaseNoChange('function funcDecl() { const result = new SomeClass(); }');
});
it('does not annotate new expressions inside function expressions', () => {
testCaseNoChange('const foo = function funcDecl() { const result = new SomeClass(); }');
});
it('does not annotate new expressions inside function expressions', () => {
testCaseNoChange('const foo = () => { const result = new SomeClass(); }');
});
it('does not annotate TypeScript helper functions (tslib)', () => {
testCaseNoChange(`
class LanguageState {}
__decorate([
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], LanguageState.prototype, "checkLanguage", null);
`);
});
it('does not annotate object literal methods', () => {
testCaseNoChange(`
const literal = {
method() {
var newClazz = new Clazz();
}
};
`);
});
});