Skip to content

Commit 240377f

Browse files
Lightning00BladeDevtools-frontend LUCI CQ
authored and
Devtools-frontend LUCI CQ
committed
[eslint] Migrate rules to TypeScript
Bug: 397586315 Change-Id: I889070caf10daab07644d9ad84efddb91bb38e9b Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6436651 Commit-Queue: Nikolay Vitkov <nvitkov@chromium.org> Reviewed-by: Benedikt Meurer <bmeurer@chromium.org> Auto-Submit: Nikolay Vitkov <nvitkov@chromium.org> Commit-Queue: Benedikt Meurer <bmeurer@chromium.org>
1 parent f30ec26 commit 240377f

10 files changed

+169
-201
lines changed

scripts/eslint_rules/lib/check-enumerated-histograms.js

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2020 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import {createRule} from './tsUtils.ts';
6+
7+
export default createRule({
8+
name: 'check-enumerated-histograms',
9+
meta: {
10+
type: 'problem',
11+
docs: {
12+
description: 'check arguments when recording enumerated histograms',
13+
category: 'Possible Errors',
14+
},
15+
fixable: 'code',
16+
messages: {
17+
invalidArgument:
18+
'When calling \'recordEnumeratedHistogram\' the third argument should be of the form \'SomeEnum.MAX_VALUE\'.',
19+
},
20+
schema: [], // no options
21+
},
22+
defaultOptions: [],
23+
create: function(context) {
24+
return {
25+
CallExpression(node) {
26+
if (node.callee.type === 'MemberExpression' && node.callee.object.type === 'Identifier' &&
27+
node.callee.object.name === 'InspectorFrontendHostInstance' && node.callee.property.type === 'Identifier' &&
28+
node.callee.property.name === 'recordEnumeratedHistogram') {
29+
const argumentNode = node.arguments[2];
30+
31+
if (argumentNode.type !== 'MemberExpression' || argumentNode.property.type !== 'Identifier' ||
32+
argumentNode.property.name !== 'MAX_VALUE') {
33+
context.report({
34+
node,
35+
messageId: 'invalidArgument',
36+
});
37+
}
38+
}
39+
},
40+
};
41+
},
42+
});

scripts/eslint_rules/lib/check-license-header.js renamed to scripts/eslint_rules/lib/check-license-header.ts

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
/**
6-
* @fileoverview Rule to check license headers
7-
* @author Tim van der Lippe
8-
*/
9-
'use strict';
5+
import type {TSESTree} from '@typescript-eslint/utils';
6+
import * as path from 'path';
107

11-
const path = require('path');
8+
import {createRule} from './tsUtils.ts';
129

1310
const FRONT_END_FOLDER = path.join(
14-
__filename,
11+
// If we want to fix this we need to change to .mts or use a package.json
12+
// with type : module
13+
// @ts-expect-error we don't build the file to CJS
14+
import.meta.filename,
1515
'..',
1616
'..',
1717
'..',
@@ -111,14 +111,10 @@ const OTHER_LICENSE_HEADERS = [
111111
'ui/Widget.js',
112112
];
113113

114-
// ------------------------------------------------------------------------------
115-
// Rule Definition
116-
// ------------------------------------------------------------------------------
117-
118114
/**
119-
* Check each linecomment that should (combined) result in the LINE_LICENSE_HEADER.
115+
* Check each line comment that should (combined) result in the LINE_LICENSE_HEADER.
120116
*/
121-
function isMissingLineCommentLicense(comments) {
117+
function isMissingLineCommentLicense(comments: TSESTree.Comment[]) {
122118
for (let i = 0; i < LINE_REGEXES.length; i++) {
123119
if (!comments[i] || !LINE_REGEXES[i].test(comments[i].value)) {
124120
return true;
@@ -131,39 +127,36 @@ function isMissingLineCommentLicense(comments) {
131127
/**
132128
* We match the whole block comment, including potential leading asterisks of the jsdoc.
133129
*/
134-
function isMissingBlockLineCommentLicense(licenseText) {
130+
function isMissingBlockLineCommentLicense(licenseText: string) {
135131
return !BLOCK_REGEX.test(licenseText);
136132
}
137133

138-
/**
139-
* @type {import('eslint').Rule.RuleModule}
140-
*/
141-
module.exports = {
134+
export default createRule({
135+
name: 'check-license-header',
142136
meta: {
143137
type: 'problem',
144-
145138
docs: {
146139
description: 'check license headers',
147140
category: 'Possible Errors',
148141
},
149142
fixable: 'code',
150-
schema: [], // no options
143+
schema: [], // no options
144+
messages: {
145+
missingLicense: 'Missing license header',
146+
incorrectLineLicense: 'Incorrect line license header',
147+
incorrectBlockLicense: 'Incorrect block license header',
148+
},
151149
},
152-
create: function (context) {
150+
defaultOptions: [],
151+
create: function(context) {
153152
const sourceCode = context.sourceCode ?? context.getSourceCode();
154153
const filename = context.filename ?? context.getFilename();
155154
const fileName = filename;
156155
// Fix windows paths for exemptions
157-
const relativePath = path
158-
.relative(FRONT_END_FOLDER, fileName)
159-
.replace(/\\/g, '/');
160-
161-
if (
162-
relativePath.startsWith('third_party') ||
163-
fileName.endsWith('TestRunner.js') ||
164-
EXCLUDED_FILES.includes(relativePath) ||
165-
OTHER_LICENSE_HEADERS.includes(relativePath)
166-
) {
156+
const relativePath = path.relative(FRONT_END_FOLDER, fileName).replace(/\\/g, '/');
157+
158+
if (relativePath.startsWith('third_party') || fileName.endsWith('TestRunner.js') ||
159+
EXCLUDED_FILES.includes(relativePath) || OTHER_LICENSE_HEADERS.includes(relativePath)) {
167160
return {};
168161
}
169162

@@ -180,7 +173,7 @@ module.exports = {
180173
(comments.length === 1 && comments[0].type === 'Shebang')) {
181174
context.report({
182175
node,
183-
message: 'Missing license header',
176+
messageId: 'missingLicense',
184177
fix(fixer) {
185178
return fixer.insertTextBefore(node, LICENSE_HEADER_ADDITION);
186179
},
@@ -202,30 +195,28 @@ module.exports = {
202195
if (isMissingLineCommentLicense(commentsToCheck)) {
203196
context.report({
204197
node,
205-
message: 'Incorrect line license header',
198+
messageId: 'incorrectLineLicense',
206199
fix(fixer) {
207200
return fixer.insertTextBefore(
208-
firstCommentToCheck,
209-
LICENSE_HEADER_ADDITION,
201+
firstCommentToCheck,
202+
LICENSE_HEADER_ADDITION,
210203
);
211204
},
212205
});
213206
}
214-
} else if (
215-
isMissingBlockLineCommentLicense(firstCommentToCheck.value)
216-
) {
207+
} else if (isMissingBlockLineCommentLicense(firstCommentToCheck.value)) {
217208
context.report({
218209
node,
219-
message: 'Incorrect block license header',
210+
messageId: 'incorrectBlockLicense',
220211
fix(fixer) {
221212
return fixer.insertTextBefore(
222-
firstCommentToCheck,
223-
LICENSE_HEADER_ADDITION,
213+
firstCommentToCheck,
214+
LICENSE_HEADER_ADDITION,
224215
);
225216
},
226217
});
227218
}
228219
},
229220
};
230221
},
231-
};
222+
});

scripts/eslint_rules/lib/check-test-definitions.js renamed to scripts/eslint_rules/lib/check-test-definitions.ts

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,29 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
/**
6-
* @fileoverview Rule to check test definitions
7-
* @author Tim van der Lippe
8-
*/
9-
'use strict';
5+
import type {TSESTree} from '@typescript-eslint/types';
106

11-
const TEST_NAME_REGEX = /^\[crbug.com\/\d+\]/;
7+
import {createRule} from './tsUtils.ts';
128

13-
// ------------------------------------------------------------------------------
14-
// Rule Definition
15-
// ------------------------------------------------------------------------------
9+
const TEST_NAME_REGEX = /^\[crbug.com\/\d+\]/;
1610

17-
function getTextValue(node) {
11+
function getTextValue(node: TSESTree.Node): string|undefined {
1812
if (node.type === 'Literal') {
19-
return node.value;
13+
return node.value?.toString();
2014
}
2115
if (node.type === 'TemplateLiteral') {
2216
if (node.quasis.length === 0) {
23-
return undefined;
17+
return;
2418
}
2519
return node.quasis[0].value.cooked;
2620
}
21+
return;
2722
}
2823

29-
/**
30-
* @type {import('eslint').Rule.RuleModule}
31-
*/
32-
module.exports = {
24+
export default createRule({
25+
name: 'check-test-definitions',
3326
meta: {
3427
type: 'problem',
35-
3628
docs: {
3729
description: 'check test implementations',
3830
category: 'Possible Errors',
@@ -42,11 +34,12 @@ module.exports = {
4234
'Skipped tests must have a CRBug included in the description: `it.skip(\'[crbug.com/BUGID]: testname\', async() => {})',
4335
extraBugId:
4436
'Non-skipped tests cannot include a CRBug tag at the beginning of the description: `it.skip(\'testname (crbug.com/BUGID)\', async() => {})',
45-
comment: 'A skipped test must have an attached comment with an explanation written before the test'
37+
comment: 'A skipped test must have an attached comment with an explanation written before the test',
4638
},
4739
fixable: 'code',
48-
schema: [] // no options
40+
schema: [], // no options
4941
},
42+
defaultOptions: [],
5043
create: function(context) {
5144
const sourceCode = context.sourceCode ?? context.getSourceCode();
5245
return {
@@ -57,7 +50,7 @@ module.exports = {
5750

5851
if ((node.object.name === 'it' || node.object.name === 'describe' || node.object.name === 'itScreenshot') &&
5952
(node.property.name === 'skip' || node.property.name === 'skipOnPlatforms') &&
60-
node.parent.type === 'CallExpression') {
53+
node.parent?.type === 'CallExpression') {
6154
const testNameNode = node.property.name === 'skip' ? node.parent.arguments[0] : node.parent.arguments[1];
6255

6356
if (!testNameNode) {
@@ -81,7 +74,7 @@ module.exports = {
8174
}
8275
},
8376

84-
CallExpression(node) {
77+
CallExpression(node: TSESTree.CallExpression) {
8578
if (node.callee.type === 'Identifier' && node.callee.name === 'it' && node.arguments[0]) {
8679
const textValue = getTextValue(node.arguments[0]);
8780

@@ -92,7 +85,7 @@ module.exports = {
9285
});
9386
}
9487
}
95-
}
88+
},
9689
};
97-
}
98-
};
90+
},
91+
});

scripts/eslint_rules/lib/check-was-shown-methods.js

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)