-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): adds rule ban-instanceof-html-element (#27178)
- Loading branch information
1 parent
a97f513
commit a3463ec
Showing
11 changed files
with
217 additions
and
8 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@fluentui-eslint-plugin-9788e3e7-808d-4201-b04e-cead71f27b9e.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "minor", | ||
"comment": "feat: rule to ban usage of instanceof HTMLElement", | ||
"packageName": "@fluentui/eslint-plugin", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-utilities-851262b0-ac71-4e89-ae48-e53733715e8a.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "chore: exports isHTMLElement as public", | ||
"packageName": "@fluentui/react-utilities", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
packages/eslint-plugin/src/rules/ban-instanceof-html-element/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// @ts-check | ||
const { AST_NODE_TYPES } = require('@typescript-eslint/experimental-utils'); | ||
const createRule = require('../../utils/createRule'); | ||
|
||
/** | ||
* @typedef {import("@typescript-eslint/types/dist/ts-estree").BinaryExpression} BinaryExpression | ||
* @typedef {import("@fluentui/react-utilities/src/utils/isHTMLElement").HTMLElementConstructorName} HTMLElementConstructorName | ||
* | ||
*/ | ||
|
||
module.exports = createRule({ | ||
name: 'ban-instanceof-html-element', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Ban usage of instanceof HTMLElement comparison', | ||
category: 'Possible Errors', | ||
recommended: 'error', | ||
}, | ||
messages: { | ||
invalidBinaryExpression: 'instanceof {{right}} should be avoided, use isHTMLElement instead.', | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create: context => ({ | ||
/** | ||
* @param {BinaryExpression} binaryExpression | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
BinaryExpression(binaryExpression) { | ||
if ( | ||
binaryExpression.operator === 'instanceof' && | ||
binaryExpression.right.type === AST_NODE_TYPES.Identifier && | ||
constructorNamesSet.has(binaryExpression.right.name) | ||
) { | ||
context.report({ | ||
node: binaryExpression, | ||
messageId: 'invalidBinaryExpression', | ||
data: { | ||
right: binaryExpression.right.name, | ||
}, | ||
}); | ||
} | ||
}, | ||
}), | ||
}); | ||
|
||
/** @type {HTMLElementConstructorName[]} */ | ||
const constructorNames = [ | ||
'HTMLElement', | ||
'HTMLAnchorElement', | ||
'HTMLAreaElement', | ||
'HTMLAudioElement', | ||
'HTMLBaseElement', | ||
'HTMLBodyElement', | ||
'HTMLBRElement', | ||
'HTMLButtonElement', | ||
'HTMLCanvasElement', | ||
'HTMLDataElement', | ||
'HTMLDataListElement', | ||
'HTMLDetailsElement', | ||
'HTMLDialogElement', | ||
'HTMLDivElement', | ||
'HTMLDListElement', | ||
'HTMLEmbedElement', | ||
'HTMLFieldSetElement', | ||
'HTMLFormElement', | ||
'HTMLHeadingElement', | ||
'HTMLHeadElement', | ||
'HTMLHRElement', | ||
'HTMLHtmlElement', | ||
'HTMLIFrameElement', | ||
'HTMLImageElement', | ||
'HTMLInputElement', | ||
'HTMLModElement', | ||
'HTMLLabelElement', | ||
'HTMLLegendElement', | ||
'HTMLLIElement', | ||
'HTMLLinkElement', | ||
'HTMLMapElement', | ||
'HTMLMetaElement', | ||
'HTMLMeterElement', | ||
'HTMLObjectElement', | ||
'HTMLOListElement', | ||
'HTMLOptGroupElement', | ||
'HTMLOptionElement', | ||
'HTMLOutputElement', | ||
'HTMLParagraphElement', | ||
'HTMLParamElement', | ||
'HTMLPreElement', | ||
'HTMLProgressElement', | ||
'HTMLQuoteElement', | ||
'HTMLSlotElement', | ||
'HTMLScriptElement', | ||
'HTMLSelectElement', | ||
'HTMLSourceElement', | ||
'HTMLSpanElement', | ||
'HTMLStyleElement', | ||
'HTMLTableElement', | ||
'HTMLTableColElement', | ||
'HTMLTableRowElement', | ||
'HTMLTableSectionElement', | ||
'HTMLTemplateElement', | ||
'HTMLTextAreaElement', | ||
'HTMLTimeElement', | ||
'HTMLTitleElement', | ||
'HTMLTrackElement', | ||
'HTMLUListElement', | ||
'HTMLVideoElement', | ||
]; | ||
|
||
/** @type {Set<string>} */ | ||
const constructorNamesSet = new Set(constructorNames); |
38 changes: 38 additions & 0 deletions
38
packages/eslint-plugin/src/rules/ban-instanceof-html-element/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// @ts-nocheck | ||
const { ESLintUtils } = require('@typescript-eslint/experimental-utils'); | ||
const rule = require('./index'); | ||
|
||
const ruleTester = new ESLintUtils.RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('ban-instanceof-htmlelement', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
event.currentTarget instanceof Object | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
isHTMLElement(event.currentTarget) | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
event.currentTarget instanceof HTMLElement | ||
event.currentTarget instanceof HTMLInputElement | ||
`, | ||
errors: [{ messageId: 'invalidBinaryExpression' }, { messageId: 'invalidBinaryExpression' }], | ||
}, | ||
{ | ||
code: ` | ||
if (event.currentTarget instanceof HTMLElement) {} | ||
if (event.currentTarget instanceof HTMLInputElement) {} | ||
`, | ||
errors: [{ messageId: 'invalidBinaryExpression' }, { messageId: 'invalidBinaryExpression' }], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/react-components/react-utilities/src/utils/isHTMLElement.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters