summaryrefslogtreecommitdiffstats
path: root/remote/test/puppeteer/tools/eslint/src/use-using.ts
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /remote/test/puppeteer/tools/eslint/src/use-using.ts
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'remote/test/puppeteer/tools/eslint/src/use-using.ts')
-rw-r--r--remote/test/puppeteer/tools/eslint/src/use-using.ts85
1 files changed, 85 insertions, 0 deletions
diff --git a/remote/test/puppeteer/tools/eslint/src/use-using.ts b/remote/test/puppeteer/tools/eslint/src/use-using.ts
new file mode 100644
index 0000000000..0c727a4334
--- /dev/null
+++ b/remote/test/puppeteer/tools/eslint/src/use-using.ts
@@ -0,0 +1,85 @@
+/**
+ * @license
+ * Copyright 2023 Google Inc.
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+import {ESLintUtils, TSESTree} from '@typescript-eslint/utils';
+
+const usingSymbols = ['ElementHandle', 'JSHandle'];
+
+const createRule = ESLintUtils.RuleCreator(name => {
+ return `https://github.com/puppeteer/puppeteer/tree/main/tools/eslint/${name}.js`;
+});
+
+const useUsingRule = createRule<[], 'useUsing' | 'useUsingFix'>({
+ name: 'use-using',
+ meta: {
+ docs: {
+ description: "Requires 'using' for element/JS handles.",
+ requiresTypeChecking: true,
+ },
+ hasSuggestions: true,
+ messages: {
+ useUsing: "Use 'using'.",
+ useUsingFix: "Replace with 'using' to ignore.",
+ },
+ schema: [],
+ type: 'problem',
+ },
+ defaultOptions: [],
+ create(context) {
+ const services = ESLintUtils.getParserServices(context);
+ const checker = services.program.getTypeChecker();
+
+ return {
+ VariableDeclaration(node): void {
+ if (['using', 'await using'].includes(node.kind) || node.declare) {
+ return;
+ }
+ for (const declaration of node.declarations) {
+ if (declaration.id.type === TSESTree.AST_NODE_TYPES.Identifier) {
+ const tsNode = services.esTreeNodeToTSNodeMap.get(declaration.id);
+ const type = checker.getTypeAtLocation(tsNode);
+ let isElementHandleReference = false;
+ if (type.isUnionOrIntersection()) {
+ for (const member of type.types) {
+ if (
+ member.symbol !== undefined &&
+ usingSymbols.includes(member.symbol.escapedName as string)
+ ) {
+ isElementHandleReference = true;
+ break;
+ }
+ }
+ } else {
+ isElementHandleReference =
+ type.symbol !== undefined
+ ? usingSymbols.includes(type.symbol.escapedName as string)
+ : false;
+ }
+ if (isElementHandleReference) {
+ context.report({
+ node: declaration.id,
+ messageId: 'useUsing',
+ suggest: [
+ {
+ messageId: 'useUsingFix',
+ fix(fixer) {
+ return fixer.replaceTextRange(
+ [node.range[0], node.range[0] + node.kind.length],
+ 'using'
+ );
+ },
+ },
+ ],
+ });
+ }
+ }
+ }
+ },
+ };
+ },
+});
+
+export = useUsingRule;