Skip to content

Commit 16d17a4

Browse files
authored
test: rewrite some files in .ts (#2968)
1 parent b609c2b commit 16d17a4

File tree

6 files changed

+83
-96
lines changed

6 files changed

+83
-96
lines changed

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ export default typegen([
275275
'@typescript-eslint/no-explicit-any': 'off',
276276
'@typescript-eslint/no-empty-object-type': 'off',
277277
'@typescript-eslint/no-namespace': 'off',
278+
'@typescript-eslint/no-non-null-assertion': 'off',
278279
'@typescript-eslint/triple-slash-reference': 'off',
279280
'@typescript-eslint/unified-signatures': 'off',
280281
'@typescript-eslint/ban-ts-comment': [

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')
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 唯然<weiran.zsd@outlook.com>
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')
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: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
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 { Rule } from '../../../node_modules/@types/eslint'
5+
import assert from 'assert'
6+
import parserVue from 'vue-eslint-parser'
7+
import rule from '../../../lib/rules/block-order'
8+
import { ESLint, RuleTester } 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: ['**/*.vue'],
1617
languageOptions: {
17-
parser: require('vue-eslint-parser'),
18+
parser: parserVue,
1819
ecmaVersion: 2015
1920
},
20-
plugins: { vue: require('../../../lib/index') },
21+
plugins: { vue: pluginVue },
2122
rules: {
2223
'vue/comment-directive': 'error',
2324
'vue/block-order': 'error'
2425
},
25-
processor: require('../../../lib/processor')
26+
processor
2627
},
2728
fix: true
2829
})
2930

3031
const tester = new RuleTester({
3132
languageOptions: {
32-
parser: require('vue-eslint-parser')
33+
parser: parserVue
3334
}
3435
})
3536

36-
tester.run('block-order', rule, {
37+
tester.run('block-order', rule as unknown as Rule.RuleModule, {
3738
valid: [
3839
// default
3940
'<script></script><template></template><style></style>',
@@ -71,44 +72,36 @@ tester.run('block-order', rule, {
7172
// order
7273
{
7374
code: '<script></script><template></template><style></style>',
74-
output: null,
7575
options: [{ order: ['script', 'template', 'style'] }]
7676
},
7777
{
7878
code: '<template></template><script></script><style></style>',
79-
output: null,
8079
options: [{ order: ['template', 'script', 'style'] }]
8180
},
8281
{
8382
code: '<style></style><template></template><script></script>',
84-
output: null,
8583
options: [{ order: ['style', 'template', 'script'] }]
8684
},
8785
{
8886
code: '<template></template><script></script><style></style>',
89-
output: null,
9087
options: [{ order: ['template', 'docs', 'script', 'style'] }]
9188
},
9289
{
9390
code: '<template></template><docs></docs><script></script><style></style>',
94-
output: null,
9591
options: [{ order: ['template', 'script', 'style'] }]
9692
},
9793
{
9894
code: '<docs><div id="id">text <!--comment--> </div><br></docs><script></script><template></template><style></style>',
99-
output: null,
10095
options: [{ order: ['docs', 'script', 'template', 'style'] }]
10196
},
10297
{
10398
code: '<script setup></script><script></script><template></template><style></style>',
104-
output: null,
10599
options: [
106100
{ order: ['script[setup]', 'script:not([setup])', 'template', 'style'] }
107101
]
108102
},
109103
{
110104
code: '<template></template><script setup></script><script></script><style></style>',
111-
output: null,
112105
options: [
113106
{
114107
order: [['script[setup]', 'script:not([setup])', 'template'], 'style']
@@ -117,24 +110,20 @@ tester.run('block-order', rule, {
117110
},
118111
{
119112
code: '<script></script><script setup></script><template></template><style></style>',
120-
output: null,
121113
options: [{ order: ['script', 'template', 'style'] }]
122114
},
123115
{
124116
code: '<template></template><script></script><script setup></script><style></style>',
125-
output: null,
126117
options: [{ order: [['script', 'template'], 'style'] }]
127118
},
128119
{
129120
code: '<script></script><script setup></script><template></template><style></style>',
130-
output: null,
131121
options: [
132122
{ order: ['script:not([setup])', 'script[setup]', 'template', 'style'] }
133123
]
134124
},
135125
{
136126
code: '<template></template><script></script><script setup></script><style></style>',
137-
output: null,
138127
options: [
139128
{
140129
order: [['script:not([setup])', 'script[setup]', 'template'], 'style']
@@ -143,7 +132,6 @@ tester.run('block-order', rule, {
143132
},
144133
{
145134
code: '<template></template><script></script><script setup></script><style scoped></style><style></style><i18n locale="ja"></i18n><i18n locale="en"></i18n>',
146-
output: null,
147135
options: [
148136
{
149137
order: [
@@ -158,7 +146,6 @@ tester.run('block-order', rule, {
158146
},
159147
{
160148
code: '<template></template><script></script><script setup></script><style scoped></style><style></style><i18n locale="en"></i18n><i18n locale="ja"></i18n>',
161-
output: null,
162149
options: [
163150
{
164151
order: [
@@ -175,17 +162,14 @@ tester.run('block-order', rule, {
175162
},
176163
{
177164
code: '<template></template><docs></docs><script></script><style></style>',
178-
output: null,
179165
options: [{ order: [['docs', 'script', 'template'], 'style'] }]
180166
},
181167
{
182168
code: '<i18n locale="en"></i18n><i18n locale="ja"></i18n>',
183-
output: null,
184169
options: [{ order: ['i18n[locale=en]', 'i18n[locale=ja]'] }]
185170
},
186171
{
187172
code: '<style></style><style scoped></style>',
188-
output: null,
189173
options: [{ order: ['style:not([scoped])', 'style[scoped]'] }]
190174
},
191175

0 commit comments

Comments
 (0)