|
| 1 | +import { applyTransform } from '@codeshift/test-utils'; |
| 2 | +import * as transformer from './transform'; |
| 3 | + |
| 4 | +describe('javascript#remove-unused-vars transform', () => { |
| 5 | + it('should remove unused variables', async () => { |
| 6 | + const result = await applyTransform( |
| 7 | + transformer, |
| 8 | + ` |
| 9 | +const x = 1; |
| 10 | +const y = 2; |
| 11 | +console.log(y); |
| 12 | + `, |
| 13 | + ); |
| 14 | + |
| 15 | + expect(result).toMatchInlineSnapshot(` |
| 16 | + "const y = 2; |
| 17 | + console.log(y);" |
| 18 | + `); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should not remove used variables', async () => { |
| 22 | + const result = await applyTransform( |
| 23 | + transformer, |
| 24 | + ` |
| 25 | +const x = 1; |
| 26 | +const y = 2; |
| 27 | +console.log(x + y); |
| 28 | + `, |
| 29 | + ); |
| 30 | + expect(result).toMatchInlineSnapshot(` |
| 31 | + "const x = 1; |
| 32 | + const y = 2; |
| 33 | + console.log(x + y);" |
| 34 | + `); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should remove unused variable in nested scope', async () => { |
| 38 | + const result = await applyTransform( |
| 39 | + transformer, |
| 40 | + ` |
| 41 | +{ |
| 42 | + if (true) { |
| 43 | + const a = 1; |
| 44 | + } |
| 45 | +}`, |
| 46 | + ); |
| 47 | + |
| 48 | + expect(result).toMatchInlineSnapshot(` |
| 49 | + "{ |
| 50 | + if (true) {} |
| 51 | + }" |
| 52 | + `); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should remove unused function in nested scope', async () => { |
| 56 | + const result = await applyTransform( |
| 57 | + transformer, |
| 58 | + ` |
| 59 | +function foo() { |
| 60 | + if (true) { |
| 61 | + const a = 1; |
| 62 | + } |
| 63 | +}`, |
| 64 | + ); |
| 65 | + |
| 66 | + expect(result).toMatchInlineSnapshot(`""`); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should remove unused variable in for loop', async () => { |
| 70 | + const result = await applyTransform( |
| 71 | + transformer, |
| 72 | + ` |
| 73 | +for (let i = 0; i < 10; i++) { |
| 74 | + const a = i; |
| 75 | +} |
| 76 | + `, |
| 77 | + ); |
| 78 | + |
| 79 | + expect(result).toMatchInlineSnapshot(`"for (let i = 0; i < 10; i++) {}"`); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should remove unused variable in destructuring assignment', async () => { |
| 83 | + const result = await applyTransform( |
| 84 | + transformer, |
| 85 | + ` |
| 86 | +const { a, b } = { a: 1, b: 2 }; |
| 87 | +console.log(a); |
| 88 | + `, |
| 89 | + ); |
| 90 | + |
| 91 | + expect(result).toMatchInlineSnapshot(` |
| 92 | + "const { |
| 93 | + a |
| 94 | + } = { a: 1, b: 2 }; |
| 95 | + console.log(a);" |
| 96 | + `); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should remove unused function argument', async () => { |
| 100 | + const result = await applyTransform( |
| 101 | + transformer, |
| 102 | + ` |
| 103 | +function foo(a, b) { |
| 104 | + console.log(a); |
| 105 | +} |
| 106 | +foo(1); |
| 107 | + `, |
| 108 | + ); |
| 109 | + |
| 110 | + expect(result).toMatchInlineSnapshot(` |
| 111 | + "function foo(a) { |
| 112 | + console.log(a); |
| 113 | + } |
| 114 | + foo(1);" |
| 115 | + `); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should not remove used variable in conditional', async () => { |
| 119 | + const result = await applyTransform( |
| 120 | + transformer, |
| 121 | + ` |
| 122 | +let a; |
| 123 | +if (true) { |
| 124 | + a = 1; |
| 125 | +} |
| 126 | +console.log(a); |
| 127 | + `, |
| 128 | + ); |
| 129 | + |
| 130 | + expect(result).toMatchInlineSnapshot(` |
| 131 | + "let a; |
| 132 | + if (true) { |
| 133 | + a = 1; |
| 134 | + } |
| 135 | + console.log(a);" |
| 136 | + `); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should remove unused function declaration', async () => { |
| 140 | + const result = await applyTransform( |
| 141 | + transformer, |
| 142 | + ` |
| 143 | +function foo() { |
| 144 | + console.log('Hello, world!'); |
| 145 | +} |
| 146 | + `, |
| 147 | + ); |
| 148 | + |
| 149 | + expect(result).toMatchInlineSnapshot(`""`); |
| 150 | + }); |
| 151 | +}); |
0 commit comments