Skip to content

Commit 084694f

Browse files
mathiasbynensDevtools-frontend LUCI CQ
authored andcommitted
Convert prefer-assert-instance-of to TypeScript
Bug: chromium:397586315 Change-Id: I53a208eab76b5c99c548a27fff1dd1cf2d3f3eb2 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6438914 Commit-Queue: Nikolay Vitkov <[email protected]> Auto-Submit: Mathias Bynens <[email protected]> Reviewed-by: Nikolay Vitkov <[email protected]>
1 parent 84b8647 commit 084694f

File tree

2 files changed

+17
-31
lines changed

2 files changed

+17
-31
lines changed

scripts/eslint_rules/lib/prefer-assert-instance-of.js renamed to scripts/eslint_rules/lib/prefer-assert-instance-of.ts

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
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-
/**
7-
* @fileoverview Prefer `assert.instanceOf` to assert that a value is an instance
8-
* of a class.
9-
*/
10-
11-
// ------------------------------------------------------------------------------
12-
// Rule Definition
13-
// ------------------------------------------------------------------------------
5+
import {createRule} from './tsUtils.ts';
146

157
const FALSY_ASSERTIONS = new Set(['isFalse', 'isNotOk', 'isNotTrue', 'notOk']);
168
const TRUTHY_ASSERTIONS = new Set(['isNotFalse', 'isOk', 'isTrue', 'ok']);
179

18-
/** @type {import('eslint').Rule.RuleModule} */
19-
module.exports = {
10+
export default createRule({
11+
name: 'prefer-assert-instance-of',
2012
meta: {
2113
type: 'suggestion',
2214

@@ -29,32 +21,27 @@ module.exports = {
2921
useAssertNotInstanceOf: 'Use `assert.notInstanceOf` to assert that a value is not an instance of a class',
3022
},
3123
fixable: 'code',
32-
schema: [], // no options
24+
schema: [], // no options
3325
},
34-
create: function (context) {
26+
defaultOptions: [],
27+
create: function(context) {
3528
function isTruthyAssertion(calleeNode) {
36-
if (calleeNode.type === 'Identifier' &&
37-
calleeNode.name === 'assert') {
29+
if (calleeNode.type === 'Identifier' && calleeNode.name === 'assert') {
3830
return true;
3931
}
40-
return calleeNode.type === 'MemberExpression' &&
41-
calleeNode.object.type === 'Identifier' &&
42-
calleeNode.object.name === 'assert' &&
43-
calleeNode.property.type === 'Identifier' &&
44-
TRUTHY_ASSERTIONS.has(calleeNode.property.name);
32+
return calleeNode.type === 'MemberExpression' && calleeNode.object.type === 'Identifier' &&
33+
calleeNode.object.name === 'assert' && calleeNode.property.type === 'Identifier' &&
34+
TRUTHY_ASSERTIONS.has(calleeNode.property.name);
4535
}
4636

4737
function isFalsyAssertion(calleeNode) {
48-
return calleeNode.type === 'MemberExpression' &&
49-
calleeNode.object.type === 'Identifier' &&
50-
calleeNode.object.name === 'assert' &&
51-
calleeNode.property.type === 'Identifier' &&
52-
FALSY_ASSERTIONS.has(calleeNode.property.name);
38+
return calleeNode.type === 'MemberExpression' && calleeNode.object.type === 'Identifier' &&
39+
calleeNode.object.name === 'assert' && calleeNode.property.type === 'Identifier' &&
40+
FALSY_ASSERTIONS.has(calleeNode.property.name);
5341
}
5442

5543
function isInstanceofExpression(argumentNode) {
56-
return argumentNode.type === 'BinaryExpression' &&
57-
argumentNode.operator === 'instanceof';
44+
return argumentNode.type === 'BinaryExpression' && argumentNode.operator === 'instanceof';
5845
}
5946

6047
function reportError(node, calleeText, messageId) {
@@ -86,4 +73,4 @@ module.exports = {
8673
}
8774
};
8875
},
89-
};
76+
});

scripts/eslint_rules/tests/prefer-assert-instance-of.test.js renamed to scripts/eslint_rules/tests/prefer-assert-instance-of.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/prefer-assert-instance-of.js');
5+
import rule from '../lib/prefer-assert-instance-of.ts';
76

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

109
new RuleTester().run('prefer-assert-instance-of', rule, {
1110
valid: [

0 commit comments

Comments
 (0)