From 8dae81e879c802bbd9b8d7449859cfe6ef408c96 Mon Sep 17 00:00:00 2001 From: Mark Skelton Date: Wed, 14 Feb 2024 20:25:52 -0600 Subject: [PATCH] feat: Support variable references in `prefer-web-first-assertions` (#210) --- .prettierrc | 3 +- package.json | 1 + pnpm-lock.yaml | 273 ++++++++++++++++++ src/rules/expect-expect.ts | 3 +- src/rules/missing-playwright-await.ts | 3 +- src/rules/prefer-to-contain.ts | 3 +- src/rules/prefer-web-first-assertions.ts | 27 +- src/utils/misc.ts | 6 + test/spec/prefer-web-first-assertions.spec.ts | 46 +++ test/utils/rule-tester.ts | 6 +- 10 files changed, 360 insertions(+), 11 deletions(-) diff --git a/.prettierrc b/.prettierrc index 85887cf..72630a2 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,4 +1,5 @@ { "proseWrap": "always", - "singleQuote": true + "singleQuote": true, + "plugins": ["prettier-plugin-jsdoc"] } diff --git a/package.json b/package.json index 38d20db..ccc5b99 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "eslint-plugin-sort": "^2.10.0", "jest": "^29.7.0", "prettier": "^3.0.3", + "prettier-plugin-jsdoc": "^1.3.0", "semantic-release": "^23.0.2", "ts-jest": "^29.1.1", "tsup": "^8.0.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4bf637b..84c9de3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -54,6 +54,9 @@ importers: prettier: specifier: ^3.0.3 version: 3.0.3 + prettier-plugin-jsdoc: + specifier: ^1.3.0 + version: 1.3.0(prettier@3.0.3) semantic-release: specifier: ^23.0.2 version: 23.0.2(typescript@5.2.2) @@ -1414,6 +1417,12 @@ packages: '@babel/types': 7.23.0 dev: true + /@types/debug@4.1.12: + resolution: {integrity: sha512-vIChWdVG3LG1SMxEvI/AK+FWJthlrqlTu7fbrlywTkkaONwk/UAGaULXRlf8vkzFBLVm0zkMdCquhL5aOjhXPQ==} + dependencies: + '@types/ms': 0.7.34 + dev: true + /@types/eslint@8.44.3: resolution: {integrity: sha512-iM/WfkwAhwmPff3wZuPLYiHX18HI24jU8k1ZSH7P8FHwxTjZ2P6CoX2wnF43oprR+YXJM6UUxATkNvyv/JHd+g==} dependencies: @@ -1454,6 +1463,16 @@ packages: /@types/json-schema@7.0.13: resolution: {integrity: sha512-RbSSoHliUbnXj3ny0CNFOoxrIDV6SUGyStHsvDqosw6CkdPV8TtWGlfecuK4ToyMEAql6pzNxgCFKanovUzlgQ==} + /@types/mdast@4.0.3: + resolution: {integrity: sha512-LsjtqsyF+d2/yFOYaN22dHZI1Cpwkrj+g06G8+qtUKlhovPW89YhqSnfKtMbkgmEtYpH2gydRNULd6y8mciAFg==} + dependencies: + '@types/unist': 3.0.2 + dev: true + + /@types/ms@0.7.34: + resolution: {integrity: sha512-nG96G3Wp6acyAgJqGasjODb+acrI7KltPiRxzHPXnP3NgI28bpQDRv53olbqGXbfcgF5aiiHmO3xpwEpS5Ld9g==} + dev: true + /@types/node@20.11.17: resolution: {integrity: sha512-QmgQZGWu1Yw9TDyAP9ZzpFJKynYNeOvwMJmaxABfieQoVoiVOS6MN1WSpqpRcbeA5+RW82kraAVxCCJg+780Qw==} dependencies: @@ -1475,6 +1494,10 @@ packages: resolution: {integrity: sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==} dev: true + /@types/unist@3.0.2: + resolution: {integrity: sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ==} + dev: true + /@types/yargs-parser@21.0.1: resolution: {integrity: sha512-axdPBuLuEJt0c4yI5OZssC19K2Mq1uKdrfZBzuxLvaztgqUtFYZUNw7lETExPYJR9jdEoIg4mb7RQKRQzOkeGQ==} dev: true @@ -1941,6 +1964,10 @@ packages: engines: {node: '>=8'} dev: true + /binary-searching@2.0.5: + resolution: {integrity: sha512-v4N2l3RxL+m4zDxyxz3Ne2aTmiPn8ZUpKFpdPtO+ItW1NcTCXA7JeHG5GMBSvoKSkQZ9ycS+EouDVxYB9ufKWA==} + dev: true + /bottleneck@2.19.5: resolution: {integrity: sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw==} dev: true @@ -2044,6 +2071,10 @@ packages: engines: {node: '>=10'} dev: true + /character-entities@2.0.2: + resolution: {integrity: sha512-shx7oQ0Awen/BRIdkjkvz54PnEEI/EjwXDSIZp86/KKdbafHh1Df/RYGBhn4hbe2+uKC9FnT5UCEdyPz3ai9hQ==} + dev: true + /chokidar@3.5.3: resolution: {integrity: sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw==} engines: {node: '>= 8.10.0'} @@ -2147,6 +2178,11 @@ packages: engines: {node: '>= 6'} dev: true + /comment-parser@1.4.1: + resolution: {integrity: sha512-buhp5kePrmda3vhc5B9t7pUQXAb2Tnd0qgpkIhPhkHXxJpiPJ11H0ZEU0oBpJ2QztSbzG/ZxMj/CHsYJqRHmyg==} + engines: {node: '>= 12.0.0'} + dev: true + /compare-func@2.0.0: resolution: {integrity: sha512-zHig5N+tPWARooBnb0Zx1MFcdfpyJrfTJ3Y5L+IFvUm8rM74hHz66z0gw0x4tijh5CorKkKUCnW82R2vmpeCRA==} dependencies: @@ -2280,6 +2316,12 @@ packages: dependencies: ms: 2.1.2 + /decode-named-character-reference@1.0.2: + resolution: {integrity: sha512-O8x12RzrUF8xyVcY0KJowWsmaJxQbmy0/EtnNtHRpsOcT7dFk5W598coHqBVpmWo1oQQfsCqfCmkZN5DJrZVdg==} + dependencies: + character-entities: 2.0.2 + dev: true + /dedent@1.5.1: resolution: {integrity: sha512-+LxW+KLWxu3HW3M2w2ympwtqPrqYRzU8fqi6Fhd18fBALe15blJPI/I4+UHveMVG6lJqB4JNd4UG0S5cnVHwIg==} peerDependencies: @@ -2306,11 +2348,22 @@ packages: resolution: {integrity: sha512-xmHIy4F3scKVwMsQ4WnVaS8bHOx0DmVwRywosKhaILI0ywMDWPtBSku2HNxRvF7jtwDRsoEwYQSfbxj8b7RlJQ==} dev: true + /dequal@2.0.3: + resolution: {integrity: sha512-0je+qPKHEMohvfRTCEo3CrPG6cAzAYgmzKyxRiYSSDkS6eGJdyVJm7WaYA5ECaAD9wLB2T4EEeymA5aFVcYXCA==} + engines: {node: '>=6'} + dev: true + /detect-newline@3.1.0: resolution: {integrity: sha512-TLz+x/vEXm/Y7P7wn1EJFNLxYpUD4TgMosxY6fAVJUnJMbupHBOncxyWUG9OpTaH9EBD7uFI5LfEgmMOc54DsA==} engines: {node: '>=8'} dev: true + /devlop@1.1.0: + resolution: {integrity: sha512-RWmIqhcFf1lRYBvNmr7qTNuyCt/7/ns2jbpp1+PalgE/rDQcBT0fioSMUpJ93irlUhC5hrg4cYqe6U+0ImW0rA==} + dependencies: + dequal: 2.0.3 + dev: true + /diff-sequences@29.6.3: resolution: {integrity: sha512-EjePK1srD3P08o2j4f0ExnylqRs5B9tJjcp9t1krH2qRi8CCdsYfwe9JgSLurFBWwq4uOlipzfk5fHNvwFKr8Q==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} @@ -3930,6 +3983,31 @@ packages: hasBin: true dev: true + /mdast-util-from-markdown@2.0.0: + resolution: {integrity: sha512-n7MTOr/z+8NAX/wmhhDji8O3bRvPTV/U0oTCaZJkjhPSKTPhS3xufVhKGF8s1pJ7Ox4QgoIU7KHseh09S+9rTA==} + dependencies: + '@types/mdast': 4.0.3 + '@types/unist': 3.0.2 + decode-named-character-reference: 1.0.2 + devlop: 1.1.0 + mdast-util-to-string: 4.0.0 + micromark: 4.0.0 + micromark-util-decode-numeric-character-reference: 2.0.1 + micromark-util-decode-string: 2.0.0 + micromark-util-normalize-identifier: 2.0.0 + micromark-util-symbol: 2.0.0 + micromark-util-types: 2.0.0 + unist-util-stringify-position: 4.0.0 + transitivePeerDependencies: + - supports-color + dev: true + + /mdast-util-to-string@4.0.0: + resolution: {integrity: sha512-0H44vDimn51F0YwvxSJSm0eCDOJTRlmN0R1yBh4HLj9wiV1Dn0QoXGbvFAWj2hSItVTlCmBF1hqKlIyUBVFLPg==} + dependencies: + '@types/mdast': 4.0.3 + dev: true + /meow@12.1.1: resolution: {integrity: sha512-BhXM0Au22RwUneMPwSCnyhTOizdWoIEPU9sp0Aqa1PnDMR5Wv2FGXYDjuzJEIX+Eo2Rb8xuYe5jrnm5QowQFkw==} engines: {node: '>=16.10'} @@ -3943,6 +4021,181 @@ packages: resolution: {integrity: sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==} engines: {node: '>= 8'} + /micromark-core-commonmark@2.0.0: + resolution: {integrity: sha512-jThOz/pVmAYUtkroV3D5c1osFXAMv9e0ypGDOIZuCeAe91/sD6BoE2Sjzt30yuXtwOYUmySOhMas/PVyh02itA==} + dependencies: + decode-named-character-reference: 1.0.2 + devlop: 1.1.0 + micromark-factory-destination: 2.0.0 + micromark-factory-label: 2.0.0 + micromark-factory-space: 2.0.0 + micromark-factory-title: 2.0.0 + micromark-factory-whitespace: 2.0.0 + micromark-util-character: 2.1.0 + micromark-util-chunked: 2.0.0 + micromark-util-classify-character: 2.0.0 + micromark-util-html-tag-name: 2.0.0 + micromark-util-normalize-identifier: 2.0.0 + micromark-util-resolve-all: 2.0.0 + micromark-util-subtokenize: 2.0.0 + micromark-util-symbol: 2.0.0 + micromark-util-types: 2.0.0 + dev: true + + /micromark-factory-destination@2.0.0: + resolution: {integrity: sha512-j9DGrQLm/Uhl2tCzcbLhy5kXsgkHUrjJHg4fFAeoMRwJmJerT9aw4FEhIbZStWN8A3qMwOp1uzHr4UL8AInxtA==} + dependencies: + micromark-util-character: 2.1.0 + micromark-util-symbol: 2.0.0 + micromark-util-types: 2.0.0 + dev: true + + /micromark-factory-label@2.0.0: + resolution: {integrity: sha512-RR3i96ohZGde//4WSe/dJsxOX6vxIg9TimLAS3i4EhBAFx8Sm5SmqVfR8E87DPSR31nEAjZfbt91OMZWcNgdZw==} + dependencies: + devlop: 1.1.0 + micromark-util-character: 2.1.0 + micromark-util-symbol: 2.0.0 + micromark-util-types: 2.0.0 + dev: true + + /micromark-factory-space@2.0.0: + resolution: {integrity: sha512-TKr+LIDX2pkBJXFLzpyPyljzYK3MtmllMUMODTQJIUfDGncESaqB90db9IAUcz4AZAJFdd8U9zOp9ty1458rxg==} + dependencies: + micromark-util-character: 2.1.0 + micromark-util-types: 2.0.0 + dev: true + + /micromark-factory-title@2.0.0: + resolution: {integrity: sha512-jY8CSxmpWLOxS+t8W+FG3Xigc0RDQA9bKMY/EwILvsesiRniiVMejYTE4wumNc2f4UbAa4WsHqe3J1QS1sli+A==} + dependencies: + micromark-factory-space: 2.0.0 + micromark-util-character: 2.1.0 + micromark-util-symbol: 2.0.0 + micromark-util-types: 2.0.0 + dev: true + + /micromark-factory-whitespace@2.0.0: + resolution: {integrity: sha512-28kbwaBjc5yAI1XadbdPYHX/eDnqaUFVikLwrO7FDnKG7lpgxnvk/XGRhX/PN0mOZ+dBSZ+LgunHS+6tYQAzhA==} + dependencies: + micromark-factory-space: 2.0.0 + micromark-util-character: 2.1.0 + micromark-util-symbol: 2.0.0 + micromark-util-types: 2.0.0 + dev: true + + /micromark-util-character@2.1.0: + resolution: {integrity: sha512-KvOVV+X1yLBfs9dCBSopq/+G1PcgT3lAK07mC4BzXi5E7ahzMAF8oIupDDJ6mievI6F+lAATkbQQlQixJfT3aQ==} + dependencies: + micromark-util-symbol: 2.0.0 + micromark-util-types: 2.0.0 + dev: true + + /micromark-util-chunked@2.0.0: + resolution: {integrity: sha512-anK8SWmNphkXdaKgz5hJvGa7l00qmcaUQoMYsBwDlSKFKjc6gjGXPDw3FNL3Nbwq5L8gE+RCbGqTw49FK5Qyvg==} + dependencies: + micromark-util-symbol: 2.0.0 + dev: true + + /micromark-util-classify-character@2.0.0: + resolution: {integrity: sha512-S0ze2R9GH+fu41FA7pbSqNWObo/kzwf8rN/+IGlW/4tC6oACOs8B++bh+i9bVyNnwCcuksbFwsBme5OCKXCwIw==} + dependencies: + micromark-util-character: 2.1.0 + micromark-util-symbol: 2.0.0 + micromark-util-types: 2.0.0 + dev: true + + /micromark-util-combine-extensions@2.0.0: + resolution: {integrity: sha512-vZZio48k7ON0fVS3CUgFatWHoKbbLTK/rT7pzpJ4Bjp5JjkZeasRfrS9wsBdDJK2cJLHMckXZdzPSSr1B8a4oQ==} + dependencies: + micromark-util-chunked: 2.0.0 + micromark-util-types: 2.0.0 + dev: true + + /micromark-util-decode-numeric-character-reference@2.0.1: + resolution: {integrity: sha512-bmkNc7z8Wn6kgjZmVHOX3SowGmVdhYS7yBpMnuMnPzDq/6xwVA604DuOXMZTO1lvq01g+Adfa0pE2UKGlxL1XQ==} + dependencies: + micromark-util-symbol: 2.0.0 + dev: true + + /micromark-util-decode-string@2.0.0: + resolution: {integrity: sha512-r4Sc6leeUTn3P6gk20aFMj2ntPwn6qpDZqWvYmAG6NgvFTIlj4WtrAudLi65qYoaGdXYViXYw2pkmn7QnIFasA==} + dependencies: + decode-named-character-reference: 1.0.2 + micromark-util-character: 2.1.0 + micromark-util-decode-numeric-character-reference: 2.0.1 + micromark-util-symbol: 2.0.0 + dev: true + + /micromark-util-encode@2.0.0: + resolution: {integrity: sha512-pS+ROfCXAGLWCOc8egcBvT0kf27GoWMqtdarNfDcjb6YLuV5cM3ioG45Ys2qOVqeqSbjaKg72vU+Wby3eddPsA==} + dev: true + + /micromark-util-html-tag-name@2.0.0: + resolution: {integrity: sha512-xNn4Pqkj2puRhKdKTm8t1YHC/BAjx6CEwRFXntTaRf/x16aqka6ouVoutm+QdkISTlT7e2zU7U4ZdlDLJd2Mcw==} + dev: true + + /micromark-util-normalize-identifier@2.0.0: + resolution: {integrity: sha512-2xhYT0sfo85FMrUPtHcPo2rrp1lwbDEEzpx7jiH2xXJLqBuy4H0GgXk5ToU8IEwoROtXuL8ND0ttVa4rNqYK3w==} + dependencies: + micromark-util-symbol: 2.0.0 + dev: true + + /micromark-util-resolve-all@2.0.0: + resolution: {integrity: sha512-6KU6qO7DZ7GJkaCgwBNtplXCvGkJToU86ybBAUdavvgsCiG8lSSvYxr9MhwmQ+udpzywHsl4RpGJsYWG1pDOcA==} + dependencies: + micromark-util-types: 2.0.0 + dev: true + + /micromark-util-sanitize-uri@2.0.0: + resolution: {integrity: sha512-WhYv5UEcZrbAtlsnPuChHUAsu/iBPOVaEVsntLBIdpibO0ddy8OzavZz3iL2xVvBZOpolujSliP65Kq0/7KIYw==} + dependencies: + micromark-util-character: 2.1.0 + micromark-util-encode: 2.0.0 + micromark-util-symbol: 2.0.0 + dev: true + + /micromark-util-subtokenize@2.0.0: + resolution: {integrity: sha512-vc93L1t+gpR3p8jxeVdaYlbV2jTYteDje19rNSS/H5dlhxUYll5Fy6vJ2cDwP8RnsXi818yGty1ayP55y3W6fg==} + dependencies: + devlop: 1.1.0 + micromark-util-chunked: 2.0.0 + micromark-util-symbol: 2.0.0 + micromark-util-types: 2.0.0 + dev: true + + /micromark-util-symbol@2.0.0: + resolution: {integrity: sha512-8JZt9ElZ5kyTnO94muPxIGS8oyElRJaiJO8EzV6ZSyGQ1Is8xwl4Q45qU5UOg+bGH4AikWziz0iN4sFLWs8PGw==} + dev: true + + /micromark-util-types@2.0.0: + resolution: {integrity: sha512-oNh6S2WMHWRZrmutsRmDDfkzKtxF+bc2VxLC9dvtrDIRFln627VsFP6fLMgTryGDljgLPjkrzQSDcPrjPyDJ5w==} + dev: true + + /micromark@4.0.0: + resolution: {integrity: sha512-o/sd0nMof8kYff+TqcDx3VSrgBTcZpSvYcAHIfHhv5VAuNmisCxjhx6YmxS8PFEpb9z5WKWKPdzf0jM23ro3RQ==} + dependencies: + '@types/debug': 4.1.12 + debug: 4.3.4 + decode-named-character-reference: 1.0.2 + devlop: 1.1.0 + micromark-core-commonmark: 2.0.0 + micromark-factory-space: 2.0.0 + micromark-util-character: 2.1.0 + micromark-util-chunked: 2.0.0 + micromark-util-combine-extensions: 2.0.0 + micromark-util-decode-numeric-character-reference: 2.0.1 + micromark-util-encode: 2.0.0 + micromark-util-normalize-identifier: 2.0.0 + micromark-util-resolve-all: 2.0.0 + micromark-util-sanitize-uri: 2.0.0 + micromark-util-subtokenize: 2.0.0 + micromark-util-symbol: 2.0.0 + micromark-util-types: 2.0.0 + transitivePeerDependencies: + - supports-color + dev: true + /micromatch@4.0.5: resolution: {integrity: sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==} engines: {node: '>=8.6'} @@ -4417,6 +4670,20 @@ packages: resolution: {integrity: sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==} engines: {node: '>= 0.8.0'} + /prettier-plugin-jsdoc@1.3.0(prettier@3.0.3): + resolution: {integrity: sha512-cQm8xIa0fN9ieJFMXACQd6JPycl+8ouOijAqUqu44EF/s4fXL3Wi9sKXuEaodsEWgCN42Xby/bNhqgM1iWx4uw==} + engines: {node: '>=14.13.1 || >=16.0.0'} + peerDependencies: + prettier: ^3.0.0 + dependencies: + binary-searching: 2.0.5 + comment-parser: 1.4.1 + mdast-util-from-markdown: 2.0.0 + prettier: 3.0.3 + transitivePeerDependencies: + - supports-color + dev: true + /prettier@3.0.3: resolution: {integrity: sha512-L/4pUDMxcNa8R/EthV08Zt42WBO4h1rarVtK0K+QJG0X187OLo7l699jWw0GKuwzkPQ//jMFA/8Xm6Fh3J/DAg==} engines: {node: '>=14'} @@ -5176,6 +5443,12 @@ packages: crypto-random-string: 4.0.0 dev: true + /unist-util-stringify-position@4.0.0: + resolution: {integrity: sha512-0ASV06AAoKCDkS2+xw5RXJywruurpbC4JZSm7nr7MOt1ojAzvyyaO+UxZf18j8FCF6kmzCZKcAgN/yu2gm2XgQ==} + dependencies: + '@types/unist': 3.0.2 + dev: true + /universal-user-agent@6.0.0: resolution: {integrity: sha512-isyNax3wXoKaulPDZWHQqbmIx1k2tb9fb3GGDBRxCscfYV2Ch7WxPArBsFEG8s/safwXTT7H4QGhaIkTp9447w==} dev: true diff --git a/src/rules/expect-expect.ts b/src/rules/expect-expect.ts index b28c2f4..1c331e2 100644 --- a/src/rules/expect-expect.ts +++ b/src/rules/expect-expect.ts @@ -1,6 +1,7 @@ import { Rule } from 'eslint'; import ESTree from 'estree'; import { dig, isExpectCall, isTestCall } from '../utils/ast'; +import { getSourceCode } from '../utils/misc'; function isAssertionCall( context: Rule.RuleContext, @@ -20,7 +21,7 @@ export default { ...((context.options?.[0] as Record) ?? {}), }; - const sourceCode = context.sourceCode ?? context.getSourceCode(); + const sourceCode = getSourceCode(context); const unchecked: ESTree.CallExpression[] = []; function checkExpressions(nodes: ESTree.Node[]) { diff --git a/src/rules/missing-playwright-await.ts b/src/rules/missing-playwright-await.ts index 00b0bef..d1652b5 100644 --- a/src/rules/missing-playwright-await.ts +++ b/src/rules/missing-playwright-await.ts @@ -7,6 +7,7 @@ import { isIdentifier, isPropertyAccessor, } from '../utils/ast'; +import { getSourceCode } from '../utils/misc'; const validTypes = new Set([ 'AwaitExpression', @@ -100,7 +101,7 @@ function getCallType( export default { create(context) { - const sourceCode = context.sourceCode ?? context.getSourceCode(); + const sourceCode = getSourceCode(context); const options = context.options[0] || {}; const awaitableMatchers = new Set([ ...expectPlaywrightMatchers, diff --git a/src/rules/prefer-to-contain.ts b/src/rules/prefer-to-contain.ts index 494f677..8a20fe1 100644 --- a/src/rules/prefer-to-contain.ts +++ b/src/rules/prefer-to-contain.ts @@ -5,6 +5,7 @@ import { isBooleanLiteral, isPropertyAccessor, } from '../utils/ast'; +import { getSourceCode } from '../utils/misc'; import { parseExpectCall } from '../utils/parseExpectCall'; import { KnownCallExpression } from '../utils/types'; @@ -48,7 +49,7 @@ export default { context.report({ fix(fixer) { - const sourceCode = context.sourceCode ?? context.getSourceCode(); + const sourceCode = getSourceCode(context); // We need to negate the expectation if the current expected // value is itself negated by the "not" modifier diff --git a/src/rules/prefer-web-first-assertions.ts b/src/rules/prefer-web-first-assertions.ts index aa79123..cd5fa10 100644 --- a/src/rules/prefer-web-first-assertions.ts +++ b/src/rules/prefer-web-first-assertions.ts @@ -1,6 +1,7 @@ import { Rule } from 'eslint'; import ESTree from 'estree'; import { getRawValue, getStringValue, isBooleanLiteral } from '../utils/ast'; +import { getSourceCode } from '../utils/misc'; import { parseExpectCall } from '../utils/parseExpectCall'; type MethodConfig = { @@ -53,9 +54,26 @@ const supportedMatchers = new Set([ 'toBeFalsy', ]); -export function getMatcherCall(node: ESTree.Node) { - const grandparent = (node as any).parent?.parent as ESTree.Node; - return grandparent.type === 'CallExpression' ? grandparent : undefined; +/** + * If the expect call argument is a variable reference, finds the variable + * initializer. + */ +function dereference(context: Rule.RuleContext, node: ESTree.Node) { + if (node.type !== 'Identifier') { + return node; + } + + const sourceCode = getSourceCode(context); + const scope = sourceCode.getScope(node); + + // Find the variable declaration and return the initializer + for (const ref of scope.references) { + const refParent = (ref.identifier as Rule.Node).parent; + + if (refParent.type === 'VariableDeclarator') { + return refParent.init; + } + } } export default { @@ -65,8 +83,9 @@ export default { const expectCall = parseExpectCall(context, node); if (!expectCall) return; - const [arg] = node.arguments; + const arg = dereference(context, node.arguments[0]); if ( + !arg || arg.type !== 'AwaitExpression' || arg.argument.type !== 'CallExpression' || arg.argument.callee.type !== 'MemberExpression' diff --git a/src/utils/misc.ts b/src/utils/misc.ts index a095ea6..216569e 100644 --- a/src/utils/misc.ts +++ b/src/utils/misc.ts @@ -1,4 +1,10 @@ +import { Rule } from 'eslint'; + export const getAmountData = (amount: number) => ({ amount: amount.toString(), s: amount === 1 ? '' : 's', }); + +export function getSourceCode(context: Rule.RuleContext) { + return context.sourceCode ?? context.getSourceCode(); +} diff --git a/test/spec/prefer-web-first-assertions.spec.ts b/test/spec/prefer-web-first-assertions.spec.ts index b9a5966..6be9205 100644 --- a/test/spec/prefer-web-first-assertions.spec.ts +++ b/test/spec/prefer-web-first-assertions.spec.ts @@ -659,6 +659,45 @@ runRuleTester('prefer-web-first-assertions', rule, { }, }, }, + // Variable references + { + code: test(` + const myValue = await page.locator('.foo').isVisible(); + expect(myValue).toBe(true); + `), + errors: [ + { + column: 9, + data: { matcher: 'toBeVisible', method: 'isVisible' }, + endColumn: 24, + line: 3, + messageId: 'useWebFirstAssertion', + }, + ], + output: test(` + const myValue = page.locator('.foo'); + await expect(myValue).toBeVisible(); + `), + }, + { + code: test(` + const content = await foo.textContent(); + expect(content).toBe("bar") + `), + errors: [ + { + column: 9, + data: { matcher: 'toHaveText', method: 'textContent' }, + endColumn: 24, + line: 3, + messageId: 'useWebFirstAssertion', + }, + ], + output: test(` + const content = foo; + await expect(content).toHaveText("bar") + `), + }, ], valid: [ { code: test('await expect(page.locator(".tweet")).toBeVisible()') }, @@ -678,5 +717,12 @@ runRuleTester('prefer-web-first-assertions', rule, { }, }, }, + // Variable references + { + code: test(` + const myValue = page.locator('.foo'); + expect(myValue).toBeVisible(); + `), + }, ], }); diff --git a/test/utils/rule-tester.ts b/test/utils/rule-tester.ts index 00007e7..aade69e 100644 --- a/test/utils/rule-tester.ts +++ b/test/utils/rule-tester.ts @@ -2,12 +2,12 @@ import { RuleTester } from 'eslint'; /** * @example - * import rule from '../../src/rules/missing-playwright-await'; + * import rule from '../../src/rules/missing-playwright-await'; * - * runRuleTester('missing-playwright-await', rule, { + * runRuleTester('missing-playwright-await', rule, { * invalid: ['expect(page.locator('checkbox')).toBeChecked()'], * valid: ['await expect(page.locator('checkbox')).toBeChecked()'], - * }); + * }); */ export function runRuleTester(...args: Parameters) { const config = {