-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremove-unused.test.js
106 lines (81 loc) · 2.31 KB
/
remove-unused.test.js
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
const removeUnused = require('./remove-unused');
const options = {
keepImports: ['React'],
keepVars: [],
};
test('should remove unused named import', () => {
const input = `
import React from 'react';
import { Component } from 'react';
import dd from '../somefile.js';
class App extends React.Component {
}
export default App;
`;
const output = removeUnused(input, options);
expect(output).not.toMatch(/import dd from '\.\.\/somefile\.js';/);
});
test('should remove unused default import', () => {
const input = `
import React from 'react';
import X from 'x';
class App extends React.Component {
}
export default App;
`;
const output = removeUnused(input, options);
expect(output).not.toMatch(/import X from 'x';/);
});
test('should remove unused class field', () => {
const input = `
import React from 'react';
class App extends React.Component {
constructor() {
super();
this.unusedVariable = 'This variable is not used.';
}
}
export default App;
`;
const output = removeUnused(input, options);
expect(output).not.toMatch(/this\.unusedVariable = 'This variable is not used.';/);
});
test('should not remove whitelisted import', () => {
const input = `
import React from 'react';
import PropTypes from 'prop-types';
class App extends React.Component {
}
export default App;
`;
const output = removeUnused(input, { ...options, keepImports: ['React', 'PropTypes'] });
expect(output).toMatch(/import PropTypes from 'prop-types';/);
});
test('should remove unused local const variable', () => {
const input = `
import React from 'react';
class App extends React.Component {
render() {
const unusedLocalVariable = 'This local variable is not used.';
return <div>Hello, world!</div>;
}
}
export default App;
`;
const output = removeUnused(input, options);
expect(output).not.toMatch(/const unusedLocalVariable = 'This local variable is not used.';/);
});
test('should remove unused local let variable', () => {
const input = `
import React from 'react';
class App extends React.Component {
render() {
let unusedLocalVariable = 'This local variable is not used.';
return <div>Hello, world!</div>;
}
}
export default App;
`;
const output = removeUnused(input, options);
expect(output).not.toMatch(/let unusedLocalVariable = 'This local variable is not used.';/);
});