The scope of this repo is to confirm custom ESLint rules work as expected. Errors were reported for async functions without try-catch blocks and .onSnapshot calls without error handling. This is a Next.js project bootstrapped with create-next-app.
First, isntall:
npm installThen, link the local rules plugins:
To make ESLint recognize the local plugin:
- in the
eslint-plugin-rulesdirectory runnpm link. - in the project's root directory
npm link eslint-plugin-rules.
This approach is not suitable for deployment or for ensuring that your custom ESLint setup works in environments outside your local development machine. It's for demo purposes that the rules work based on the test cases in eslintTestCases.tsx. ESLint expects npm packages.
Finally, build:
npm run buildIf linking was done appropriately you should see in your Terminal:
Failed to compile.
./src/eslintTestCases.tsx
12:1 Error: Async function's body is empty or malformed. rules/async-function-try-catch
17:1 Error: Async function must be wrapped in a try-catch block. rules/async-function-try-catch
36:1 Error: .onSnapshot should have an error handling callback. rules/on-snapshot-error-handlingThis signifies that custom Val rules are a success and rules are recognized and enforced in the build process via ESLint. Alternatively you can run npx eslint .
-
Created the Plugin Directory: Started by creating a directory named
eslint-plugin-rulesat the root of the project. This directory is where the custom ESLint rules and the plugin's configuration reside. -
Develop Custom Rules: Inside the
eslint-plugin-rulesdirectory, created js files for each custom rule (async-function-try-catch.jsandon-snapshot-error-handling.js). These files define the behavior of the custom rules using ESLint's rule API as per Val's recommendations. -
Configure the Plugin: In the
eslint-plugin-rulesdirectory, added anindex.jsandpackage.json. Theindex.jsfile exports the plugin configuration, including the custom rules and thepackage.jsonis created to trick ESLint config expecting NPM packages.
In a production environtment these are expected to be npm packages
- Link the Plugin Locally: To make ESLint recognize the local plugin, i had to use
npm linkto create a global symlink for theeslint-plugin-rulesdirectory. Then, i linked this symlink to the project by runningnpm link eslint-plugin-rulesin the project's root directory. This approach is not suitable for deployment or for ensuring that your custom ESLint setup works in environments outside your local development machine.
Using npm link to create a global symlink for the eslint-plugin-rules directory and linking it to our project works only for local development and it's made so for demonstration purposes of the test cases and eslint rules. However, this approach is not suitable for deployment or for ensuring that custom ESLint setup works in environments outside local development machine. The npm link symlinks are not preserved during the deployment process, and the deployment environment won't have access to local filesystem where the symlink points.
In a production environtment the rules are expected to be npm packages. This Repo is for demo'ing that the rules work.
-
Install ESLint and Related Packages: Made sure TypeScript ESLint parser (
@typescript-eslint/parser), and the TypeScript ESLint plugin (@typescript-eslint/eslint-plugin) were installed in the project. -
Update ESLint Configuration: Modified the project's
.eslintrc.jsonto include the custom plugin and its rules:{ "extends": ["next/core-web-vitals", "plugin:@typescript-eslint/recommended"], "rules": { "rules/async-function-try-catch": "error", "rules/on-snapshot-error-handling": "error" }, "plugins": [ "@typescript-eslint", "rules" ] }Here,
"rules"refers to the local ESLint plugins, and we specify our custom rules under the"rules"key.
-
Writing Test Cases: In (
eslintTestCases.tsx) you can find code examples specifically designed to trigger the custom ESLint rules. This included async functions both with and without try-catch blocks and.onSnapshotmethod calls with and without error handling. -
Running ESLint: With the test cases in place, we ran ESLint on the project (
npx eslint .). This step confirms that our custom rules were correctly identifying the intended patterns and that the ESLint setup was properly configured to flag violations of these rules.
The output from running ESLint confirmed that the custom rules worked as expected. Errors were reported for async functions without try-catch blocks and .onSnapshot calls without error handling.
To learn more about Next.js, take a look at the following resources:
- Next.js Documentation - learn about Next.js features and API.
- Learn Next.js - an interactive Next.js tutorial.
You can check out the Next.js GitHub repository - your feedback and contributions are welcome!