-
Notifications
You must be signed in to change notification settings - Fork 9
feat(eslint): add eslint plugin, add expect method call lint rule COMPASS-10060 #600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Anemy
wants to merge
2
commits into
main
Choose a base branch
from
COMPASS-10060-add-throw-eslint-rule
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,3 @@ | ||
| ignores: | ||
| - '@mongodb-js/prettier-config-devtools' | ||
| - '@mongodb-js/eslint-config-devtools' |
This file contains hidden or 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,2 @@ | ||
| .nyc-output | ||
| dist |
This file contains hidden or 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,9 @@ | ||
| 'use strict'; | ||
| module.exports = { | ||
| root: true, | ||
| extends: ['@mongodb-js/eslint-config-devtools'], | ||
| parserOptions: { | ||
| tsconfigRootDir: __dirname, | ||
| project: ['./tsconfig-lint.json'], | ||
| }, | ||
| }; |
This file contains hidden or 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,6 @@ | ||
| 'use strict'; | ||
| module.exports = { | ||
| ...require('@mongodb-js/mocha-config-devtools'), | ||
| spec: ['rules/**/*.test.js'], | ||
| watchFiles: ['rules/**/*.js'], | ||
| }; |
This file contains hidden or 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 @@ | ||
| "@mongodb-js/prettier-config-devtools" |
This file contains hidden or 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,6 @@ | ||
| 'use strict'; | ||
| module.exports = { | ||
| rules: { | ||
| 'no-expect-method-without-call': require('./rules/no-expect-method-without-call'), | ||
| }, | ||
| }; |
This file contains hidden or 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,36 @@ | ||
| { | ||
| "name": "@mongodb-js/eslint-plugin-devtools", | ||
| "description": "Custom eslint rules for DevTools", | ||
| "homepage": "https://github.com/mongodb-js/devtools-shared", | ||
| "version": "0.1.0", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/mongodb-js/devtools-shared.git" | ||
| }, | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "license": "SSPL", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "eslint": "eslint", | ||
| "prettier": "prettier", | ||
| "lint": "npm run eslint . && npm run prettier -- --check .", | ||
| "depcheck": "depcheck", | ||
| "check": "npm run lint && npm run depcheck", | ||
| "check-ci": "npm run check", | ||
| "test": "mocha", | ||
| "test-cov": "nyc --compact=false --produce-source-map=false -x \"**/*.spec.*\" --reporter=lcov --reporter=text --reporter=html npm run test", | ||
| "test-watch": "npm run test -- --watch", | ||
| "test-ci": "npm run test-cov", | ||
| "reformat": "npm run eslint . -- --fix && npm run prettier -- --write ." | ||
| }, | ||
| "devDependencies": { | ||
| "@mongodb-js/mocha-config-devtools": "^1.0.5", | ||
| "@mongodb-js/prettier-config-devtools": "^1.0.1", | ||
| "depcheck": "^1.4.7", | ||
| "eslint": "^7.25.0", | ||
| "mocha": "^8.4.0", | ||
| "nyc": "^15.1.0" | ||
| } | ||
| } |
119 changes: 119 additions & 0 deletions
119
configs/eslint-plugin-devtools/rules/no-expect-method-without-call.js
This file contains hidden or 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,119 @@ | ||
| 'use strict'; | ||
|
|
||
| const PROPERTY_TERMINATORS = [ | ||
| // Default chai terminators. | ||
| 'ok', | ||
| 'true', | ||
| 'false', | ||
| 'null', | ||
| 'undefined', | ||
| 'exist', | ||
| 'empty', | ||
| 'arguments', | ||
| 'NaN', | ||
| 'extensible', | ||
| 'sealed', | ||
| 'frozen', | ||
| 'finite', | ||
|
|
||
| // Terminators from chai-as-promised. | ||
| 'fulfilled', | ||
| 'rejected', | ||
|
|
||
| // Terminators from sinon-chai. | ||
| 'called', | ||
| 'calledOnce', | ||
| 'calledTwice', | ||
| 'calledThrice', | ||
| 'calledWithNew', | ||
|
|
||
| // Terminators from chai-dom. | ||
| 'displayed', | ||
| 'visible', | ||
| 'focus', | ||
| 'checked', | ||
| ]; | ||
|
|
||
| function followNodeChainToExpectCall(node) { | ||
| while (node) { | ||
| if (node.type === 'CallExpression') { | ||
| if (node.callee.type === 'Identifier' && node.callee.name === 'expect') { | ||
| return node; | ||
| } | ||
| if (node.callee.type === 'MemberExpression') { | ||
| node = node.callee.object; | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| // Continue on the node chain (e.g. .not, .to). | ||
| if (node.type === 'MemberExpression') { | ||
| node = node.object; | ||
| continue; | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| } | ||
|
|
||
| /** @type {import('eslint').Rule.RuleModule} */ | ||
| module.exports = { | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: | ||
| 'Require invocation of expect method assertions (e.g., to.throw())', | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| const options = context.options[0] ?? {}; | ||
| const additionalTerminators = options.properties ?? []; | ||
| const validTerminators = [ | ||
| ...PROPERTY_TERMINATORS, | ||
| ...additionalTerminators, | ||
| ]; | ||
|
|
||
| return { | ||
| MemberExpression(node) { | ||
| if ( | ||
| node.type !== 'MemberExpression' || | ||
| validTerminators.includes(node.property.name) || | ||
| node.property.name === 'expect' | ||
| ) { | ||
| return null; | ||
| } | ||
|
|
||
| const isInvoked = | ||
| node.parent && | ||
| node.parent.type === 'CallExpression' && | ||
| node.parent.callee === node; | ||
|
|
||
| const isPartOfLongerChain = | ||
| node.parent && | ||
| node.parent.type === 'MemberExpression' && | ||
| node.parent.object === node; | ||
|
|
||
| if (isInvoked || isPartOfLongerChain) { | ||
| return null; | ||
| } | ||
|
|
||
| const expectCall = followNodeChainToExpectCall(node); | ||
| if (!expectCall) { | ||
| return null; | ||
| } | ||
|
|
||
| const source = context.getSourceCode(); | ||
| const calleeText = source.getText(node); | ||
| const expectText = source.getText(expectCall); | ||
| const assertionText = calleeText.substring(expectText.length + 1); | ||
|
|
||
| context.report({ | ||
| node, | ||
| // message: `"${assertionText}" used as function` | ||
| message: `expect().${assertionText} must be invoked with parentheses, e.g., expect().${assertionText}()`, | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.