-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin-internal): [no-dynamic-tests] new internal Lint rule to ban dynamic syntax in generating tests #11323
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
nayounsang
wants to merge
24
commits into
typescript-eslint:main
Choose a base branch
from
nayounsang:dynamic-lint
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.
+666
−3
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
c1bc74d
test: add test cases
nayounsang 8a2f933
feat: internal no-dynamic-tests rule
nayounsang e83a77d
feat: export rule
nayounsang 5a2d020
fix: more suitable conditions
nayounsang 6a29248
refactor: remove cmts
nayounsang 987cd78
test: add valid tc for noFormat
nayounsang 47396ab
Merge branch 'main' into dynamic-lint
nayounsang 0037283
fix: should allow noFormat tag
nayounsang 9c3ebce
fix: narrow report node
nayounsang ac6726f
test: add nested dynamic test
nayounsang dc8b568
fix: fix err column
nayounsang f81a9de
feat: enable object dynamic value
nayounsang d684378
Merge branch 'main' into dynamic-lint
nayounsang ba32259
feat: key to validate
nayounsang 62d39b7
test: add test case for object value: error and code
nayounsang 9aa122c
fix: ban direct assigned test case
nayounsang 4b9d47a
chore: test commit to enable new rule in CI
nayounsang 53171cd
Merge branch 'main' into dynamic-lint
nayounsang f2b9c1a
chore: make bulk supress file
nayounsang f2f6e5e
chore: utility script for update bulk suprresion
nayounsang 9c8b49a
fix: update lock
nayounsang a3e8ff2
Merge branch 'main' into dynamic-lint
nayounsang 69c8818
fix: revert weird script
nayounsang e668354
Merge branch 'dynamic-lint' of https://github.com/nayounsang/typescri…
nayounsang 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
{ | ||
"packages/eslint-plugin-internal/tests/rules/plugin-test-formatting.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 26 | ||
} | ||
}, | ||
"packages/eslint-plugin/tests/rules/dot-notation.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 1 | ||
} | ||
}, | ||
"packages/eslint-plugin/tests/rules/member-ordering/member-ordering-alphabetically-case-insensitive-order.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 4 | ||
} | ||
}, | ||
"packages/eslint-plugin/tests/rules/member-ordering/member-ordering-alphabetically-order.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 4 | ||
} | ||
}, | ||
"packages/eslint-plugin/tests/rules/naming-convention/naming-convention.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 12 | ||
} | ||
}, | ||
"packages/eslint-plugin/tests/rules/no-extraneous-class.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 12 | ||
} | ||
}, | ||
"packages/eslint-plugin/tests/rules/no-invalid-this.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 43 | ||
} | ||
}, | ||
"packages/eslint-plugin/tests/rules/no-loop-func.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 2 | ||
} | ||
}, | ||
"packages/eslint-plugin/tests/rules/no-this-alias.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 14 | ||
} | ||
}, | ||
"packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 44 | ||
} | ||
}, | ||
"packages/eslint-plugin/tests/rules/no-unnecessary-template-expression.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 1 | ||
} | ||
}, | ||
"packages/eslint-plugin/tests/rules/no-unsafe-enum-comparison.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 2 | ||
} | ||
}, | ||
"packages/eslint-plugin/tests/rules/no-unused-vars/no-unused-vars-eslint.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 154 | ||
} | ||
}, | ||
"packages/eslint-plugin/tests/rules/no-useless-empty-export.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 9 | ||
} | ||
}, | ||
"packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 42 | ||
} | ||
}, | ||
"packages/eslint-plugin/tests/rules/prefer-optional-chain/prefer-optional-chain.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 17 | ||
} | ||
}, | ||
"packages/eslint-plugin/tests/rules/return-await.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 1 | ||
} | ||
}, | ||
"packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 4 | ||
} | ||
}, | ||
"packages/eslint-plugin/tests/rules/unbound-method.test.ts": { | ||
"@typescript-eslint/internal/no-dynamic-tests": { | ||
"count": 3 | ||
} | ||
} | ||
} |
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
140 changes: 140 additions & 0 deletions
140
packages/eslint-plugin-internal/src/rules/no-dynamic-tests.ts
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,140 @@ | ||
import type { InvalidTestCase } from '@typescript-eslint/rule-tester'; | ||
import type { TSESTree } from '@typescript-eslint/utils'; | ||
|
||
import { AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
|
||
import { createRule } from '../util'; | ||
|
||
export default createRule({ | ||
name: 'no-dynamic-tests', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallow dynamic syntax in RuleTester test arrays', | ||
}, | ||
messages: { | ||
noDynamicTests: | ||
'Dynamic syntax is not allowed in RuleTester test arrays. Use static values only.', | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
function isRuleTesterCall(node: TSESTree.Node): boolean { | ||
return ( | ||
node.type === AST_NODE_TYPES.CallExpression && | ||
node.callee.type === AST_NODE_TYPES.MemberExpression && | ||
node.callee.object.type === AST_NODE_TYPES.Identifier && | ||
node.callee.object.name === 'ruleTester' && | ||
node.callee.property.type === AST_NODE_TYPES.Identifier && | ||
node.callee.property.name === 'run' | ||
); | ||
} | ||
|
||
function reportDynamicElements(node: TSESTree.Node): void { | ||
switch (node.type) { | ||
case AST_NODE_TYPES.CallExpression: | ||
case AST_NODE_TYPES.SpreadElement: | ||
case AST_NODE_TYPES.Identifier: | ||
case AST_NODE_TYPES.BinaryExpression: | ||
case AST_NODE_TYPES.ConditionalExpression: | ||
case AST_NODE_TYPES.MemberExpression: | ||
context.report({ | ||
node, | ||
messageId: 'noDynamicTests', | ||
}); | ||
break; | ||
case AST_NODE_TYPES.TemplateLiteral: | ||
node.expressions.forEach(expr => { | ||
reportDynamicElements(expr); | ||
}); | ||
break; | ||
case AST_NODE_TYPES.ArrayExpression: | ||
node.elements.forEach(element => { | ||
if (element) { | ||
reportDynamicElements(element); | ||
} | ||
}); | ||
break; | ||
case AST_NODE_TYPES.ObjectExpression: | ||
node.properties.forEach(prop => { | ||
if (prop.type === AST_NODE_TYPES.SpreadElement) { | ||
context.report({ | ||
node: prop, | ||
messageId: 'noDynamicTests', | ||
}); | ||
} else { | ||
// InvalidTestCase extends ValidTestCase | ||
type TestCaseKey = keyof InvalidTestCase<string, []>; | ||
const keyToValidate: TestCaseKey[] = ['code', 'errors']; | ||
|
||
if ( | ||
prop.key.type === AST_NODE_TYPES.Identifier && | ||
keyToValidate.includes(prop.key.name as TestCaseKey) | ||
) { | ||
reportDynamicElements(prop.value); | ||
} else if ( | ||
prop.key.type === AST_NODE_TYPES.Literal && | ||
keyToValidate.includes(prop.key.value as TestCaseKey) | ||
) { | ||
reportDynamicElements(prop.value); | ||
} | ||
} | ||
}); | ||
break; | ||
case AST_NODE_TYPES.TaggedTemplateExpression: | ||
kirkwaiblinger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( | ||
!( | ||
node.tag.type === AST_NODE_TYPES.Identifier && | ||
node.tag.name === 'noFormat' | ||
) | ||
) { | ||
context.report({ | ||
node: node.tag, | ||
messageId: 'noDynamicTests', | ||
}); | ||
} | ||
break; | ||
case AST_NODE_TYPES.Literal: | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
return { | ||
CallExpression(node) { | ||
if (isRuleTesterCall(node)) { | ||
// If valid code, arg length is always 3 but we need to avoid conflict while dev | ||
if (node.arguments.length < 3) { | ||
return; | ||
} | ||
const testObject = node.arguments[2]; | ||
kirkwaiblinger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (testObject.type === AST_NODE_TYPES.ObjectExpression) { | ||
for (const prop of testObject.properties) { | ||
const isTestCases = | ||
prop.type === AST_NODE_TYPES.Property && | ||
prop.key.type === AST_NODE_TYPES.Identifier && | ||
(prop.key.name === 'valid' || prop.key.name === 'invalid'); | ||
|
||
if (isTestCases) { | ||
if (prop.value.type === AST_NODE_TYPES.ArrayExpression) { | ||
prop.value.elements.forEach(element => { | ||
if (element) { | ||
reportDynamicElements(element); | ||
} | ||
}); | ||
} else { | ||
context.report({ | ||
node: prop.value, | ||
messageId: 'noDynamicTests', | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
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.