-
-
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
6
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.
+311
−0
Open
Changes from all commits
Commits
Show all changes
6 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 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
94 changes: 94 additions & 0 deletions
94
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,94 @@ | ||
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 isDynamicExpression(node: TSESTree.Node): boolean { | ||
switch (node.type) { | ||
case AST_NODE_TYPES.CallExpression: | ||
return true; | ||
case AST_NODE_TYPES.SpreadElement: | ||
return true; | ||
case AST_NODE_TYPES.Identifier: | ||
return true; | ||
case AST_NODE_TYPES.TemplateLiteral: | ||
return node.expressions.some(expr => isDynamicExpression(expr)); | ||
case AST_NODE_TYPES.BinaryExpression: | ||
return true; | ||
case AST_NODE_TYPES.ConditionalExpression: | ||
return true; | ||
case AST_NODE_TYPES.MemberExpression: | ||
return true; | ||
case AST_NODE_TYPES.ArrayExpression: | ||
return node.elements.some( | ||
element => element && isDynamicExpression(element), | ||
); | ||
case AST_NODE_TYPES.ObjectExpression: | ||
return node.properties.some(prop => { | ||
if (prop.type === AST_NODE_TYPES.SpreadElement) { | ||
return true; | ||
} | ||
return isDynamicExpression(prop.value); | ||
}); | ||
case AST_NODE_TYPES.Literal: | ||
case AST_NODE_TYPES.TaggedTemplateExpression: | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
return { | ||
CallExpression(node) { | ||
if (isRuleTesterCall(node)) { | ||
const testObject = node.arguments[2]; | ||
if (testObject.type === AST_NODE_TYPES.ObjectExpression) { | ||
for (const prop of testObject.properties) { | ||
if ( | ||
prop.type === AST_NODE_TYPES.Property && | ||
prop.key.type === AST_NODE_TYPES.Identifier && | ||
(prop.key.name === 'valid' || prop.key.name === 'invalid') && | ||
prop.value.type === AST_NODE_TYPES.ArrayExpression | ||
) { | ||
prop.value.elements.forEach(element => { | ||
if (element && isDynamicExpression(element)) { | ||
context.report({ | ||
node: element, | ||
messageId: 'noDynamicTests', | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
215 changes: 215 additions & 0 deletions
215
packages/eslint-plugin-internal/tests/rules/no-dynamic-tests.test.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,215 @@ | ||
import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
|
||
import rule from '../../src/rules/no-dynamic-tests'; | ||
|
||
const ruleTester = new RuleTester({ | ||
languageOptions: { | ||
parserOptions: { | ||
ecmaFeatures: {}, | ||
ecmaVersion: 6, | ||
sourceType: 'module', | ||
}, | ||
}, | ||
}); | ||
|
||
ruleTester.run('no-dynamic-tests', rule, { | ||
invalid: [ | ||
// Function calls in test arrays | ||
{ | ||
code: ` | ||
ruleTester.run('test', rule, { | ||
valid: [generateTestCases()], | ||
invalid: [], | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
column: 11, | ||
line: 3, | ||
messageId: 'noDynamicTests', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
ruleTester.run('test', rule, { | ||
valid: [], | ||
invalid: [...getInvalidCases()], | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
column: 13, | ||
line: 4, | ||
messageId: 'noDynamicTests', | ||
}, | ||
], | ||
}, | ||
// Spread operator in test arrays | ||
{ | ||
code: ` | ||
ruleTester.run('test', rule, { | ||
valid: [...validTestCases], | ||
invalid: [], | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
column: 11, | ||
line: 3, | ||
messageId: 'noDynamicTests', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
ruleTester.run('test', rule, { | ||
valid: [...validTestCases.map(t => t.code)], | ||
invalid: [], | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
column: 11, | ||
line: 3, | ||
messageId: 'noDynamicTests', | ||
}, | ||
], | ||
}, | ||
// Simple identifiers in test arrays | ||
{ | ||
code: ` | ||
ruleTester.run('test', rule, { | ||
valid: [testCase], | ||
invalid: [], | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
column: 11, | ||
line: 3, | ||
messageId: 'noDynamicTests', | ||
}, | ||
], | ||
}, | ||
// Template literals in test arrays | ||
{ | ||
code: ` | ||
ruleTester.run('test', rule, { | ||
valid: [\`\${getTest()}\`], | ||
invalid: [], | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
column: 11, | ||
line: 3, | ||
messageId: 'noDynamicTests', | ||
}, | ||
], | ||
}, | ||
// Binary expressions in test arrays | ||
{ | ||
code: ` | ||
ruleTester.run('test', rule, { | ||
valid: ['test' + getSuffix()], | ||
invalid: [], | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
column: 11, | ||
line: 3, | ||
messageId: 'noDynamicTests', | ||
}, | ||
], | ||
}, | ||
// Conditional expressions in test arrays | ||
{ | ||
code: ` | ||
ruleTester.run('test', rule, { | ||
valid: [shouldTest ? 'test1' : 'test2'], | ||
invalid: [], | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
column: 11, | ||
line: 3, | ||
messageId: 'noDynamicTests', | ||
}, | ||
], | ||
}, | ||
// Member expressions in test arrays | ||
{ | ||
code: ` | ||
ruleTester.run('test', rule, { | ||
valid: [testConfig.cases], | ||
invalid: [], | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
column: 11, | ||
line: 3, | ||
messageId: 'noDynamicTests', | ||
}, | ||
], | ||
}, | ||
// Object spread | ||
{ | ||
code: ` | ||
ruleTester.run('test', rule, { | ||
valid: [{ ...testConfig }], | ||
invalid: [], | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
column: 11, | ||
line: 3, | ||
messageId: 'noDynamicTests', | ||
}, | ||
], | ||
}, | ||
], | ||
valid: [ | ||
{ | ||
code: ` | ||
ruleTester.run('test', rule, { | ||
valid: ['const x = 1;'], | ||
invalid: [], | ||
}); | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
ruleTester.run('test', rule, { | ||
valid: ['const x = 1;', 'let y = 2;'], | ||
invalid: [ | ||
{ | ||
code: 'var z = 3;', | ||
errors: [{ messageId: 'error' }], | ||
}, | ||
], | ||
}); | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
ruleTester.run('test', rule, { | ||
valid: [{ code: 'const x = 1;' }, { code: 'let y = 2;' }], | ||
invalid: [], | ||
}); | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
ruleTester.run('test', rule, { | ||
valid: [noFormat\`const x = 1;\`], | ||
invalid: [], | ||
}); | ||
`, | ||
}, | ||
], | ||
}); |
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.