Skip to content

Commit

Permalink
Convert eslint-plugin-test-rules/index.js to Typescript
Browse files Browse the repository at this point in the history
Fixes #576

Convert `eslint-plugin-test-rules/index.js` to TypeScript.

* Rename `eslint-plugin-test-rules/index.js` to `eslint-plugin-test-rules/index.ts`.
* Update `module.exports` to `export` and add TypeScript types for ESLint rule creation.
* Modify rule creation function to use TypeScript syntax.
* Update `eslint-plugin-test-rules/package.json` to reference `index.ts` instead of `index.js`.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/w3bdesign/dfweb-v4/issues/576?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
w3bdesign committed Jan 5, 2025
1 parent 3e4b54b commit 8bef3e8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 47 deletions.
46 changes: 0 additions & 46 deletions eslint-plugin-test-rules/index.js

This file was deleted.

53 changes: 53 additions & 0 deletions eslint-plugin-test-rules/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Rule } from 'eslint';
import { RuleMeta } from '../.eslintrc-custom-rules';

interface RuleDefinition {
meta: RuleMeta;
create: (context: Rule.RuleContext) => Rule.RuleListener;
}

export const configs = {
recommended: {
plugins: ['test-rules'],
rules: {
'test-rules/arrange-act-assert': 'error'
}
}
};

export const rules: { [key: string]: RuleDefinition } = {
'arrange-act-assert': {
meta: {
type: 'suggestion',
docs: {
description: 'enforce AAA pattern in tests',
category: 'Best Practices',
recommended: true
},
schema: [] // no options
},
create(context) {
return {
CallExpression(node) {
if (node.callee.name === 'it' || node.callee.name === 'test') {
const testFn = node.arguments[1];
if (testFn && testFn.type === 'ArrowFunctionExpression') {
const comments = context.getSourceCode().getCommentsBefore(testFn.body);
const hasAAA = comments.some(comment =>
comment.value.includes('Arrange') ||
comment.value.includes('Act') ||
comment.value.includes('Assert')
);
if (!hasAAA) {
context.report({
node,
message: 'Test should follow AAA pattern with comments'
});
}
}
}
}
};
}
}
};
2 changes: 1 addition & 1 deletion eslint-plugin-test-rules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "eslint-plugin-test-rules",
"version": "1.0.0",
"description": "Custom ESLint rules for testing",
"main": "index.js",
"main": "index.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down

0 comments on commit 8bef3e8

Please sign in to comment.