Skip to content

Commit c18fac6

Browse files
Lightning00BladeDevtools-frontend LUCI CQ
authored andcommitted
[eslint] Migrate rules to TypeScript
Rule that were migrated: no-it-screenshot-only-or-repeat no-screenshot-test-outside-perf-panel prefer-url-string static-custom-event-names Bug: 397586315 Change-Id: I7e9cc01e01663906ce301a9f2f7021e22305c665 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6450703 Commit-Queue: Nikolay Vitkov <[email protected]> Reviewed-by: Benedikt Meurer <[email protected]> Commit-Queue: Benedikt Meurer <[email protected]> Auto-Submit: Nikolay Vitkov <[email protected]>
1 parent eb7e352 commit c18fac6

10 files changed

+313
-250
lines changed

scripts/eslint_rules/lib/no-it-screenshot-only-or-repeat.js renamed to scripts/eslint_rules/lib/no-it-screenshot-only-or-repeat.ts

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

5-
'use strict';
6-
/**
7-
* @type {import('eslint').Rule.RuleModule}
8-
*/
9-
module.exports = {
5+
import {createRule} from './tsUtils.ts';
6+
7+
type MessageIds = `itScreenshot-${'only'|'repeat'}`;
8+
9+
const BANNED_IT_EXTENSIONS = new Set(['only', 'repeat']);
10+
11+
export default createRule<[], MessageIds>({
12+
name: 'no-it-screenshot-only-or-repeat',
1013
meta: {
1114
type: 'problem',
12-
1315
docs: {
1416
description: 'ensure no itScreenshot.only or itScreenshot.repeat calls',
1517
category: 'Possible Errors',
1618
},
1719
messages: {
18-
// Formatted like this to make it easily to dynamically look up the
19-
// message based on the invalid property name.
2020
'itScreenshot-only': 'Focused screenshot tests are not allowed.',
2121
'itScreenshot-repeat': 'Repeated screenshot tests are not allowed.',
2222
},
23-
fixable: 'code',
24-
schema: [] // no options
23+
schema: [], // no options
2524
},
25+
defaultOptions: [],
2626
create: function(context) {
27-
const BANNED_IT_EXTENSIONS = new Set(['only', 'repeat']);
2827
return {
29-
'CallExpression[callee.type="MemberExpression"][callee.object.name="itScreenshot"]'(node) {
28+
CallExpression(node): void {
29+
if (node.callee.type !== 'MemberExpression' || node.callee.object.type !== 'Identifier' ||
30+
node.callee.object.name !== 'itScreenshot' || node.callee.property.type !== 'Identifier') {
31+
return;
32+
}
33+
3034
const calleePropertyName = node.callee.property.name;
3135
if (!BANNED_IT_EXTENSIONS.has(calleePropertyName)) {
3236
return;
3337
}
34-
const errorMessageId = `itScreenshot-${calleePropertyName}`;
3538

36-
// We report the node.callee.property as the bad node so that in an editor
37-
// only the ".only" / ".repeat" part is highlighted as an error, else it is very
38-
// distracting when you're working on debugging a test and the entire
39-
// body of the test is highlighted as an error.
39+
// Construct the message ID dynamically and assert its type
40+
const errorMessageId = `itScreenshot-${calleePropertyName}` as MessageIds;
41+
42+
// Report the specific property (.only or .repeat) as the error location
4043
context.report({node: node.callee.property, messageId: errorMessageId});
41-
}
44+
},
4245
};
43-
}
44-
};
46+
},
47+
});

scripts/eslint_rules/lib/no-screenshot-test-outside-perf-panel.js

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2023 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 * as path from 'path';
6+
7+
import {createRule} from './tsUtils.ts';
8+
9+
const PERFORMANCE_PANEL_INTERACTION_TESTS_PATH = path.join(
10+
// @ts-expect-error
11+
import.meta.dirname,
12+
'..',
13+
'..',
14+
'..',
15+
'test',
16+
'interactions',
17+
'panels',
18+
'performance',
19+
);
20+
const UI_COMPONENTS_PATH = path.join(
21+
// @ts-expect-error
22+
import.meta.dirname,
23+
'..',
24+
'..',
25+
'..',
26+
'test',
27+
'interactions',
28+
'ui',
29+
'components',
30+
);
31+
32+
export default createRule({
33+
name: 'no-screenshot-test-outside-perf-panel',
34+
meta: {
35+
type: 'problem',
36+
docs: {
37+
description: 'Bans writing screenshot tests outside the directory for the Performance Panel interaction tests.',
38+
category: 'Possible Errors',
39+
},
40+
messages: {
41+
invalidScreenshotTest:
42+
'It is banned to write screenshot tests outside the directory of the Performance Panel interaction tests or UI components tests.',
43+
},
44+
schema: [], // no options
45+
},
46+
defaultOptions: [],
47+
create: function(context) {
48+
const filename: string = context.filename;
49+
const absoluteFileName: string = path.resolve(filename);
50+
51+
if (absoluteFileName.includes(PERFORMANCE_PANEL_INTERACTION_TESTS_PATH) ||
52+
absoluteFileName.includes(UI_COMPONENTS_PATH)) {
53+
return {};
54+
}
55+
56+
return {
57+
CallExpression(node) {
58+
if (node.callee.type === 'Identifier' && node.callee.name === 'itScreenshot') {
59+
context.report({
60+
node,
61+
messageId: 'invalidScreenshotTest',
62+
});
63+
}
64+
},
65+
66+
MemberExpression(node) {
67+
if (node.object.type === 'Identifier' && node.object.name === 'itScreenshot' &&
68+
node.property.type === 'Identifier' && (node.property.name === 'skip' || node.property.name === 'only')) {
69+
context.report({
70+
node,
71+
messageId: 'invalidScreenshotTest',
72+
});
73+
}
74+
},
75+
};
76+
},
77+
});

scripts/eslint_rules/lib/prefer-url-string.js

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

0 commit comments

Comments
 (0)