From 45f23d51100538197242891c9aa4410a754abf70 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 7 May 2024 19:08:14 +0800 Subject: [PATCH] `prefer-dom-node-dataset`: Ignore `await`ed `getAttribute` call (#2334) --- rules/prefer-dom-node-dataset.js | 11 +++++++++++ test/prefer-dom-node-dataset.mjs | 2 ++ 2 files changed, 13 insertions(+) diff --git a/rules/prefer-dom-node-dataset.js b/rules/prefer-dom-node-dataset.js index db4bfdd6b4..d27788feaf 100644 --- a/rules/prefer-dom-node-dataset.js +++ b/rules/prefer-dom-node-dataset.js @@ -91,6 +91,17 @@ const create = context => ({ return; } + const method = callExpression.callee.property.name; + // Playwright's `Locator#getAttribute()` returns a promise. + // https://playwright.dev/docs/api/class-locator#locator-get-attribute + if ( + callExpression.parent.type === 'AwaitExpression' + && callExpression.parent.argument === callExpression + && method === 'getAttribute' + ) { + return; + } + const attributeName = callExpression.arguments[0].value.toLowerCase(); if (!attributeName.startsWith('data-')) { diff --git a/test/prefer-dom-node-dataset.mjs b/test/prefer-dom-node-dataset.mjs index 722281d136..6b6dc2bc31 100644 --- a/test/prefer-dom-node-dataset.mjs +++ b/test/prefer-dom-node-dataset.mjs @@ -187,6 +187,8 @@ test.snapshot({ // First Argument is not startsWith `data-` 'element.getAttribute("foo-unicorn");', 'element.getAttribute("data");', + // https://github.com/sindresorhus/eslint-plugin-unicorn/issues/2307 + 'await page.locator("text=Hello").getAttribute("data-foo")', ], invalid: [ outdent`