Skip to content

Commit 8f1e32b

Browse files
OrKoNDevtools-frontend LUCI CQ
authored andcommitted
Migrate lint rules to TS
Bug: 397586315 Change-Id: I62df80eb3bd891e3f86998b82af71af6066543c3 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6431670 Commit-Queue: Danil Somsikov <[email protected]> Auto-Submit: Alex Rudenko <[email protected]> Reviewed-by: Danil Somsikov <[email protected]> Reviewed-by: Nikolay Vitkov <[email protected]>
1 parent e654762 commit 8f1e32b

File tree

7 files changed

+56
-60
lines changed

7 files changed

+56
-60
lines changed
Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
// Copyright 2021 The Chromium Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
'use strict';
54

65
/**
76
* @fileoverview Prevent usage of customElements.define() and use the helper
87
* function instead
98
*/
109

11-
// ------------------------------------------------------------------------------
12-
// Rule Definition
13-
// ------------------------------------------------------------------------------
10+
import * as fs from 'fs';
11+
import * as path from 'path';
1412

15-
const fs = require('fs');
16-
const path = require('path');
13+
import {createRule} from './tsUtils.ts';
1714

18-
/**
19-
* @type {import('eslint').Rule.RuleModule}
20-
*/
21-
module.exports = {
15+
export default createRule({
16+
name: 'check-css-import',
2217
meta: {
2318
type: 'problem',
24-
2519
docs: {
2620
description: 'check CSS file imports',
2721
category: 'Possible Errors',
2822
},
2923
fixable: 'code',
30-
schema: [] // no options
24+
messages: {
25+
fileDoesNotExist: 'File {{filename}} does not exist. Check you are importing the correct file.',
26+
},
27+
schema: [], // no options
3128
},
29+
defaultOptions: [],
3230
create: function(context) {
33-
const filename = context.filename ?? context.getFilename();
31+
const filename = context.getFilename();
3432
return {
3533
ImportDeclaration(node) {
3634
const importPath = path.normalize(`${node.source.value}`);
@@ -43,11 +41,12 @@ module.exports = {
4341
if (!fs.existsSync(importedCSS)) {
4442
context.report({
4543
node,
46-
message: `File ${path.basename(importedCSS)} does not exist. Check you are importing the correct file.`
44+
messageId: 'fileDoesNotExist',
45+
data: {filename: path.basename(importedCSS)},
4746
});
4847
}
4948
}
50-
}
49+
},
5150
};
52-
}
53-
};
51+
},
52+
});
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
// Copyright 2021 The Chromium Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
'use strict';
54

6-
const {isLitHtmlTemplateCall} = require('./utils.js');
5+
import {createRule} from './tsUtils.ts';
6+
import {isLitHtmlTemplateCall} from './utils.js';
77

8-
/**
9-
* @type {import('eslint').Rule.RuleModule}
10-
*/
11-
module.exports = {
8+
export default createRule({
9+
name: 'no-a-tags-in-lit',
1210
meta: {
1311
type: 'problem',
1412
docs: {
1513
description: 'Check for <a> and </a> in Lit templates instead of using x-link.',
1614
category: 'Possible Errors',
1715
},
18-
schema: [] // no options
16+
messages: {
17+
foundAnchor: 'Found an anchor element in a lit template. Use XLink.ts instead.',
18+
},
19+
fixable: 'code',
20+
schema: [], // no options
1921
},
22+
defaultOptions: [],
2023
create: function(context) {
2124
return {
2225
TaggedTemplateExpression(node) {
@@ -30,11 +33,11 @@ module.exports = {
3033
if (templatePart.value.raw.match(/<a[\s>]/) || templatePart.value.raw.includes('</a>')) {
3134
context.report({
3235
node,
33-
message: 'Adding links to a component should be done using `front_end/ui/legacy/XLink.ts`',
36+
messageId: 'foundAnchor',
3437
});
3538
}
3639
}
3740
},
3841
};
3942
}
40-
};
43+
});

scripts/eslint_rules/lib/no-assert-deep-strict-equal.js renamed to scripts/eslint_rules/lib/no-assert-deep-strict-equal.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Copyright 2024 The Chromium Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
'use strict';
4+
5+
import {createRule} from './tsUtils.ts';
56

67
/**
78
* @fileoverview Disallow usage of `assert.deepStrictEqual`.
@@ -16,11 +17,10 @@
1617
// Rule Definition
1718
// ------------------------------------------------------------------------------
1819

19-
/** @type {import('eslint').Rule.RuleModule} */
20-
module.exports = {
20+
export default createRule({
21+
name: 'no-assert-deep-strict-equal',
2122
meta: {
2223
type: 'suggestion',
23-
2424
docs: {
2525
description: 'Disallow usage of `assert.deepStrictEqual` in favor of `assert.deepEqual`.',
2626
category: 'Best Practices',
@@ -29,15 +29,14 @@ module.exports = {
2929
unexpectedAssertDeepStrictEqual: 'Unexpected assert.deepStrictEqual. Use assert.deepEqual instead.',
3030
},
3131
fixable: 'code',
32-
schema: [], // no options
32+
schema: [], // no options
3333
},
34-
create: function (context) {
35-
function isAssertDeepStrictEqual(calleeNode) {
36-
return calleeNode.type === 'MemberExpression' &&
37-
calleeNode.object.type === 'Identifier' &&
38-
calleeNode.object.name === 'assert' &&
39-
calleeNode.property.type === 'Identifier' &&
40-
calleeNode.property.name === 'deepStrictEqual';
34+
defaultOptions: [],
35+
create(context) {
36+
function isAssertDeepStrictEqual(calleeNode): boolean {
37+
return calleeNode.type === 'MemberExpression' && calleeNode.object.type === 'Identifier' &&
38+
calleeNode.object.name === 'assert' && calleeNode.property.type === 'Identifier' &&
39+
calleeNode.property.name === 'deepStrictEqual';
4140
}
4241

4342
function reportError(node) {
@@ -58,4 +57,4 @@ module.exports = {
5857
}
5958
};
6059
},
61-
};
60+
});

scripts/eslint_rules/tests/check-css-import.test.js renamed to scripts/eslint_rules/tests/check-css-import.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// Copyright 2021 The Chromium Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
'use strict';
54

6-
const rule = require('../lib/check-css-import.js');
5+
import rule from '../lib/check-css-import.ts';
76

8-
const {RuleTester} = require('./utils/utils.js');
7+
import {RuleTester} from './utils/tsUtils.ts';
98

109
new RuleTester().run('check-css-import', rule, {
1110
valid: [
@@ -26,7 +25,7 @@ new RuleTester().run('check-css-import', rule, {
2625
filename: 'front_end/ui/components/component/file.ts',
2726
errors: [
2827
{
29-
message: 'File styles.css does not exist. Check you are importing the correct file.',
28+
messageId: 'fileDoesNotExist',
3029
},
3130
],
3231
},
@@ -36,7 +35,7 @@ new RuleTester().run('check-css-import', rule, {
3635
filename: 'front_end/ui/components/icon_button/file.ts',
3736
errors: [
3837
{
39-
message: 'File check_css_import_tests_file.css does not exist. Check you are importing the correct file.',
38+
messageId: 'fileDoesNotExist',
4039
},
4140
],
4241
},

scripts/eslint_rules/tests/no-a-tags-in-lit.test.js renamed to scripts/eslint_rules/tests/no-a-tags-in-lit.test.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
// Copyright 2021 The Chromium Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
'use strict';
5-
const rule = require('../lib/no-a-tags-in-lit.js');
64

7-
const {RuleTester} = require('./utils/utils.js');
5+
import rule from '../lib/no-a-tags-in-lit.ts';
86

9-
const EXPECTED_ERROR_MESSAGE = 'Adding links to a component should be done using `front_end/ui/legacy/XLink.ts`';
7+
import {RuleTester} from './utils/tsUtils.ts';
108

119
new RuleTester().run('no-a-tags-in-lit', rule, {
1210
valid: [
@@ -40,30 +38,30 @@ new RuleTester().run('no-a-tags-in-lit', rule, {
4038
{
4139
code: 'Lit.html`<a />`',
4240
filename: 'front_end/components/test.ts',
43-
errors: [{message: EXPECTED_ERROR_MESSAGE}],
41+
errors: [{messageId: 'foundAnchor'}],
4442
},
4543
{
4644
code: 'Lit.html`<a></a>`',
4745
filename: 'front_end/components/test.ts',
48-
errors: [{message: EXPECTED_ERROR_MESSAGE}],
46+
errors: [{messageId: 'foundAnchor'}],
4947
},
5048
{
5149
code: 'Lit.html`</a>`',
5250
filename: 'front_end/components/test.ts',
53-
errors: [{message: EXPECTED_ERROR_MESSAGE}],
51+
errors: [{messageId: 'foundAnchor'}],
5452
},
5553
{
5654
code: 'Lit.html`<a >`',
5755
filename: 'front_end/components/test.ts',
58-
errors: [{message: EXPECTED_ERROR_MESSAGE}],
56+
errors: [{messageId: 'foundAnchor'}],
5957
},
6058
{
6159
code: 'Lit.html`<p><${DataGrid.litTagName}></${DataGrid.litTagName}><a></a></p>`',
62-
errors: [{message: EXPECTED_ERROR_MESSAGE}],
60+
errors: [{messageId: 'foundAnchor'}],
6361
},
6462
{
6563
code: 'Lit.html`<${DataGrid.litTagName}><a /></${DataGrid.litTagName}>`',
66-
errors: [{message: EXPECTED_ERROR_MESSAGE}],
64+
errors: [{messageId: 'foundAnchor'}],
6765
},
6866
],
6967
});

scripts/eslint_rules/tests/no-assert-deep-strict-equal.test.js renamed to scripts/eslint_rules/tests/no-assert-deep-strict-equal.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// Copyright 2024 The Chromium Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
'use strict';
54

6-
const rule = require('../lib/no-assert-deep-strict-equal.js');
5+
import rule from '../lib/no-assert-deep-strict-equal.ts';
76

8-
const {RuleTester} = require('./utils/utils.js');
7+
import {RuleTester} from './utils/tsUtils.ts';
98

109
new RuleTester().run('no-assert-deep-strict-equal', rule, {
1110
valid: [

test/run.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,8 @@ class ScriptsMochaTests extends Tests {
184184
return super.run(
185185
tests.map(test => ScriptPathPair.getFromPair(test)),
186186
[
187-
'--experimental-strip-types',
188-
'--no-warnings=ExperimentalWarning',
189-
path.join(SOURCE_ROOT, 'node_modules', 'mocha', 'bin', 'mocha'),
187+
'--experimental-strip-types', '--no-warnings=ExperimentalWarning',
188+
path.join(SOURCE_ROOT, 'node_modules', 'mocha', 'bin', 'mocha'), '--extension=ts,js'
190189
],
191190
);
192191
}

0 commit comments

Comments
 (0)