forked from simonhaenisch/prettier-plugin-organize-imports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
151 lines (123 loc) · 3.29 KB
/
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
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
const test = require('ava').default;
const prettier = require('prettier');
/**
* @param {string} code
* @param {prettier.Options} options
*/
const prettify = (code, options) => prettier.format(code, { plugins: ['.'], filepath: 'file.ts', ...options });
/**
* @param {prettier.Options['parser']} parser
*/
const getMacro = (parser) => {
/**
* @param {import('ava').Assertions} t
* @param {string} input
* @param {string} expected
* @param {object} [options]
* @param {prettier.Options} [options.options]
* @param {(res: string) => string} [options.transformer]
*/
function macro(t, input, expected, { options = {}, transformer = (res) => res.split('\n')[0] } = {}) {
const formattedCode = prettify(input, { parser, ...options });
t.is(transformer(formattedCode), expected);
}
/**
* @param {string} title
*/
macro.title = (title) => `[${parser}] ${title}`;
return macro;
};
/**
* @type {import('ava').OneOrMoreMacros<[string, string] | [string, string, { options?: prettier.Options, transformer?: (res: string) => string }], unknown>}
*/
const macros = [getMacro('typescript'), getMacro('babel'), getMacro('babel-ts')];
test(
'sorts imports',
macros,
`
import { foo, bar } from "foobar"
export const foobar = foo + bar
`,
'import { bar, foo } from "foobar";',
);
test(
'removes partially unused imports',
macros,
`
import { foo, bar, baz } from "foobar";
const foobar = foo + baz
`,
'import { baz, foo } from "foobar";',
);
test('removes completely unused imports', macros, 'import { foo } from "foobar"', '');
test(
'works with multi-line imports',
macros,
`
import {
foo,
bar,
baz,
} from "foobar";
console.log({ foo, bar, baz });
`,
'import { bar, baz, foo } from "foobar";',
);
test(
'works without a filepath',
macros,
`
import { foo, bar } from "foobar"
export const foobar = foo + bar
`,
'import { bar, foo } from "foobar";',
{ options: { filepath: undefined } },
);
test(
'files with `// organize-imports-ignore` are skipped',
macros,
`
// organize-imports-ignore
import { foo, bar } from "foobar"
export const foobar = foo + bar
`,
'import { foo, bar } from "foobar";',
{ transformer: (res) => res.split('\n')[1] },
);
test('works with TypeScript code inside Vue files', (t) => {
const code = `
<script lang="ts">
import {defineComponent,compile} from 'vue';
console.log(compile);
export default defineComponent({});
</script>
`;
const formattedCode = prettify(code, { filepath: 'file.vue' });
t.is(formattedCode.split('\n')[1], `import { compile, defineComponent } from "vue";`);
});
test('works with Vue setup scripts', (t) => {
const code = `
<script setup lang="ts">
import {defineComponent,compile} from 'vue';
export default defineComponent({});
</script>
`;
const formattedCode = prettify(code, { filepath: 'file.vue' });
t.is(formattedCode.split('\n')[1], `import { defineComponent } from "vue";`);
});
test('preserves new lines and comments in Vue files', (t) => {
const code = `<script lang="ts">
import { defineComponent, ref } from "vue";
export default defineComponent({
setup() {
// please don't break me
const test = ref("");
return { test };
},
});
</script>
<style></style>
`;
const formattedCode = prettify(code, { filepath: 'file.vue' });
t.is(formattedCode, code);
});