UNPKG

@carto/ps-style-guide

Version:

CARTO's engineering style guide for Professional Services

184 lines (136 loc) 5.83 kB
# The CARTO PS Style Guide <a aria-label="NPM version" href="https://www.npmjs.com/package/@carto/ps-style-guide"> <img alt="" src="https://img.shields.io/npm/v/@carto/ps-style-guide.svg?style=flat-square&labelColor=000000"> </a> <a aria-label="License" href="https://github.com/CartoDB/carto-ps-lib/blob/canary/LICENSE.md"> <img alt="" src="https://img.shields.io/npm/l/@carto/ps-style-guide.svg?style=flat-square&labelColor=000000"> </a> <a aria-label="CI status" href="https://github.com/carto/ps-style-guide/actions/workflows/pull-request.yml?query=event%3Apush+branch%3Amain"> <img alt="" src="https://img.shields.io/github/actions/workflow/status/carto/ps-style-guide/pull-request.yml?event=push&branch=main&style=flat-square&labelColor=000000"> </a> ## Introduction This package is the home of CARTO's PS style guide, which includes configs for popular linting and styling tools. The following configs are available, and are designed to be used together. - [Prettier](#prettier) - [ESLint](#eslint) - [TypeScript](#typescript) ## Contributing Please read our [contributing](https://github.com/carto/carto-ps-lib/blob/main/CONTRIBUTING.md) guide before creating a pull request. ## Installation All of our configs are contained in one package, `@carto/ps-style-guide`. To install: ```sh # If you use npm npm i --save-dev @carto/ps-style-guide # If you use pnpm pnpm add --save-dev @carto/ps-style-guide ``` Some of our ESLint configs require peer dependencies. We'll note those alongside the available configs in the [ESLint](#eslint) section. ## Prettier > Note: Prettier is a peer-dependency of this package, and should be installed > at the root of your project. > > See: https://prettier.io/docs/en/install.html To use the shared Prettier config, set the following in `package.json`. ```json { "prettier": "@carto/ps-style-guide/prettier" } ``` ## ESLint > Note: ESLint is a peer-dependency of this package, and should be installed > at the root of your project. > > See: https://eslint.org/docs/user-guide/getting-started#installation-and-usage This ESLint config is designed to be composable. The following base configs are available. You can use one or both of these configs, but they should always be first in `extends`: - `@carto/ps-style-guide/eslint/browser` - `@carto/ps-style-guide/eslint/node` Note that you can scope configs, so that configs only target specific files. For more information, see: [Scoped configuration with `overrides`](#scoped-configuration-with-overrides). The following additional configs are available: - `@carto/ps-style-guide/eslint/react` - `@carto/ps-style-guide/eslint/typescript` (requires `typescript` to be installed and [additional configuration](#configuring-eslint-for-typescript)) > You'll need to use `require.resolve` to provide ESLint with absolute paths, > due to an issue around ESLint config resolution (see > [eslint/eslint#9188](https://github.com/eslint/eslint/issues/9188)). ### Configuring ESLint for TypeScript Some of the rules enabled in the TypeScript config require additional type information, you'll need to provide the path to your `tsconfig.json`. For more information, see: https://typescript-eslint.io/docs/linting/type-linting ```js const { resolve } = require('node:path') const project = resolve(__dirname, 'tsconfig.json') module.exports = { root: true, extends: [ require.resolve('@carto/ps-style-guide/eslint/react'), require.resolve('@carto/ps-style-guide/eslint/typescript'), ], parserOptions: { project, }, settings: { 'import/resolver': { typescript: { project, }, }, }, } ``` ### Configuring custom components for `jsx-a11y` It's common practice for React apps to have shared components like `Button`, which wrap native elements. You can pass this information along to `jsx-a11y` via the `components` setting. The below list is not exhaustive. ```js module.exports = { root: true, extends: [require.resolve('@carto/ps-style-guide/eslint/react')], settings: { 'jsx-a11y': { components: { Article: 'article', Button: 'button', Image: 'img', Input: 'input', Link: 'a', Video: 'video', }, }, }, } ``` ### Scoped configuration with `overrides` ESLint configs can be scoped to include/exclude specific paths. This ensures that rules don't "leak" into places where those rules don't apply. #### A note on file extensions By default, all TypeScript rules are scoped to files ending with `.ts` and `.tsx`. However, when using overrides, file extensions must be included or ESLint will only include `.js` files. ```js module.exports = { overrides: [ { files: [`directory/**/*.[jt]s?(x)`], rules: { 'my-rule': 'off' } }, ], } ``` ## TypeScript This style guide provides multiple TypeScript configs. These configs correlate to the LTS Node.js versions, providing the appropriate `lib`, `module`, `target`, and `moduleResolution` settings for each version. The following configs are available for **Node >=20**: | Environment | TypeScript Config | | ----------- | ------------------------------------------ | | Node | `@carto/ps-style-guide/typescript/node` | | Library | `@carto/ps-style-guide/typescript/library` | | React | `@carto/ps-style-guide/typescript/react` | To use the shared TypeScript config, set the following in `tsconfig.json` for CARTO's PS react project. ```json { "extends": "@carto/ps-style-guide/typescript/react" } ``` The base TypeScript config is also available as [`@carto/ps-style-guide/typescript/_base`](./typescript/_base.json) which only specifies a set of general rules. You should inherit from this file when setting custom `lib`, `module`, `target`, and `moduleResolution` settings.