|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { Replacement } from 'tslint/lib'; |
| 3 | +import { getFailureMessage, Rule } from '../src/importDestructuringSpacingRule'; |
1 | 4 | import { assertAnnotated, assertSuccess } from './testHelper'; |
2 | 5 |
|
3 | | -describe('import-destructuring-spacing', () => { |
4 | | - describe('invalid import spacing', () => { |
5 | | - it('should fail when the imports have no spaces', () => { |
6 | | - let source = ` |
| 6 | +const { |
| 7 | + metadata: { ruleName } |
| 8 | +} = Rule; |
| 9 | + |
| 10 | +describe(ruleName, () => { |
| 11 | + describe('failure', () => { |
| 12 | + it('should fail when a single import has no spaces', () => { |
| 13 | + const source = ` |
7 | 14 | import {Foo} from './foo' |
8 | 15 | ~~~~~ |
9 | 16 | `; |
10 | 17 | assertAnnotated({ |
11 | | - ruleName: 'import-destructuring-spacing', |
12 | | - message: "You need to leave whitespaces inside of the import statement's curly braces", |
| 18 | + message: getFailureMessage(), |
| 19 | + ruleName, |
13 | 20 | source |
14 | 21 | }); |
15 | 22 | }); |
16 | 23 |
|
17 | | - it('should fail with multiple items to import', () => { |
18 | | - let source = ` |
| 24 | + it('should fail when multiple imports have no spaces', () => { |
| 25 | + const source = ` |
19 | 26 | import {Foo,Bar} from './foo' |
20 | 27 | ~~~~~~~~~ |
21 | 28 | `; |
22 | | - assertAnnotated({ |
23 | | - ruleName: 'import-destructuring-spacing', |
24 | | - message: "You need to leave whitespaces inside of the import statement's curly braces", |
25 | | - source |
26 | | - }); |
| 29 | + assertAnnotated({ message: getFailureMessage(), ruleName, source }); |
27 | 30 | }); |
28 | 31 |
|
29 | | - it('should fail with spaces between items', () => { |
30 | | - let source = ` |
31 | | - import {Foo, Bar} from './foo' |
32 | | - ~~~~~~~~~~~ |
33 | | - `; |
34 | | - assertAnnotated({ |
35 | | - ruleName: 'import-destructuring-spacing', |
36 | | - message: "You need to leave whitespaces inside of the import statement's curly braces", |
37 | | - source |
38 | | - }); |
39 | | - }); |
40 | | - |
41 | | - it('should fail with only one whitespace in the left', () => { |
42 | | - let source = ` |
| 32 | + it('should fail when there are no trailing spaces', () => { |
| 33 | + const source = ` |
43 | 34 | import { Foo} from './foo'; |
44 | 35 | ~~~~~~ |
45 | 36 | `; |
46 | 37 | assertAnnotated({ |
47 | | - ruleName: 'import-destructuring-spacing', |
48 | | - message: "You need to leave whitespaces inside of the import statement's curly braces", |
| 38 | + message: getFailureMessage(), |
| 39 | + ruleName, |
49 | 40 | source |
50 | 41 | }); |
51 | 42 | }); |
52 | 43 |
|
53 | | - it('should fail with only one whitespace in the right', () => { |
54 | | - let source = ` |
| 44 | + it('should fail when there are no leading spaces', () => { |
| 45 | + const source = ` |
55 | 46 | import {Foo } from './foo'; |
56 | 47 | ~~~~~~ |
57 | 48 | `; |
58 | 49 | assertAnnotated({ |
59 | | - ruleName: 'import-destructuring-spacing', |
60 | | - message: "You need to leave whitespaces inside of the import statement's curly braces", |
| 50 | + message: getFailureMessage(), |
| 51 | + ruleName, |
61 | 52 | source |
62 | 53 | }); |
63 | 54 | }); |
64 | 55 | }); |
65 | 56 |
|
66 | | - describe('valid import spacing', () => { |
| 57 | + describe('failure with replacements', () => { |
| 58 | + it('should fail and apply proper replacements when there are no spaces', () => { |
| 59 | + const source = ` |
| 60 | + import {Foo} from './foo'; |
| 61 | + ~~~~~ |
| 62 | + `; |
| 63 | + const failures = assertAnnotated({ message: getFailureMessage(), ruleName, source }); |
| 64 | + |
| 65 | + if (!Array.isArray(failures)) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + const replacement = Replacement.applyFixes(source, failures.map(f => f.getFix())); |
| 70 | + |
| 71 | + expect(replacement).to.eq(` |
| 72 | + import { Foo } from './foo'; |
| 73 | + ~~~~~ |
| 74 | + `); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should fail and apply proper replacements when there is more than one leading space', () => { |
| 78 | + const source = ` |
| 79 | + import { Bar, BarFoo, Foo } from './foo'; |
| 80 | + ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 81 | + `; |
| 82 | + const failures = assertAnnotated({ message: getFailureMessage(), ruleName, source }); |
| 83 | + |
| 84 | + if (!Array.isArray(failures)) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + const replacement = Replacement.applyFixes(source, failures.map(f => f.getFix())); |
| 89 | + |
| 90 | + expect(replacement).to.eq(` |
| 91 | + import { Bar, BarFoo, Foo } from './foo'; |
| 92 | + ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 93 | + `); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should fail and apply proper replacements when there is more than one trailing space', () => { |
| 97 | + const source = ` |
| 98 | + import { Bar, BarFoo, Foo } from './foo'; |
| 99 | + ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 100 | + `; |
| 101 | + const failures = assertAnnotated({ message: getFailureMessage(), ruleName, source }); |
| 102 | + |
| 103 | + if (!Array.isArray(failures)) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + const replacement = Replacement.applyFixes(source, failures.map(f => f.getFix())); |
| 108 | + |
| 109 | + expect(replacement).to.eq(` |
| 110 | + import { Bar, BarFoo, Foo } from './foo'; |
| 111 | + ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 112 | + `); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should fail and apply proper replacements when there is more than one space left and right', () => { |
| 116 | + const source = ` |
| 117 | + import { Bar, BarFoo, Foo } from './foo'; |
| 118 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 119 | + `; |
| 120 | + const failures = assertAnnotated({ message: getFailureMessage(), ruleName, source }); |
| 121 | + |
| 122 | + if (!Array.isArray(failures)) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + const replacement = Replacement.applyFixes(source, failures.map(f => f.getFix())); |
| 127 | + |
| 128 | + expect(replacement).to.eq(` |
| 129 | + import { Bar, BarFoo, Foo } from './foo'; |
| 130 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 131 | + `); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('success', () => { |
67 | 136 | it('should succeed with valid spacing', () => { |
68 | | - let source = "import { Foo } from './foo';"; |
69 | | - assertSuccess('import-destructuring-spacing', source); |
| 137 | + const source = ` |
| 138 | + import { Foo } from './foo'; |
| 139 | + `; |
| 140 | + assertSuccess(ruleName, source); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should succeed with multiple spaces between imports', () => { |
| 144 | + const source = ` |
| 145 | + import { Bar, Foo } from './foo'; |
| 146 | + `; |
| 147 | + assertSuccess(ruleName, source); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should succeed for blank imports', () => { |
| 151 | + const source = ` |
| 152 | + import {} from 'foo'; |
| 153 | + `; |
| 154 | + assertSuccess(ruleName, source); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should succeed for module imports', () => { |
| 158 | + const source = ` |
| 159 | + import foo = require('./foo'); |
| 160 | + `; |
| 161 | + assertSuccess(ruleName, source); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should succeed for patch imports', () => { |
| 165 | + const source = ` |
| 166 | + import 'rxjs/add/operator/map'; |
| 167 | + `; |
| 168 | + assertSuccess(ruleName, source); |
70 | 169 | }); |
71 | 170 |
|
72 | | - it('should work with alias imports', () => { |
73 | | - let source = "import * as Foo from './foo';"; |
74 | | - assertSuccess('import-destructuring-spacing', source); |
| 171 | + it('should succeed with alias imports', () => { |
| 172 | + const source = ` |
| 173 | + import * as Foo from './foo'; |
| 174 | + `; |
| 175 | + assertSuccess(ruleName, source); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should succeed for alias imports inside braces', () => { |
| 179 | + const source = ` |
| 180 | + import { default as _rollupMoment, Moment } from 'moment'; |
| 181 | + `; |
| 182 | + assertSuccess(ruleName, source); |
| 183 | + }); |
| 184 | + |
| 185 | + it('should succeed for multiline imports', () => { |
| 186 | + const source = ` |
| 187 | + import { |
| 188 | + Bar, |
| 189 | + BarFoo, |
| 190 | + Foo |
| 191 | + } from './foo'; |
| 192 | + `; |
| 193 | + assertSuccess(ruleName, source); |
75 | 194 | }); |
76 | 195 | }); |
77 | 196 | }); |
0 commit comments