Salesforce ESLint Configuration for TypeScript
eslint-config-salesforce-typescript is a comprehensive ESLint configuration designed specifically for TypeScript projects within the Salesforce ecosystem. Currently at version 4.0.1, this package provides a curated set of ESLint rules, including those from `@typescript-eslint/eslint-plugin` and `@typescript-eslint/parser`, ensuring consistent code quality and adherence to Salesforce's coding standards for TypeScript. It offers a structured approach to linting, integrating best practices for TypeScript development, and helps prevent common issues. The package receives regular maintenance updates, often weekly or bi-weekly as evidenced by the release log, primarily focused on dependency bumps and minor rule adjustments. It differentiates itself by providing a ready-to-use setup tailored for Salesforce projects, abstracting away the complexity of configuring multiple ESLint plugins for TypeScript. Developers can extend this configuration in their `.eslintrc` files, simplifying the setup process for new and existing projects.
Common errors
-
Parsing error: 'parserOptions.project' has been set for @typescript-eslint/parser. The file does not match your project config.
cause The `tsconfig.json` file specified in `parserOptions.project` either does not exist at the given path or does not include the files ESLint is trying to lint.fixVerify that `parserOptions.project` in your `.eslintrc` points to the correct `tsconfig.json` path and that your `tsconfig.json`'s `include` array covers the files being linted. -
ESLint couldn't find the plugin 'eslint-plugin-import'.
cause A required peer dependency, `eslint-plugin-import`, is not installed in the project.fixInstall the missing plugin: `yarn add eslint-plugin-import` or `npm install eslint-plugin-import`. -
TypeError: ESLint is not a constructor
cause This error typically occurs when an older version of ESLint is trying to load a configuration or plugin that expects a newer ESLint API, or due to incorrect CommonJS `require()` syntax in an ES Modules environment.fixEnsure your ESLint installation is up-to-date (`yarn add eslint@latest` or `npm install eslint@latest`). If using CommonJS, verify your `.eslintrc.js` uses `module.exports = { ... }` correctly. If in an ESM project, ensure your `.eslintrc.js` file name and contents are compatible with ESM, or consider using `.eslintrc.cjs`.
Warnings
- breaking Major version 4.x introduces potential breaking changes compared to 3.x, adhering to semantic versioning. Review the official changelog for specific rule changes or removals when upgrading.
- gotcha This configuration requires several peer dependencies (ESLint, various plugins, and TypeScript parser) to be installed explicitly in your project. Failing to install them will result in ESLint errors indicating missing modules.
- gotcha For TypeScript type-aware rules to function correctly, you must specify `parserOptions.project` in your `.eslintrc` file, pointing to your `tsconfig.json`. Incorrect paths or missing `tsconfig.json` can lead to errors or rules not being applied.
Install
-
npm install eslint-config-salesforce-typescript -
yarn add eslint-config-salesforce-typescript -
pnpm add eslint-config-salesforce-typescript
Imports
- ESLint Configuration Extension
import { config } from 'eslint-config-salesforce-typescript';module.exports = { extends: ['eslint-config-salesforce-typescript'] }; - ESLint Rule Override
module.exports = { extends: ['eslint-config-salesforce-typescript'], rules: { 'indent': ['error', 4], '@typescript-eslint/no-explicit-any': 'off' } }; - TypeScript Parser Options
module.exports = { extends: ['eslint-config-salesforce-typescript'], parserOptions: { project: './tsconfig.json' } };
Quickstart
yarn add eslint-config-salesforce-typescript eslint eslint-plugin-import eslint-plugin-prettier eslint-plugin-jsdoc @typescript-eslint/eslint-plugin @typescript-eslint/parser
// Create a .eslintrc.js file in your project root:
// .eslintrc.js
module.exports = {
extends: [
'eslint-config-salesforce-typescript'
],
// If you are using type-aware rules, uncomment and adjust the parserOptions
// parserOptions: {
// project: './tsconfig.json'
// }
// Add any project-specific overrides here, e.g., to disable a rule:
// rules: {
// '@typescript-eslint/no-explicit-any': 'off'
// }
};
// To run ESLint:
// npx eslint src/