-
Notifications
You must be signed in to change notification settings - Fork 0
feat: rule to enforece 2 digit dates in e2e tests #32
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
harshal015
wants to merge
2
commits into
master
Choose a base branch
from
require-date-day-two-digits
base: master
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { ESLintUtils, AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
|
|
||
| const createRule = ESLintUtils.RuleCreator( | ||
| () => 'https://github.com/fylein/fyle-eslint-plugin/blob/main/packages/docs/rules/require-date-day-two-digits.md', | ||
| ); | ||
|
|
||
| const RULE_NAME = 'require-date-day-two-digits'; | ||
|
|
||
| export default createRule({ | ||
| name: RULE_NAME, | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: | ||
| "Require 'day' option to use '2-digit' when calling Date.prototype.toLocaleDateString to ensure two-digit day formatting (e.g., '01 Mar' not '1 Mar').", | ||
| recommended: 'recommended', | ||
| }, | ||
| schema: [], | ||
| messages: { | ||
| requireTwoDigit: "Use day: '2-digit' in toLocaleDateString options to produce two-digit day values.", | ||
| }, | ||
| }, | ||
| defaultOptions: [], | ||
| create(context) { | ||
| function isToLocaleDateStringCall(node) { | ||
| if (node.type !== AST_NODE_TYPES.CallExpression) return false; | ||
| const callee = node.callee; | ||
| if (callee.type !== AST_NODE_TYPES.MemberExpression) return false; | ||
| const prop = callee.property; | ||
| if (prop.type !== AST_NODE_TYPES.Identifier) return false; | ||
| return prop.name === 'toLocaleDateString'; | ||
| } | ||
|
|
||
| return { | ||
| CallExpression(node) { | ||
| if (!isToLocaleDateStringCall(node)) return; | ||
|
|
||
| // Determine which argument is the options object. | ||
| // toLocaleDateString(locales?, options?) -> options is arg[1] if arg0 is string/array, else arg[0] if it's an object | ||
| const args = node.arguments || []; | ||
| let optionsArg = null; | ||
| if (args.length >= 2) { | ||
| optionsArg = args[1]; | ||
| } else if (args.length === 1) { | ||
| const first = args[0]; | ||
| if (first.type === AST_NODE_TYPES.ObjectExpression) optionsArg = first; | ||
| } | ||
|
|
||
| if (!optionsArg) return; | ||
|
|
||
| // If optionsArg is not object literal, skip | ||
| if (optionsArg.type !== AST_NODE_TYPES.ObjectExpression) return; | ||
|
|
||
| // Find day property | ||
| for (const prop of optionsArg.properties) { | ||
| if (prop.type !== AST_NODE_TYPES.Property) continue; | ||
| const key = prop.key; | ||
| const value = prop.value; | ||
| const keyName = | ||
| key.type === AST_NODE_TYPES.Identifier | ||
| ? key.name | ||
| : key.type === AST_NODE_TYPES.Literal | ||
| ? String(key.value) | ||
| : null; | ||
| if (keyName === 'day') { | ||
| if (value.type === AST_NODE_TYPES.Literal && value.value === 'numeric') { | ||
| context.report({ node: value, messageId: 'requireTwoDigit' }); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
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,67 @@ | ||
| import { ESLintUtils, AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
|
|
||
| const createRule = ESLintUtils.RuleCreator( | ||
| () => 'https://github.com/fylein/fyle-eslint-plugin/blob/main/packages/docs/rules/require-test-assignee-comment.md', | ||
| ); | ||
|
|
||
| const RULE_NAME = 'require-test-assignee-comment'; | ||
|
|
||
| const ASSIGNEE_COMMENT_PATTERN = /Assignee:\s+@[\w.-]+/i; | ||
|
|
||
| export default createRule({ | ||
| name: RULE_NAME, | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: 'Require an explicit assignee comment for Playwright `test.fixme` and `test.fail` cases.', | ||
| recommended: 'recommended', | ||
| }, | ||
| schema: [], | ||
| messages: { | ||
| missingAssignee: | ||
| 'test.fixme/test.fail must include an explicit assignee comment immediately above the call, e.g. "// @assignee @username".', | ||
| }, | ||
| }, | ||
| defaultOptions: [], | ||
| create(context) { | ||
| const sourceCode = context.getSourceCode(); | ||
|
|
||
| function isTestFixmeOrFailCall(node) { | ||
| if (node.type !== AST_NODE_TYPES.CallExpression) { | ||
| return false; | ||
| } | ||
|
|
||
| const callee = node.callee; | ||
| if (callee.type !== AST_NODE_TYPES.MemberExpression) { | ||
| return false; | ||
| } | ||
|
|
||
| const object = callee.object; | ||
| const property = callee.property; | ||
| if (object.type !== AST_NODE_TYPES.Identifier || object.name !== 'test') { | ||
| return false; | ||
| } | ||
|
|
||
| return property.type === AST_NODE_TYPES.Identifier && (property.name === 'fixme' || property.name === 'fail'); | ||
| } | ||
|
|
||
| function hasAssigneeComment(node) { | ||
| const statement = node.parent && node.parent.type === AST_NODE_TYPES.ExpressionStatement ? node.parent : node; | ||
| const leadingComments = sourceCode.getCommentsBefore(statement); | ||
|
|
||
| return leadingComments.some((comment) => ASSIGNEE_COMMENT_PATTERN.test(comment.value)); | ||
| } | ||
|
|
||
| return { | ||
| CallExpression(node) { | ||
| if (!isTestFixmeOrFailCall(node)) { | ||
| return; | ||
| } | ||
|
|
||
| if (!hasAssigneeComment(node)) { | ||
| context.report({ node, messageId: 'missingAssignee' }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
38 changes: 38 additions & 0 deletions
38
packages/core/src/tests/require-date-day-two-digits.test.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,38 @@ | ||
| import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
| import tsParser from '@typescript-eslint/parser'; | ||
| import rule from '../rules/require-date-day-two-digits.js'; | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| languageOptions: { | ||
| parser: tsParser, | ||
| parserOptions: { | ||
| ecmaVersion: 2020, | ||
| sourceType: 'module', | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| ruleTester.run('require-date-day-two-digits', rule, { | ||
| valid: [ | ||
| `new Date().toLocaleDateString('en-US', { month: 'short', day: '2-digit', year: 'numeric' })`, | ||
| `new Date().toLocaleDateString({ month: 'short', day: '2-digit' })`, | ||
| // no day option | ||
| `new Date().toLocaleDateString('en-US', { month: 'short', year: 'numeric' })`, | ||
| // non-literal day value (can't assert) | ||
| `const d = 'numeric'; new Date().toLocaleDateString('en-US', { day: d })`, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: `new Date().toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' })`, | ||
| errors: [{ messageId: 'requireTwoDigit' }], | ||
| }, | ||
| { | ||
| code: `new Date().toLocaleDateString({ day: 'numeric' })`, | ||
| errors: [{ messageId: 'requireTwoDigit' }], | ||
| }, | ||
| { | ||
| code: `new Date().toLocaleDateString('en-GB', { day: 'numeric' })`, | ||
| errors: [{ messageId: 'requireTwoDigit' }], | ||
| }, | ||
| ], | ||
| }); |
39 changes: 39 additions & 0 deletions
39
packages/core/src/tests/require-test-assignee-comment.test.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,39 @@ | ||
| import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
| import tsParser from '@typescript-eslint/parser'; | ||
| import rule from '../rules/require-test-assignee-comment.js'; | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| languageOptions: { | ||
| parser: tsParser, | ||
| parserOptions: { | ||
| ecmaVersion: 2020, | ||
| sourceType: 'module', | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| ruleTester.run('require-test-assignee-comment', rule, { | ||
| valid: [ | ||
| `// Assignee: @arjun\n test.fixme('known issue', async ({ page }) => {});`, | ||
| `test('works normally', async ({ page }) => {});`, | ||
| `test.skip('skipped test without fail', async ({ page }) => {});`, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: `test.fixme('known issue', async ({ page }) => {});`, | ||
| errors: [{ messageId: 'missingAssignee' }], | ||
| }, | ||
| { | ||
| code: `// TODO: temporary bug\n test.fail('unstable feature', async ({ page }) => {});`, | ||
| errors: [{ messageId: 'missingAssignee' }], | ||
| }, | ||
| { | ||
| code: `// @owner @aniruddha\n test.fail('should retry after failure', async ({ page }) => {});`, | ||
| errors: [{ messageId: 'missingAssignee' }], | ||
| }, | ||
| { | ||
| code: `/* @responsible @omkar */\n test.fail('known bug', async ({ page }) => {});`, | ||
| errors: [{ messageId: 'missingAssignee' }], | ||
| }, | ||
| ], | ||
| }); |
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,27 @@ | ||
| # Custom ESLint Rule: require-date-day-two-digits | ||
|
|
||
| This rule enforces using `day: '2-digit'` when calling `Date.prototype.toLocaleDateString` so date strings always use two-digit day representations (e.g., `01 Mar` instead of `1 Mar`). | ||
|
|
||
| ## What it checks | ||
|
|
||
| - Calls to `toLocaleDateString` with an object literal options argument containing `day: 'numeric'` are reported. | ||
| - Cases where `day` is missing or uses `'2-digit'` are allowed. | ||
| - If the `day` value is a non-literal (variable/expression), the rule does not attempt to assert and will not report. | ||
|
|
||
| ## Example | ||
|
|
||
| ### ✅ Valid | ||
|
|
||
| ```js | ||
| new Date().toLocaleDateString('en-US', { month: 'short', day: '2-digit', year: 'numeric' }); | ||
| ``` | ||
|
|
||
| ### ❌ Invalid | ||
|
|
||
| ```js | ||
| new Date().toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' }); | ||
| ``` | ||
|
|
||
| ## Rationale | ||
|
|
||
| E2E tests should match the application's date formatting which uses two-digit days. Using `day: 'numeric'` produces single-digit days for the 1st–9th days, causing intermittent failures when tests expect two-digit formatting. | ||
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,52 @@ | ||
| # Custom ESLint Rule: require-test-assignee-comment | ||
|
|
||
| This rule ensures that Playwright `test.fixme` and `test.fail` annotations are never left orphaned by requiring an explicit assignee comment. | ||
|
|
||
| ## What it checks | ||
|
|
||
| - `test.fixme(...)` | ||
| - `test.fail(...)` | ||
|
|
||
| The rule reports when these calls are present without a nearby comment assigning ownership. | ||
|
|
||
| ## Accepted comment formats | ||
|
|
||
| Valid examples: | ||
|
|
||
| ```js | ||
| // Assignee: @username | ||
| ``` | ||
|
|
||
| Only the `@assignee` syntax is accepted by this rule. | ||
|
|
||
| ## Example | ||
|
|
||
| ### ✅ Valid | ||
|
|
||
| ```js | ||
| // Assignee: @arjun | ||
| test.fixme('known issue', async ({ page }) => {}); | ||
| ``` | ||
|
|
||
| ### ❌ Invalid | ||
|
|
||
| ```js | ||
| test.fixme('known issue', async ({ page }) => {}); | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| ```js | ||
| import fyleCore from '@fyle/eslint-plugin'; | ||
|
|
||
| export default [ | ||
| { | ||
| plugins: { | ||
| '@fyle': fyleCore, | ||
| }, | ||
| rules: { | ||
| '@fyle/require-test-assignee-comment': 'error', | ||
| }, | ||
| }, | ||
| ]; | ||
| ``` |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Configuration is missing