Skip to content

Commit e3dbe93

Browse files
committed
cherry-pick: vuejs#2968
1 parent 7cfa8d0 commit e3dbe93

File tree

5 files changed

+81
-78
lines changed

5 files changed

+81
-78
lines changed

tests/lib/configs/eslintrc.js renamed to tests/lib/configs/eslintrc.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict'
2-
3-
const { ESLint } = require('../../eslint-compat')
4-
const plugin = require('../../../lib/index.ts').default
1+
import { ESLint } from '../../eslint-compat'
2+
import plugin from '../../../lib'
53

64
describe('eslintrc configs', () => {
75
for (const name of Object.keys(plugin.configs)) {

tests/lib/configs/flat.js renamed to tests/lib/configs/flat.ts

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
* @author 唯然<[email protected]>
44
*/
55

6-
'use strict'
6+
import { Linter } from 'eslint'
7+
import plugin from '../../../lib'
8+
import { strict as assert } from 'assert'
9+
import { FlatESLint } from '../../eslint-compat'
710

8-
const plugin = require('../../../lib/index.ts').default
9-
const { strict: assert } = require('assert') // node v14 does not support 'assert/strict'
10-
const { FlatESLint } = require('../../eslint-compat')
11-
12-
function mergeConfig(configs) {
13-
let config = { rules: {}, plugins: {} }
11+
function mergeConfig(configs: Linter.FlatConfig[]): Linter.FlatConfig {
12+
let config: Linter.FlatConfig = { rules: {}, plugins: {} }
1413
for (const item of configs) {
1514
config = {
1615
...config,
@@ -37,16 +36,16 @@ describe('flat configs', () => {
3736
const forVue = mergeConfig(
3837
base.filter((config) => config.files?.includes('*.vue') || !config.files)
3938
)
40-
assert.strictEqual(forVue.plugins.vue, plugin)
39+
assert.strictEqual(forVue.plugins!.vue, plugin)
4140
assert.strictEqual(forVue.processor, 'vue/vue')
42-
assert.strictEqual(forVue.rules['vue/comment-directive'], 'error')
41+
assert.strictEqual(forVue.rules!['vue/comment-directive'], 'error')
4342

4443
const forOtherThanVue = mergeConfig(
4544
base.filter((config) => !config.files?.includes('*.vue'))
4645
)
47-
assert.strictEqual(forOtherThanVue.plugins.vue, plugin)
46+
assert.strictEqual(forOtherThanVue.plugins!.vue, plugin)
4847
assert.strictEqual(
49-
forOtherThanVue.rules['vue/comment-directive'],
48+
forOtherThanVue.rules!['vue/comment-directive'],
5049
undefined
5150
)
5251
})
@@ -61,20 +60,20 @@ describe('flat configs', () => {
6160
(config) => config.files?.includes('*.vue') || !config.files
6261
)
6362
)
64-
assert.strictEqual(forVue.plugins.vue, plugin)
65-
assert.strictEqual(forVue.rules['vue/comment-directive'], 'error')
66-
assert.strictEqual(forVue.rules['vue/multi-word-component-names'], 'error')
63+
assert.strictEqual(forVue.plugins!.vue, plugin)
64+
assert.strictEqual(forVue.rules!['vue/comment-directive'], 'error')
65+
assert.strictEqual(forVue.rules!['vue/multi-word-component-names'], 'error')
6766

6867
const forOtherThanVue = mergeConfig(
6968
essential.filter((config) => !config.files?.includes('*.vue'))
7069
)
71-
assert.strictEqual(forOtherThanVue.plugins.vue, plugin)
70+
assert.strictEqual(forOtherThanVue.plugins!.vue, plugin)
7271
assert.strictEqual(
73-
forOtherThanVue.rules['vue/comment-directive'],
72+
forOtherThanVue.rules!['vue/comment-directive'],
7473
undefined
7574
)
7675
assert.strictEqual(
77-
forOtherThanVue.rules['vue/multi-word-component-names'],
76+
forOtherThanVue.rules!['vue/multi-word-component-names'],
7877
'error'
7978
)
8079
})
@@ -89,20 +88,20 @@ describe('flat configs', () => {
8988
(config) => config.files?.includes('*.vue') || !config.files
9089
)
9190
)
92-
assert.strictEqual(forVue.plugins.vue, plugin)
93-
assert.strictEqual(forVue.rules['vue/comment-directive'], 'error')
94-
assert.strictEqual(forVue.rules['vue/multi-word-component-names'], 'error')
91+
assert.strictEqual(forVue.plugins!.vue, plugin)
92+
assert.strictEqual(forVue.rules!['vue/comment-directive'], 'error')
93+
assert.strictEqual(forVue.rules!['vue/multi-word-component-names'], 'error')
9594

9695
const forOtherThanVue = mergeConfig(
9796
stronglyRecommended.filter((config) => !config.files?.includes('*.vue'))
9897
)
99-
assert.strictEqual(forOtherThanVue.plugins.vue, plugin)
98+
assert.strictEqual(forOtherThanVue.plugins!.vue, plugin)
10099
assert.strictEqual(
101-
forOtherThanVue.rules['vue/comment-directive'],
100+
forOtherThanVue.rules!['vue/comment-directive'],
102101
undefined
103102
)
104103
assert.strictEqual(
105-
forOtherThanVue.rules['vue/multi-word-component-names'],
104+
forOtherThanVue.rules!['vue/multi-word-component-names'],
106105
'error'
107106
)
108107
})
@@ -117,24 +116,24 @@ describe('flat configs', () => {
117116
(config) => config.files?.includes('*.vue') || !config.files
118117
)
119118
)
120-
assert.strictEqual(forVue.plugins.vue, plugin)
121-
assert.strictEqual(forVue.rules['vue/comment-directive'], 'error')
122-
assert.strictEqual(forVue.rules['vue/multi-word-component-names'], 'error')
123-
assert.strictEqual(forVue.rules['vue/attributes-order'], 'warn')
119+
assert.strictEqual(forVue.plugins!.vue, plugin)
120+
assert.strictEqual(forVue.rules!['vue/comment-directive'], 'error')
121+
assert.strictEqual(forVue.rules!['vue/multi-word-component-names'], 'error')
122+
assert.strictEqual(forVue.rules!['vue/attributes-order'], 'warn')
124123

125124
const forOtherThanVue = mergeConfig(
126125
recommended.filter((config) => !config.files?.includes('*.vue'))
127126
)
128-
assert.strictEqual(forOtherThanVue.plugins.vue, plugin)
127+
assert.strictEqual(forOtherThanVue.plugins!.vue, plugin)
129128
assert.strictEqual(
130-
forOtherThanVue.rules['vue/comment-directive'],
129+
forOtherThanVue.rules!['vue/comment-directive'],
131130
undefined
132131
)
133132
assert.strictEqual(
134-
forOtherThanVue.rules['vue/multi-word-component-names'],
133+
forOtherThanVue.rules!['vue/multi-word-component-names'],
135134
'error'
136135
)
137-
assert.strictEqual(forOtherThanVue.rules['vue/attributes-order'], 'warn')
136+
assert.strictEqual(forOtherThanVue.rules!['vue/attributes-order'], 'warn')
138137
})
139138

140139
it('should export vue2-essential config', () => {
@@ -147,20 +146,20 @@ describe('flat configs', () => {
147146
(config) => config.files?.includes('*.vue') || !config.files
148147
)
149148
)
150-
assert.strictEqual(forVue.plugins.vue, plugin)
151-
assert.strictEqual(forVue.rules['vue/comment-directive'], 'error')
152-
assert.strictEqual(forVue.rules['vue/multi-word-component-names'], 'error')
149+
assert.strictEqual(forVue.plugins!.vue, plugin)
150+
assert.strictEqual(forVue.rules!['vue/comment-directive'], 'error')
151+
assert.strictEqual(forVue.rules!['vue/multi-word-component-names'], 'error')
153152

154153
const forOtherThanVue = mergeConfig(
155154
essential.filter((config) => !config.files?.includes('*.vue'))
156155
)
157-
assert.strictEqual(forOtherThanVue.plugins.vue, plugin)
156+
assert.strictEqual(forOtherThanVue.plugins!.vue, plugin)
158157
assert.strictEqual(
159-
forOtherThanVue.rules['vue/comment-directive'],
158+
forOtherThanVue.rules!['vue/comment-directive'],
160159
undefined
161160
)
162161
assert.strictEqual(
163-
forOtherThanVue.rules['vue/multi-word-component-names'],
162+
forOtherThanVue.rules!['vue/multi-word-component-names'],
164163
'error'
165164
)
166165
})
@@ -175,20 +174,20 @@ describe('flat configs', () => {
175174
(config) => config.files?.includes('*.vue') || !config.files
176175
)
177176
)
178-
assert.strictEqual(forVue.plugins.vue, plugin)
179-
assert.strictEqual(forVue.rules['vue/comment-directive'], 'error')
180-
assert.strictEqual(forVue.rules['vue/multi-word-component-names'], 'error')
177+
assert.strictEqual(forVue.plugins!.vue, plugin)
178+
assert.strictEqual(forVue.rules!['vue/comment-directive'], 'error')
179+
assert.strictEqual(forVue.rules!['vue/multi-word-component-names'], 'error')
181180

182181
const forOtherThanVue = mergeConfig(
183182
stronglyRecommended.filter((config) => !config.files?.includes('*.vue'))
184183
)
185-
assert.strictEqual(forOtherThanVue.plugins.vue, plugin)
184+
assert.strictEqual(forOtherThanVue.plugins!.vue, plugin)
186185
assert.strictEqual(
187-
forOtherThanVue.rules['vue/comment-directive'],
186+
forOtherThanVue.rules!['vue/comment-directive'],
188187
undefined
189188
)
190189
assert.strictEqual(
191-
forOtherThanVue.rules['vue/multi-word-component-names'],
190+
forOtherThanVue.rules!['vue/multi-word-component-names'],
192191
'error'
193192
)
194193
})
@@ -203,24 +202,24 @@ describe('flat configs', () => {
203202
(config) => config.files?.includes('*.vue') || !config.files
204203
)
205204
)
206-
assert.strictEqual(forVue.plugins.vue, plugin)
207-
assert.strictEqual(forVue.rules['vue/comment-directive'], 'error')
208-
assert.strictEqual(forVue.rules['vue/multi-word-component-names'], 'error')
209-
assert.strictEqual(forVue.rules['vue/attributes-order'], 'warn')
205+
assert.strictEqual(forVue.plugins!.vue, plugin)
206+
assert.strictEqual(forVue.rules!['vue/comment-directive'], 'error')
207+
assert.strictEqual(forVue.rules!['vue/multi-word-component-names'], 'error')
208+
assert.strictEqual(forVue.rules!['vue/attributes-order'], 'warn')
210209

211210
const forOtherThanVue = mergeConfig(
212211
recommended.filter((config) => !config.files?.includes('*.vue'))
213212
)
214-
assert.strictEqual(forOtherThanVue.plugins.vue, plugin)
213+
assert.strictEqual(forOtherThanVue.plugins!.vue, plugin)
215214
assert.strictEqual(
216-
forOtherThanVue.rules['vue/comment-directive'],
215+
forOtherThanVue.rules!['vue/comment-directive'],
217216
undefined
218217
)
219218
assert.strictEqual(
220-
forOtherThanVue.rules['vue/multi-word-component-names'],
219+
forOtherThanVue.rules!['vue/multi-word-component-names'],
221220
'error'
222221
)
223-
assert.strictEqual(forOtherThanVue.rules['vue/attributes-order'], 'warn')
222+
assert.strictEqual(forOtherThanVue.rules!['vue/attributes-order'], 'warn')
224223
})
225224

226225
it('should work the suppress comments with base config', async () => {
@@ -281,7 +280,7 @@ describe('flat configs', () => {
281280
const result = await eslint.lintText(code, { filePath: 'MyComponent.vue' })
282281

283282
assert.deepStrictEqual(
284-
result[0].messages.map((message) => message.ruleId),
283+
result[0].messages.map((message: Linter.LintMessage) => message.ruleId),
285284
[
286285
'vue/no-parsing-error',
287286
'vue/max-attributes-per-line',

tests/lib/rules/block-order.js renamed to tests/lib/rules/block-order.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
11
/**
22
* @author Yosuke Ota
33
*/
4-
'use strict'
5-
6-
const rule = require('../../../lib/rules/block-order')
7-
const RuleTester = require('../../eslint-compat').RuleTester
8-
const assert = require('assert')
9-
const { ESLint } = require('../../eslint-compat')
4+
import assert from 'assert'
5+
import parserVue from 'vue-eslint-parser'
6+
import rule from '../../../lib/rules/block-order'
7+
import { ESLint, RuleTester } from '../../eslint-compat'
8+
import pluginVue from '../../../lib'
9+
import processor from '../../../lib/processor'
1010

1111
// Initialize linter.
1212
const eslint = new ESLint({
1313
overrideConfigFile: true,
1414
overrideConfig: {
1515
files: ['**/*.vue'],
1616
languageOptions: {
17-
parser: require('vue-eslint-parser'),
17+
parser: parserVue,
1818
ecmaVersion: 2015
1919
},
20-
plugins: { vue: require('../../../lib/index.ts').default },
20+
plugins: { vue: pluginVue },
2121
rules: {
2222
'vue/comment-directive': 'error',
2323
'vue/block-order': 'error'
2424
},
25-
processor: require('../../../lib/processor.ts').default
25+
processor
2626
},
2727
fix: true
2828
})
2929

3030
const tester = new RuleTester({
3131
languageOptions: {
32-
parser: require('vue-eslint-parser')
32+
parser: parserVue
3333
}
3434
})
3535

36+
// @ts-expect-error
3637
tester.run('block-order', rule, {
3738
valid: [
3839
// default

tests/lib/rules/comment-directive.js renamed to tests/lib/rules/comment-directive.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,33 @@
33
* @author Toru Nagashima
44
*/
55

6-
'use strict'
7-
8-
const assert = require('assert')
9-
const { ESLint } = require('../../eslint-compat')
6+
import assert from 'assert'
7+
import parserVue from 'vue-eslint-parser'
8+
import { ESLint } from '../../eslint-compat'
9+
import pluginVue from '../../../lib'
10+
import processor from '../../../lib/processor'
1011

1112
// Initialize linter.
1213
const eslint = new ESLint({
1314
overrideConfigFile: true,
1415
overrideConfig: {
1516
files: ['*.*'],
1617
languageOptions: {
17-
parser: require('vue-eslint-parser'),
18+
parser: parserVue,
1819
ecmaVersion: 2015
1920
},
20-
plugins: { vue: require('../../../lib/index.ts').default },
21+
plugins: { vue: pluginVue },
2122
rules: {
2223
'no-unused-vars': 'error',
2324
'vue/comment-directive': 'error',
2425
'vue/no-parsing-error': 'error',
2526
'vue/no-duplicate-attributes': 'error'
2627
},
27-
processor: require('../../../lib/processor.ts').default
28+
processor
2829
}
2930
})
3031

31-
async function lintMessages(code) {
32+
async function lintMessages(code: string) {
3233
const result = await eslint.lintText(code, { filePath: 'test.vue' })
3334
return result[0].messages
3435
}
@@ -357,10 +358,10 @@ describe('comment-directive', () => {
357358
overrideConfig: {
358359
files: ['**/*.vue'],
359360
languageOptions: {
360-
parser: require('vue-eslint-parser'),
361+
parser: parserVue,
361362
ecmaVersion: 2015
362363
},
363-
plugins: { vue: require('../../../lib/index.ts').default },
364+
plugins: { vue: pluginVue },
364365
rules: {
365366
'no-unused-vars': 'error',
366367
'vue/comment-directive': [
@@ -370,11 +371,11 @@ describe('comment-directive', () => {
370371
'vue/no-parsing-error': 'error',
371372
'vue/no-duplicate-attributes': 'error'
372373
},
373-
processor: require('../../../lib/processor.ts').default
374+
processor
374375
}
375376
})
376377

377-
async function lintMessages(code) {
378+
async function lintMessages(code: string) {
378379
const result = await eslint.lintText(code, { filePath: 'test.vue' })
379380
return result[0].messages
380381
}

tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"module": "preserve",
55
"moduleResolution": "bundler",
66
"lib": ["es2020"],
7+
"types": [
8+
"vitest/globals"
9+
],
710
"allowJs": true,
811
"checkJs": true,
912
"noEmit": true,
@@ -28,6 +31,7 @@
2831
},
2932
"include": [
3033
"lib/**/*",
34+
"tests/**/*.ts",
3135
"typings/eslint-plugin-vue/global.d.ts",
3236
"docs/.vitepress/**/*.ts",
3337
"docs/.vitepress/**/*.mts"

0 commit comments

Comments
 (0)