-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
chore(eslint-plugin-internal): [plugin-test-formatting] support random object literal tests #5895
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9d928ca
chore(eslint-plugin-internal): [plugin-test-formatting] support rando…
bradzacher 15793c1
Merge branch 'main' into check-variables-formatting-plugin-test
bradzacher 0b10187
update test column
bradzacher ffff806
Merge branch 'main' into check-variables-formatting-plugin-test
bradzacher 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { getContextualType } from '@typescript-eslint/type-utils'; | ||
import type { TSESTree } from '@typescript-eslint/utils'; | ||
import { AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'; | ||
import { format, resolveConfig } from 'prettier'; | ||
|
||
import { createRule } from '../util'; | ||
|
@@ -108,6 +109,7 @@ export default createRule<Options, MessageIds>({ | |
docs: { | ||
description: `Enforces that eslint-plugin test snippets are correctly formatted`, | ||
recommended: 'error', | ||
requiresTypeChecking: true, | ||
}, | ||
fixable: 'code', | ||
schema: [ | ||
|
@@ -146,6 +148,11 @@ export default createRule<Options, MessageIds>({ | |
], | ||
create(context, [{ formatWithPrettier }]) { | ||
const sourceCode = context.getSourceCode(); | ||
const { program, esTreeNodeToTSNodeMap } = | ||
ESLintUtils.getParserServices(context); | ||
const checker = program.getTypeChecker(); | ||
|
||
const checkedObjects = new Set<TSESTree.ObjectExpression>(); | ||
|
||
function prettierFormat( | ||
code: string, | ||
|
@@ -448,6 +455,12 @@ export default createRule<Options, MessageIds>({ | |
test: TSESTree.ObjectExpression, | ||
isErrorTest = true, | ||
): void { | ||
if (checkedObjects.has(test)) { | ||
return; | ||
} | ||
|
||
checkedObjects.add(test); | ||
|
||
for (const prop of test.properties) { | ||
if ( | ||
prop.type !== AST_NODE_TYPES.Property || | ||
|
@@ -478,33 +491,99 @@ export default createRule<Options, MessageIds>({ | |
} | ||
} | ||
|
||
const invalidTestsSelectorPath = [ | ||
AST_NODE_TYPES.CallExpression, | ||
AST_NODE_TYPES.ObjectExpression, | ||
'Property[key.name = "invalid"]', | ||
AST_NODE_TYPES.ArrayExpression, | ||
AST_NODE_TYPES.ObjectExpression, | ||
]; | ||
|
||
return { | ||
// valid | ||
'CallExpression > ObjectExpression > Property[key.name = "valid"] > ArrayExpression': | ||
checkValidTest, | ||
// invalid - errors | ||
[invalidTestsSelectorPath.join(' > ')]: checkInvalidTest, | ||
// invalid - suggestions | ||
[[ | ||
...invalidTestsSelectorPath, | ||
'Property[key.name = "errors"]', | ||
AST_NODE_TYPES.ArrayExpression, | ||
AST_NODE_TYPES.CallExpression, | ||
AST_NODE_TYPES.ObjectExpression, | ||
'Property[key.name = "suggestions"]', | ||
'Property[key.name = "invalid"]', | ||
AST_NODE_TYPES.ArrayExpression, | ||
AST_NODE_TYPES.ObjectExpression, | ||
].join(' > ')]: checkInvalidTest, | ||
// special case for our batchedSingleLineTests utility | ||
'CallExpression[callee.name = "batchedSingleLineTests"] > ObjectExpression': | ||
checkInvalidTest, | ||
|
||
/** | ||
* generic, type-aware handling for any old object | ||
* this is a fallback to handle random variables people declare or object | ||
* literals that are passed via array maps, etc | ||
*/ | ||
ObjectExpression(node): void { | ||
if (checkedObjects.has(node)) { | ||
return; | ||
} | ||
|
||
const type = getContextualType( | ||
checker, | ||
esTreeNodeToTSNodeMap.get(node), | ||
); | ||
if (!type) { | ||
return; | ||
} | ||
|
||
const typeString = checker.typeToString(type); | ||
if (/^RunTests\b/.test(typeString)) { | ||
checkedObjects.add(node); | ||
|
||
for (const prop of node.properties) { | ||
if ( | ||
prop.type === AST_NODE_TYPES.SpreadElement || | ||
prop.computed || | ||
prop.key.type !== AST_NODE_TYPES.Identifier || | ||
prop.value.type !== AST_NODE_TYPES.ArrayExpression | ||
) { | ||
continue; | ||
} | ||
|
||
switch (prop.key.name) { | ||
case 'valid': | ||
checkValidTest(prop.value); | ||
break; | ||
|
||
case 'invalid': | ||
for (const element of prop.value.elements) { | ||
if (element.type === AST_NODE_TYPES.ObjectExpression) { | ||
checkInvalidTest(element); | ||
} | ||
} | ||
break; | ||
} | ||
} | ||
return; | ||
} | ||
|
||
if (/^ValidTestCase\b/.test(typeString)) { | ||
checkInvalidTest(node); | ||
return; | ||
} | ||
|
||
if (/^InvalidTestCase\b/.test(typeString)) { | ||
checkInvalidTest(node); | ||
for (const testProp of node.properties) { | ||
if ( | ||
testProp.type === AST_NODE_TYPES.SpreadElement || | ||
testProp.computed || | ||
testProp.key.type !== AST_NODE_TYPES.Identifier || | ||
testProp.key.name !== 'errors' || | ||
testProp.value.type !== AST_NODE_TYPES.ArrayExpression | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this is almost the same as the previous |
||
) { | ||
continue; | ||
} | ||
|
||
for (const errorElement of testProp.value.elements) { | ||
if (errorElement.type !== AST_NODE_TYPES.ObjectExpression) { | ||
continue; | ||
} | ||
|
||
checkInvalidTest(errorElement); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
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
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.