forked from vuejs/eslint-plugin-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautofix.js
217 lines (196 loc) · 5.31 KB
/
autofix.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* @author Yosuke Ota <https://github.com/ota-meshi>
* See LICENSE file in root directory for full license.
*/
'use strict'
const Linter = require('eslint').Linter
const parser = require('vue-eslint-parser')
const assert = require('assert')
const rules = require('../..').rules
const baseConfig = {
parser: 'vue-eslint-parser',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
jsx: true
}
}
}
describe('Complex autofix test cases', () => {
const linter = new Linter()
linter.defineParser('vue-eslint-parser', parser)
for (const key of Object.keys(rules)) {
const ruleId = `vue/${key}`
linter.defineRule(ruleId, rules[key])
}
// https://github.com/vuejs/eslint-plugin-vue/issues/566
describe('Autofix of `vue/order-in-components` and `comma-dangle` should not conflict.', () => {
const config = Object.assign({}, baseConfig, {
rules: {
'vue/order-in-components': ['error'],
'comma-dangle': ['error', 'always']
}
})
it('Even if set comma-dangle:always, the output should be as expected.', () => {
const code = `
<script>
export default {
data() {
},
name: 'burger'
};
</script>`
const output = `
<script>
export default {
name: 'burger',
data() {
},
};
</script>`
assert.equal(linter.verifyAndFix(code, config, 'test.vue').output, output)
})
it('Even if include comments, the output should be as expected.', () => {
const code = `
<script>
export default {
/**data*/
data() {
}/*after data*/,
/**name*/
name: 'burger'/*after name*/
};
</script>`
const output = `
<script>
export default {
/**name*/
name: 'burger',
/**data*/
data() {
},/*after data*//*after name*/
};
</script>`
assert.equal(linter.verifyAndFix(code, config, 'test.vue').output, output)
})
})
// https://github.com/vuejs/eslint-plugin-vue/issues/554
describe('Autofix of `html-self-closing` and `component-name-in-template-casing` should not conflict.', () => {
const kebabConfig = Object.assign({}, baseConfig, {
rules: {
'vue/html-self-closing': [
'error',
{
html: {
component: 'never'
}
}
],
'vue/component-name-in-template-casing': [
'error',
'kebab-case',
{ registeredComponentsOnly: false }
]
}
})
const pascalConfig = Object.assign({}, baseConfig, {
rules: {
'vue/html-self-closing': [
'error',
{
html: {
component: 'never'
}
}
],
'vue/component-name-in-template-casing': [
'error',
'PascalCase',
{ registeredComponentsOnly: false }
]
}
})
it('Even if set kebab-case, the output should be as expected.', () => {
const code = `
<template>
<VueComponent />
</template>`
const output = `
<template>
<vue-component ></vue-component>
</template>`
assert.equal(
linter.verifyAndFix(code, kebabConfig, 'test.vue').output,
output
)
})
it('Even if set PascalCase, the output should be as expected.', () => {
const code = `
<template>
<vue-component />
</template>`
const output = `
<template>
<VueComponent ></VueComponent>
</template>`
assert.equal(
linter.verifyAndFix(code, pascalConfig, 'test.vue').output,
output
)
})
it('Even if element have an attributes, the output should be as expected.', () => {
const code = `
<template>
<vue-component attr
id="item1" />
</template>`
const output = `
<template>
<VueComponent attr
id="item1" ></VueComponent>
</template>`
assert.equal(
linter.verifyAndFix(code, pascalConfig, 'test.vue').output,
output
)
})
})
})
describe('Autofix test cases with Typescript', () => {
const tsLinter = new Linter()
tsLinter.defineParser('vue-eslint-parser', parser)
for (const key of Object.keys(rules)) {
const ruleId = `vue/${key}`
tsLinter.defineRule(ruleId, rules[key])
}
describe('Autofix of `vue/order-in-components` with type assertions in "props".', () => {
const config = Object.assign({}, baseConfig, {
rules: {
'vue/order-in-components': ['error', { order: ['props', 'setup'] }]
},
})
config.parserOptions.parser = { 'ts': require.resolve('@typescript-eslint/parser') }
it('Should fix fields order', () => {
const code = `
<script lang="ts">
export default {
setup () {},
props: {
foo: { type: Array as PropType<number[]> },
}
};
</script>`
const output = `
<script lang="ts">
export default {
props: {
foo: { type: Array as PropType<number[]> },
},
setup () {}
};
</script>`
assert.equal(tsLinter.verifyAndFix(code, config, 'test.vue').output, output)
})
})
})