Skip to content

Commit a253473

Browse files
authored
test: add typescript tests (#152)
* test(ts): add tests for babel+ts and ts-eslint * test: add basic typescript tests * docs(readme): add section on usage with typescript
1 parent 9e35f1e commit a253473

30 files changed

+2778
-554
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@ Example of `.eslintrc`:
3636
}
3737
```
3838

39+
### Usage with TypeScript
40+
41+
To enable working with TypeScript projects, install `@babel/preset-typescript` as a dependency add `"typescript"` to `parserOptions.babelOptions.parserOpts.plugins` in your `.eslintrc`.
42+
43+
Example:
44+
45+
```json
46+
{
47+
"parserOptions": {
48+
"babelOptions": {
49+
"parserOpts": {
50+
"plugins": [
51+
"classProperties",
52+
["decorators", { "decoratorsBeforExport": false }],
53+
"typescript"
54+
]
55+
}
56+
}
57+
}
58+
}
59+
```
60+
3961
For more details about configuration please refer to the dedicated section in the ESLint documentation: https://eslint.org/docs/user-guide/configuring
4062

4163
## Configurations

lib/rules/no-restricted-browser-globals-during-ssr.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = {
1919
meta: {
2020
type: 'problem',
2121
docs: {
22-
url: docUrl('no-browser-globals-during-ssr'),
22+
url: docUrl('no-restricted-browser-globals-during-ssr'),
2323
category: 'LWC',
2424
description: 'disallow access to global browser APIs during SSR',
2525
},

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"devDependencies": {
2323
"@babel/core": "^7.24.4",
2424
"@babel/eslint-parser": "^7.24.1",
25+
"@babel/preset-typescript": "^7.24.6",
2526
"eslint": "^8.57.0",
2627
"husky": "^9.0.11",
2728
"lint-staged": "^15.2.2",

test/lib/rules/consistent-component-name.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,9 @@
66
*/
77
'use strict';
88

9-
const { RuleTester } = require('eslint');
9+
const { testRule } = require('../shared');
1010

11-
const { ESLINT_TEST_CONFIG } = require('../shared');
12-
const rule = require('../../../lib/rules/consistent-component-name');
13-
14-
const ruleTester = new RuleTester(ESLINT_TEST_CONFIG);
15-
16-
ruleTester.run('consistent-component-name', rule, {
11+
testRule('consistent-component-name', {
1712
valid: [
1813
{
1914
code: `

test/lib/rules/no-api-reassignments.js

Lines changed: 182 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,9 @@
66
*/
77
'use strict';
88

9-
const { RuleTester } = require('eslint');
9+
const { testRule, testTypeScript } = require('../shared');
1010

11-
const { ESLINT_TEST_CONFIG } = require('../shared');
12-
const rule = require('../../../lib/rules/no-api-reassignments');
13-
14-
const ruleTester = new RuleTester(ESLINT_TEST_CONFIG);
15-
16-
ruleTester.run('no-api-reassignments', rule, {
11+
testRule('no-api-reassignments', {
1712
valid: [
1813
{
1914
code: `this.foo = 1;`,
@@ -192,3 +187,183 @@ ruleTester.run('no-api-reassignments', rule, {
192187
},
193188
],
194189
});
190+
191+
testTypeScript('no-api-reassignments', {
192+
valid: [
193+
{
194+
code: `this.foo = 1;`,
195+
},
196+
{
197+
code: `class Test {
198+
@api foo: string;
199+
}`,
200+
},
201+
{
202+
code: `class Test {
203+
@api foo: number = 1;
204+
}`,
205+
},
206+
{
207+
code: `class Test {
208+
@api foo: number;
209+
210+
method(obj: any) {
211+
obj.foo = 1;
212+
}
213+
}`,
214+
},
215+
{
216+
code: `class Test {
217+
@api foo: number;
218+
219+
method() {
220+
function test() {
221+
this.foo = 1;
222+
}
223+
}
224+
}`,
225+
},
226+
{
227+
code: `class Test {
228+
@api public foo: number;
229+
230+
method() {
231+
const test = function() {
232+
this.foo = 1;
233+
}
234+
}
235+
}`,
236+
},
237+
{
238+
code: `class Test {
239+
@api public foo: number;
240+
241+
method() {
242+
const obj = {
243+
inner() {
244+
this.foo = 1;
245+
}
246+
}
247+
}
248+
}`,
249+
},
250+
{
251+
code: `class Test {
252+
@api
253+
public foo(): void {
254+
this.foo = 1;
255+
}
256+
}`,
257+
},
258+
],
259+
invalid: [
260+
{
261+
code: `class Test {
262+
@api foo: number;
263+
264+
method() {
265+
this.foo = 1;
266+
}
267+
}`,
268+
errors: [
269+
{
270+
message: 'Invalid reassignment of public property "foo"',
271+
},
272+
],
273+
},
274+
{
275+
code: `class Test {
276+
@api
277+
set foo(v: number) {}
278+
279+
method() {
280+
this.foo = 1;
281+
}
282+
}`,
283+
errors: [
284+
{
285+
message: 'Invalid reassignment of public property "foo"',
286+
},
287+
],
288+
},
289+
{
290+
code: `class Test {
291+
@api
292+
get foo(): number {}
293+
294+
method() {
295+
this.foo = 1;
296+
}
297+
}`,
298+
errors: [
299+
{
300+
message: 'Invalid reassignment of public property "foo"',
301+
},
302+
],
303+
},
304+
{
305+
code: `class Test {
306+
@api public foo: number;
307+
308+
method() {
309+
const obj = {
310+
inner: () => {
311+
this.foo = 1;
312+
}
313+
}
314+
}
315+
}`,
316+
errors: [
317+
{
318+
message: 'Invalid reassignment of public property "foo"',
319+
},
320+
],
321+
},
322+
{
323+
code: `class Test {
324+
@api foo: number;
325+
326+
method() {
327+
{
328+
this.foo = 1;
329+
}
330+
}
331+
}`,
332+
errors: [
333+
{
334+
message: 'Invalid reassignment of public property "foo"',
335+
},
336+
],
337+
},
338+
{
339+
code: `class Test {
340+
@api foo: number;
341+
342+
method() {
343+
const test = () => {
344+
this.foo = 1;
345+
}
346+
}
347+
}`,
348+
errors: [
349+
{
350+
message: 'Invalid reassignment of public property "foo"',
351+
},
352+
],
353+
},
354+
{
355+
code: `class Test {
356+
@api foo: number;
357+
358+
method = () => {
359+
this.foo = 1;
360+
}
361+
}`,
362+
errors: [
363+
{
364+
message: 'Invalid reassignment of public property "foo"',
365+
},
366+
],
367+
},
368+
],
369+
});

test/lib/rules/no-async-await.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,9 @@
66
*/
77
'use strict';
88

9-
const { RuleTester } = require('eslint');
9+
const { testRule } = require('../shared');
1010

11-
const { ESLINT_TEST_CONFIG } = require('../shared');
12-
const rule = require('../../../lib/rules/no-async-await');
13-
14-
const ruleTester = new RuleTester(ESLINT_TEST_CONFIG);
15-
16-
ruleTester.run('no-async-await', rule, {
11+
testRule('no-async-await', {
1712
valid: [
1813
{
1914
code: `function foo () {}`,

test/lib/rules/no-async-operation.js

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66
*/
77
'use strict';
88

9-
const { RuleTester } = require('eslint');
9+
const { testRule, testTypeScript } = require('../shared');
1010

11-
const { ESLINT_TEST_CONFIG } = require('../shared');
12-
const rule = require('../../../lib/rules/no-async-operation');
11+
// TODO: Type assertions break this rule
1312

14-
const ruleTester = new RuleTester(ESLINT_TEST_CONFIG);
15-
16-
ruleTester.run('no-async-operations', rule, {
13+
testRule('no-async-operation', {
1714
valid: [
1815
{
1916
code: 'obj.setTimeout();',
@@ -87,3 +84,78 @@ ruleTester.run('no-async-operations', rule, {
8784
},
8885
],
8986
});
87+
88+
testTypeScript('no-async-operation', {
89+
valid: [
90+
{
91+
code: 'obj.setTimeout();',
92+
},
93+
{
94+
code: `
95+
const obj: object = {};
96+
obj.setTimeout();
97+
`,
98+
},
99+
{
100+
code: `
101+
const setTimeout: Function = () => {};
102+
setTimeout();
103+
`,
104+
},
105+
{
106+
code: `
107+
const setTimeout: Function = () => {};
108+
109+
function foo() {
110+
setTimeout();
111+
}
112+
`,
113+
},
114+
],
115+
invalid: [
116+
{
117+
code: `
118+
setTimeout() satisfies number;
119+
setInterval() satisfies number;
120+
requestAnimationFrame() satisfies number;
121+
`,
122+
errors: [
123+
{ message: 'Restricted async operation "setTimeout"' },
124+
{ message: 'Restricted async operation "setInterval"' },
125+
{ message: 'Restricted async operation "requestAnimationFrame"' },
126+
],
127+
},
128+
// {
129+
// code: `
130+
// (window as any).setTimeout();
131+
// (window as any).setInterval();
132+
// (window as any).requestAnimationFrame();
133+
// `,
134+
// errors: [
135+
// { message: 'Restricted async operation "setTimeout"' },
136+
// { message: 'Restricted async operation "setInterval"' },
137+
// { message: 'Restricted async operation "requestAnimationFrame"' },
138+
// ],
139+
// },
140+
// {
141+
// code: `
142+
// (window as any)["setTimeout"]();
143+
// (window as any)["setInterval"]();
144+
// (window as any)["requestAnimationFrame"]();
145+
// `,
146+
// errors: [
147+
// { message: 'Restricted async operation "setTimeout"' },
148+
// { message: 'Restricted async operation "setInterval"' },
149+
// { message: 'Restricted async operation "requestAnimationFrame"' },
150+
// ],
151+
// },
152+
{
153+
code: `
154+
function foo(): void {
155+
setTimeout();
156+
}
157+
`,
158+
errors: [{ message: 'Restricted async operation "setTimeout"' }],
159+
},
160+
],
161+
});

0 commit comments

Comments
 (0)