@@ -27,11 +27,11 @@ test('no config > js > semi', async t => {
27
27
} ) ;
28
28
29
29
test ( 'no config > ts > semi' , async t => {
30
- const filePath = path . join ( t . context . cwd , 'test.ts' ) ;
31
- const { results } = await new Xo ( { cwd : t . context . cwd } ) . lintText (
32
- dedent `console.log('hello')\n` ,
33
- { filePath} ,
34
- ) ;
30
+ const { cwd } = t . context ;
31
+ const filePath = path . join ( cwd , 'test.ts' ) ;
32
+ const text = dedent `console.log('hello')\n` ;
33
+ await fs . writeFile ( filePath , text , 'utf8' ) ;
34
+ const { results } = await new Xo ( { cwd } ) . lintText ( text , { filePath } ) ;
35
35
36
36
t . is ( results ?. [ 0 ] ?. messages ?. length , 1 ) ;
37
37
t . is ( results ?. [ 0 ] ?. messages ?. [ 0 ] ?. ruleId , '@stylistic/semi' ) ;
@@ -59,9 +59,10 @@ test('flat config > js > semi', async t => {
59
59
} ) ;
60
60
61
61
test ( 'flat config > ts > semi' , async t => {
62
- const filePath = path . join ( t . context . cwd , 'test.ts' ) ;
62
+ const { cwd} = t . context ;
63
+ const filePath = path . join ( cwd , 'test.ts' ) ;
63
64
await fs . writeFile (
64
- path . join ( t . context . cwd , 'xo.config.js' ) ,
65
+ path . join ( cwd , 'xo.config.js' ) ,
65
66
dedent `
66
67
export default [
67
68
{
@@ -71,10 +72,10 @@ test('flat config > ts > semi', async t => {
71
72
` ,
72
73
'utf8' ,
73
74
) ;
74
- const xo = new Xo ( { cwd : t . context . cwd } ) ;
75
- const { results } = await xo . lintText ( dedent `console.log('hello ');\n` , {
76
- filePath ,
77
- } ) ;
75
+ const text = dedent `console.log('hello');\n` ;
76
+ await fs . writeFile ( filePath , text , 'utf8 ') ;
77
+ const xo = new Xo ( { cwd } ) ;
78
+ const { results } = await xo . lintText ( text , { filePath } ) ;
78
79
t . is ( results ?. [ 0 ] ?. messages ?. length , 1 ) ;
79
80
t . is ( results ?. [ 0 ] ?. messages ?. [ 0 ] ?. ruleId , '@stylistic/semi' ) ;
80
81
} ) ;
@@ -139,10 +140,11 @@ test('flat config > js > space', async t => {
139
140
} ) ;
140
141
141
142
test ( 'flat config > ts > space' , async t => {
142
- const filePath = path . join ( t . context . cwd , 'test.ts' ) ;
143
+ const { cwd} = t . context ;
144
+ const filePath = path . join ( cwd , 'test.ts' ) ;
143
145
144
146
await fs . writeFile (
145
- path . join ( t . context . cwd , 'xo.config.js' ) ,
147
+ path . join ( cwd , 'xo.config.js' ) ,
146
148
dedent `
147
149
export default [
148
150
{
@@ -153,17 +155,15 @@ test('flat config > ts > space', async t => {
153
155
'utf8' ,
154
156
) ;
155
157
156
- const xo = new Xo ( { cwd : t . context . cwd } ) ;
157
- const { results} = await xo . lintText (
158
- dedent `
159
- export function foo() {
160
- console.log('hello');
161
- }\n
162
- ` ,
163
- {
164
- filePath,
165
- } ,
166
- ) ;
158
+ const text = dedent `
159
+ export function foo() {
160
+ console.log('hello');
161
+ }\n
162
+ ` ;
163
+ await fs . writeFile ( filePath , text , 'utf8' ) ;
164
+
165
+ const xo = new Xo ( { cwd} ) ;
166
+ const { results} = await xo . lintText ( text , { filePath} ) ;
167
167
t . is ( results ?. [ 0 ] ?. messages . length , 1 ) ;
168
168
t . is ( results ?. [ 0 ] ?. messages ?. [ 0 ] ?. messageId , 'wrongIndentation' ) ;
169
169
t . is ( results ?. [ 0 ] ?. messages ?. [ 0 ] ?. ruleId , '@stylistic/indent' ) ;
@@ -188,18 +188,17 @@ test('plugin > js > no-use-extend-native', async t => {
188
188
) ;
189
189
} ) ;
190
190
191
- test ( 'pliugin > ts > no-use-extend-native' , async t => {
191
+ test ( 'plugin > ts > no-use-extend-native' , async t => {
192
192
const { cwd} = t . context ;
193
- const tsFilePath = path . join ( t . context . cwd , 'test.ts' ) ;
194
- const { results} = await new Xo ( { cwd} ) . lintText (
195
- dedent `
196
- import {util} from 'node:util';
193
+ const filePath = path . join ( cwd , 'test.ts' ) ;
194
+ const text = dedent `
195
+ import {util} from 'node:util';
197
196
198
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
199
- util.isBoolean('50bda47b09923e045759db8e8dd01a0bacd97370'.shortHash() === '50bdcs47');\n
200
- ` ,
201
- { filePath : tsFilePath } ,
202
- ) ;
197
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
198
+ util.isBoolean('50bda47b09923e045759db8e8dd01a0bacd97370'.shortHash() === '50bdcs47');\n
199
+ ` ;
200
+ await fs . writeFile ( filePath , text , 'utf8' ) ;
201
+ const { results } = await new Xo ( { cwd } ) . lintText ( text , { filePath } ) ;
203
202
t . true ( results [ 0 ] ?. messages ?. length === 1 ) ;
204
203
t . truthy ( results [ 0 ] ?. messages ?. [ 0 ] ) ;
205
204
t . is (
@@ -229,15 +228,14 @@ test('plugin > js > eslint-plugin-import import-x/order', async t => {
229
228
test ( 'plugin > ts > eslint-plugin-import import-x/order' , async t => {
230
229
const { cwd} = t . context ;
231
230
const filePath = path . join ( cwd , 'test.ts' ) ;
232
- const { results} = await new Xo ( { cwd} ) . lintText (
233
- dedent `
234
- import foo from 'foo';
235
- import util from 'node:util';
231
+ const text = dedent `
232
+ import foo from 'foo';
233
+ import util from 'node:util';
236
234
237
- util.inspect(foo);\n
238
- ` ,
239
- { filePath} ,
240
- ) ;
235
+ util.inspect(foo);\n
236
+ ` ;
237
+ await fs . writeFile ( filePath , text , 'utf8' ) ;
238
+ const { results } = await new Xo ( { cwd } ) . lintText ( text , { filePath } ) ;
241
239
t . true ( results [ 0 ] ?. messages ?. length === 1 ) ;
242
240
t . truthy ( results [ 0 ] ?. messages ?. [ 0 ] ) ;
243
241
t . is ( results [ 0 ] ?. messages ?. [ 0 ] ?. ruleId , 'import-x/order' ) ;
@@ -262,14 +260,13 @@ test('plugin > js > eslint-plugin-import import-x/extensions', async t => {
262
260
test ( 'plugin > ts > eslint-plugin-import import-x/extensions' , async t => {
263
261
const { cwd} = t . context ;
264
262
const filePath = path . join ( cwd , 'test.ts' ) ;
265
- const { results} = await new Xo ( { cwd} ) . lintText (
266
- dedent `
267
- import foo from './foo';
263
+ const text = dedent `
264
+ import foo from './foo';
268
265
269
- console.log(foo);\n
270
- ` ,
271
- { filePath} ,
272
- ) ;
266
+ console.log(foo);\n
267
+ ` ;
268
+ await fs . writeFile ( filePath , text , 'utf8' ) ;
269
+ const { results } = await new Xo ( { cwd } ) . lintText ( text , { filePath } ) ;
273
270
t . true ( results [ 0 ] ?. messages ?. length === 1 ) ;
274
271
t . truthy ( results [ 0 ] ?. messages ?. [ 0 ] ) ;
275
272
t . is ( results [ 0 ] ?. messages ?. [ 0 ] ?. ruleId , 'import-x/extensions' ) ;
@@ -278,14 +275,13 @@ test('plugin > ts > eslint-plugin-import import-x/extensions', async t => {
278
275
test ( 'plugin > ts > eslint-plugin-import import-x/no-absolute-path' , async t => {
279
276
const { cwd} = t . context ;
280
277
const filePath = path . join ( cwd , 'test.ts' ) ;
281
- const { results} = await new Xo ( { cwd} ) . lintText (
282
- dedent `
283
- import foo from '/foo';
278
+ const text = dedent `
279
+ import foo from '/foo';
284
280
285
- console.log(foo);\n
286
- ` ,
287
- { filePath} ,
288
- ) ;
281
+ console.log(foo);\n
282
+ ` ;
283
+ await fs . writeFile ( filePath , text , 'utf8' ) ;
284
+ const { results } = await new Xo ( { cwd } ) . lintText ( text , { filePath } ) ;
289
285
t . true ( results [ 0 ] ?. messages ?. some ( ( { ruleId} ) => ruleId === 'import-x/no-absolute-path' ) ) ;
290
286
} ) ;
291
287
@@ -318,13 +314,12 @@ test('plugin > js > eslint-plugin-n n/prefer-global/process', async t => {
318
314
319
315
test ( 'plugin > ts > eslint-plugin-n n/prefer-global/process' , async t => {
320
316
const { cwd} = t . context ;
321
- const tsFilePath = path . join ( cwd , 'test.ts' ) ;
322
- const { results} = await new Xo ( { cwd} ) . lintText (
323
- dedent `
324
- process.cwd();\n
325
- ` ,
326
- { filePath : tsFilePath } ,
327
- ) ;
317
+ const filePath = path . join ( cwd , 'test.ts' ) ;
318
+ const text = dedent `
319
+ process.cwd();\n
320
+ ` ;
321
+ await fs . writeFile ( filePath , text , 'utf8' ) ;
322
+ const { results} = await new Xo ( { cwd} ) . lintText ( text , { filePath} ) ;
328
323
t . true ( results [ 0 ] ?. messages ?. length === 1 ) ;
329
324
t . truthy ( results [ 0 ] ?. messages ?. [ 0 ] ) ;
330
325
t . is ( results [ 0 ] ?. messages ?. [ 0 ] ?. ruleId , 'n/prefer-global/process' ) ;
@@ -351,17 +346,61 @@ test('plugin > js > eslint-plugin-eslint-comments enable-duplicate-disable', asy
351
346
test ( 'plugin > ts > eslint-plugin-eslint-comments no-duplicate-disable' , async t => {
352
347
const { cwd} = t . context ;
353
348
const tsFilePath = path . join ( cwd , 'test.ts' ) ;
354
- const { results} = await new Xo ( {
355
- cwd,
356
- } ) . lintText (
357
- dedent `
358
- /* eslint-disable no-undef */
359
- export const foo = 10; // eslint-disable-line no-undef
360
- \n
361
- ` ,
362
- { filePath : tsFilePath } ,
363
- ) ;
349
+ const text = dedent `
350
+ /* eslint-disable no-undef */
351
+ export const foo = 10; // eslint-disable-line no-undef
352
+ \n
353
+ ` ;
354
+ await fs . writeFile ( tsFilePath , text , 'utf8' ) ;
355
+ const { results} = await new Xo ( { cwd} ) . lintText ( text , { filePath : tsFilePath } ) ;
364
356
t . true ( results [ 0 ] ?. errorCount === 1 ) ;
365
357
t . true ( results [ 0 ] ?. messages . some ( ( { ruleId} ) =>
366
358
ruleId === '@eslint-community/eslint-comments/no-duplicate-disable' ) ) ;
367
359
} ) ;
360
+
361
+ test ( 'lint-text can be ran multiple times in a row with top level typescript rules' , async t => {
362
+ const { cwd} = t . context ;
363
+
364
+ const filePath = path . join ( cwd , 'test.ts' ) ;
365
+ // Text should violate the @typescript -eslint/naming-convention rule
366
+ const text = dedent `
367
+ const fooBar = 10;
368
+ const FooBar = 10;
369
+ const FOO_BAR = 10;
370
+ const foo_bar = 10;\n
371
+ ` ;
372
+
373
+ // We must write tsfiles to disk for the typescript rules to apply
374
+ await fs . writeFile ( filePath , text , 'utf8' ) ;
375
+
376
+ const { results : resultsNoConfig } = await Xo . lintText ( text , { cwd, filePath} ) ;
377
+ // Ensure that with no config, the text is linted and errors are found
378
+ t . true ( resultsNoConfig [ 0 ] ?. errorCount === 3 ) ;
379
+
380
+ await fs . writeFile (
381
+ path . join ( cwd , 'xo.config.ts' ) ,
382
+ dedent `
383
+ export default [
384
+ {
385
+ rules: {
386
+ '@typescript-eslint/naming-convention': 'off',
387
+ '@typescript-eslint/no-unused-vars': 'off'
388
+ },
389
+ }
390
+ ];\n
391
+ ` ,
392
+ 'utf8' ,
393
+ ) ;
394
+
395
+ // Now with a config that turns off the naming-convention rule, the text should not have any errors
396
+ // and should not have any messages when ran multiple times
397
+ const { results} = await Xo . lintText ( text , { cwd, filePath} ) ;
398
+ t . is ( results [ 0 ] ?. errorCount , 0 ) ;
399
+ t . true ( results [ 0 ] ?. messages ?. length === 0 ) ;
400
+ const { results : results2 } = await Xo . lintText ( text , { cwd, filePath} ) ;
401
+ t . is ( results2 [ 0 ] ?. errorCount , 0 ) ;
402
+ t . true ( results2 [ 0 ] ?. messages ?. length === 0 ) ;
403
+ const { results : results3 } = await Xo . lintText ( text , { cwd, filePath} ) ;
404
+ t . is ( results3 [ 0 ] ?. errorCount , 0 ) ;
405
+ t . true ( results3 [ 0 ] ?. messages ?. length === 0 ) ;
406
+ } ) ;
0 commit comments