5.1.2 ā€¢ Published 22 days ago

eslint-plugin-criteo v5.1.2

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
22 days ago

eslint-plugin-criteo

This ES lint plugin defines custom rules we use at Criteo and exposes a recommended set of rules.

Installation

npm install eslint eslint-plugin-criteo --save-dev

Like any library, keep it updated to make sure your project follows the latest coding recommendations.

Usage

Add criteo to the plugins section of your .eslintrc configuration file and apply:

  • plugin:criteo/recommended-angular-app if the project is an Angular application
  • plugin:criteo/recommended-angular-lib if the project is an Angular library
  • plugin:criteo/recommended-react-app if the project is a React application
  • plugin:criteo/recommended-react-lib if the project is a React library
  • plugin:criteo/recommended for general-purpose rules which are included in all the above recommended configs
  • plugin:criteo/recommended-angular-template for Angular HTML templates
{
  "plugins": ["criteo"],
  "overrides": [
    {
      "files": ["*.ts"],
      "extends": ["plugin:criteo/recommended-angular-app"], // or recommended-react-app, recommended-angular-lib...
      [...]
    },
    {
      "files": ["*.html"],
      "extends": ["plugin:criteo/recommended-angular-template"],
      [...]
    }
  ]
}

Then configure/disable the rules under the rules section, following your project's context and constraints.

{
  "rules": {
    "criteo/rule-1": ["error", { "custom-config-key": "custom-config-value" }],
    "criteo/rule-2": "off"
  }
}

Pre-commit Git hook

As a developer, it can be very frustrating to get a change rejected by QA bot because the code does not respect an ES lint rule. Even more if the failing rule has an auto-fix! To avoid it, you can configure your project to run ES lint automatically on pre-commit. Because it will target only staged files, it is quite fast!

  1. Define the pre-commit hook script .git-hook-lint-staged at the root of the first/main UI project of the repository. It will make it available to all developers since Git hooks cannot be pushed directly.
#!/bin/sh
cd project1 && npx lint-staged \
  && cd ../project2 && npx lint-staged # If the repository hosts several projects, add each of them
  1. Also define the postinstall NPM script in the scripts section of package.json:
"scripts": {
  "postinstall": "npx shx cp .git-hook-lint-staged ../.git/hooks/lint-staged"
}

It will enable the hook automatically after npm install.

  1. Still in the same project, install shx to make the copy command work on all environments: npm install --save-dev shx

  2. Install lint-staged in each UI project of the repository: npm install --save-dev lint-staged

  3. In each project, also define its configuration by declaring in package.json:

"lint-staged": {
  "**.{ts,component.html}": "eslint --fix"
}

You can also declare "*.{ts,component.html,json,scss}": "prettier --write" if you use Prettier.

šŸ™Œ

Criteo rules

cypress-no-force

Cypress: disallow using of 'force: true' option.

Why?

The Cypress ESLint plugin provides this rule, with the following explanation:

Using force: true on inputs appears to be confusing rather than helpful. It usually silences the actual problem instead of providing a way to overcome it. See Cypress Core Concepts.

Unfortunately, the Cypress plugin's version only works when calling the function like cy.get(...).click({ force: true }), i.e. with cy in the same line. But in our code we often use helper functions, so the rule does not detect our usages. This new version of the rule leverages TypeScript to work out if the method is called on a Cypress.Chainable object.

filename

Ensure file names are valid and consistent.

Config:

  • pattern: allowed name pattern (default: /^[a-z0-9\.\-]+$/)

Why?

Improve the file tree readability. For example, a component called MyButtonComponent should be defined under the path */my-button/my-button.component.{html,ts,css,scss,...}.

filename-match-export

Ensure file names reflect the named exported members.

Config:

  • removeFromFilename: text removed from file names before check (default: [])

Why?

The name of the file should describe its content for readability.

independent-folders

Ensure feature folders are independent by preventing imports between each other. Features folders imports are also forbidden from the shared modules.

Config:

  • basePath: base path (from the project) applied to all featureFolders and sharedFolders (default: ./)
  • featureFolders: array of the feature folders paths that should remain independent (default: [])
  • sharedFolders: array of the shared folders paths that cannot use any feature folder (default: [])

Sample :

'criteo/independent-folders': [
  'error',
  {
    basePath: './src/app/',
    featureFolders: ['broad-targeting-audience', 'similar-audience'],
    sharedFolders: ['shared'],
  },
],

Why?

To avoid messy and circular imports:

  • Feature folders should be independent or use each others using the public API.
  • Shared folders should not rely on feature folders.

ngx-component-display

Ensure Angular components have a display property set.

Config:

  • ignore: classes whose names match this regular expression (defined as string) will be ignored (default: '^.*(?:Dialog|Modal)Component$')
  • propertyName: name of the display property (default: 'cdsDisplay')

Why?

  • By default, custom components are displayed as inline by the browser which is rarely what we expect. For example, it makes impossible to define a width or margins on them. E.g. <my-component class="w-100 cds-mb-3"></my-component> would have no effect.
  • The workaround of wrapping them in <div></div> should be avoided to not make the DOM and the bundle file heavier.

ngx-no-styles-in-component

Forbid using styles, styleUrl or styleUrls in a component's metadata: favour the Criteo design system instead.

Why?

To maintain a coherent user experience, we aim to use the classes and components from the shared component library as much as possible, as opposed to custom styles.

no-indexed-access-on-enums

Forbid accessing an enum thanks to an index: Enum0.

Why?

The names of the enum values should not be "data" in the context of the application at runtime. This brings a lack of clarity, a lack of readability, maintenance issues, and can be error-prone.

no-ngxs-select-decorator

Forbid using the NGXS @Select() decorator: @ViewSelectSnapshot() should be preferred.

Why?

@Select() exposes an Observable that must be subscribed using the async pipe in the template. It makes it verbose and creates one subscription per usage. However, the @ViewSelectSnapshot() exposes the raw value directly and refreshes the view on every change ; making it more concise and easier to use.

no-null-undefined-comparison

Forbid comparisons with null and undefined.

Why?

The difference between null and undefined is specific to Javascript and can be tricky for juniors/backend developers. Most of the time, we don't need to distinguish these 2 values, so using isNil from lodash is safer.

ngxs-selector-array-length

Ensure that when using the @Selector() decorator, the number of selectors passed in matches the number of arguments passed to the selector function.

Why?

It can be tricky to pin down the source of an error when using the @Selector() decorator. While this rule can't make sure you put all the parameters in the right order, it does avoid the most obvious mistakes.

no-spreading-accumulators

Ensure that reducers do not mistakenly have O(n^2) complexity.

Why?

When using .reduce(), it may be tempting to do something like this for the sake of brevity:

const mappedById = myArray.reduce((acc, entity) => ({ ...acc, [entity.id]: entity }), {});

However, spreading the accumulator at every iteration results in an operation with O(n^2) time & spatial complexity.

This rule helps ensure that .reduce() is an O(n) operation. For example:

const mappedById = myArray.reduce((acc, entity) => { acc[entity.id] = entity; return acc; }, {});

no-todo-without-ticket

Ensure that comments with TODO or FIXME specify a JIRA ticket in which the work will be completed.

Why?

Commits with TODO comments indicating that a portion of functionality has yet to be implemented are easy to overlook later on. This rule encourages all outstanding work to be tracked by an external ticket as well as in code comments.

prefer-readonly-decorators

Ensure that appropriate decorated properties are readonly.

Config:

  • decorators: array of decorator names that should always be readonly (default: ['Output', 'ViewSelectSnapshot'])

Why?

Some decorated properties should be readonly because their decorator handles the value assignment and should never be assigned by hand (ex: @ViewSelectSnapshot()), or their value should never change (ex: @Output()) to prevent errors.

until-destroy

Ensure @UntilDestroy() decorator is not forgotten, nor applied when it is not necessary.

Why?

@UntilDestroy() defines mandatory properties in decorated class to make the untilDestroyed operator work. Nevertheless, the decorator should not be applied when it is not necessary because it would inject useless code.

External rules

In addition to the rules defined above, we have chosen some rules from external libraries which we activate by default. Some of these have custom config to better address our specific use cases.

LibraryRule nameDocumentationApplies to Angular applicationsApplies to Angular librariesApplies to React applicationsApplies to React library
AngularAll recommended rules from @angular-eslint/templatehttps://github.com/angular-eslint/angular-eslint/tree/master/packages/eslint-plugin-template/docs/rulesāœ…āœ…
AngularAll recommended rules from @angular-eslinthttps://github.com/angular-eslint/angular-eslint/tree/master/packages/eslint-plugin/docs/rulesāœ…āœ…
Angular@angular-eslint/no-lifecycle-callhttps://github.com/angular-eslint/angular-eslint/blob/master/packages/eslint-plugin/docs/rules/no-lifecycle-call.mdāœ…āœ…
Angular@angular-eslint/no-pipe-impurehttps://github.com/angular-eslint/angular-eslint/blob/master/packages/eslint-plugin/docs/rules/no-pipe-impure.mdāœ…āœ…
Angular@angular-eslint/prefer-on-push-component-change-detectionhttps://github.com/angular-eslint/angular-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-on-push-component-change-detection.mdāœ…āœ…
Angular@angular-eslint/relative-url-prefixhttps://github.com/angular-eslint/angular-eslint/blob/master/packages/eslint-plugin/docs/rules/relative-url-prefix.mdāœ…āœ…
Angular@angular-eslint/template/alt-texthttps://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin-template/docs/rules/alt-text.mdāœ…āœ…
Angular@angular-eslint/template/elements-contenthttps://github.com/angular-eslint/angular-eslint/blob/master/packages/eslint-plugin-template/docs/rules/elements-content.mdāœ…āœ…
Angular@angular-eslint/template/label-has-associated-controlhttps://github.com/angular-eslint/angular-eslint/blob/master/packages/eslint-plugin-template/docs/rules/label-has-associated-control.mdāœ…āœ…
Angular@angular-eslint/template/no-call-expressionhttps://github.com/angular-eslint/angular-eslint/blob/master/packages/eslint-plugin-template/docs/rules/no-call-expression.mdāœ…āœ…
Angular@angular-eslint/template/no-duplicate-attributeshttps://github.com/angular-eslint/angular-eslint/blob/master/packages/eslint-plugin-template/docs/rules/no-duplicate-attributes.mdāœ…āœ…
Angular@angular-eslint/template/no-positive-tabindexhttps://github.com/angular-eslint/angular-eslint/blob/master/packages/eslint-plugin-template/docs/rules/no-positive-tabindex.mdāœ…āœ…
Angular@angular-eslint/template/use-track-by-functionhttps://github.com/angular-eslint/angular-eslint/blob/master/packages/eslint-plugin-template/docs/rules/use-track-by-function.mdāœ…āœ…
Angular@angular-eslint/template/valid-ariahttps://github.com/angular-eslint/angular-eslint/blob/master/packages/eslint-plugin-template/docs/rules/valid-aria.mdāœ…āœ…
Angular@angular-eslint/use-component-view-encapsulationhttps://github.com/angular-eslint/angular-eslint/blob/master/packages/eslint-plugin/docs/rules/use-component-view-encapsulation.mdāœ…āœ…
CypressAll recommended rules from cypresshttps://github.com/cypress-io/eslint-plugin-cypress#rulesāœ…āœ…
ESLintAll recommended ESLint ruleshttps://eslint.org/docs/rules/āœ…āœ…āœ…āœ…
ReactAll recommended rules from reacthttps://github.com/jsx-eslint/eslint-plugin-reactāœ…āœ…
Reactreact/hook-use-statehttps://github.com/jsx-eslint/eslint-plugin-reactāœ…āœ…
Reactreact/jsx-boolean-valuehttps://github.com/jsx-eslint/eslint-plugin-reactāœ…āœ…
Reactreact/jsx-closing-bracket-locationhttps://github.com/jsx-eslint/eslint-plugin-reactāœ…āœ…
Reactreact/jsx-key.https://github.com/jsx-eslint/eslint-plugin-reactāœ…āœ…
Reactreact/jsx-no-useless-fragmenthttps://github.com/jsx-eslint/eslint-plugin-reactāœ…āœ…
Reactreact/no-arrow-function-lifecyclehttps://github.com/jsx-eslint/eslint-plugin-reactāœ…āœ…
Reactreact/no-invalid-html-attributehttps://github.com/jsx-eslint/eslint-plugin-reactāœ…āœ…
Reactreact/no-unused-statehttps://github.com/jsx-eslint/eslint-plugin-reactāœ…āœ…
Reactreact/self-closing-comphttps://github.com/jsx-eslint/eslint-plugin-reactāœ…āœ…
React HooksAll ruleshttps://github.com/lydell/eslint-plugin-simple-import-sortāœ…āœ…
RxJSAll recommended rules from rxjshttps://github.com/cartant/eslint-plugin-rxjs#rulesāœ…āœ…
RxJSrxjs-angular/prefer-takeuntil (with custom config)https://github.com/cartant/eslint-plugin-rxjs-angular/blob/main/docs/rules/prefer-takeuntil.mdāœ…āœ…
RxJSrxjs/finnish (with custom config)https://github.com/cartant/eslint-plugin-rxjs/blob/main/docs/rules/finnish.mdāœ…āœ…
RxJSrxjs/no-unsafe-takeuntil (with custom config)https://github.com/cartant/eslint-plugin-rxjs/blob/main/docs/rules/no-unsafe-takeuntil.mdāœ…āœ…
TypeScriptAll recommended rules from @typescript-eslinthttps://github.com/typescript-eslint/typescript-eslint/tree/main/packages/eslint-plugin#supported-rulesāœ…āœ…āœ…āœ…
TypeScriptno-empty-functionhttps://typescript-eslint.io/rules/no-empty-function/āœ…āœ…āœ…āœ…
TypeScriptno-restricted-imports (with custom config)https://typescript-eslint.io/rules/no-restricted-imports/āœ…āœ…
deprecationdeprecationhttps://github.com/gund/eslint-plugin-deprecation?tab=readme-ov-file#disallow-usage-of-deprecated-apis-deprecationdeprecationāœ…āœ…āœ…āœ…
eslint-commentsAll recommended rules from eslint-commentshttps://mysticatea.github.io/eslint-plugin-eslint-comments/rules/āœ…āœ…āœ…āœ…
eslint-commentseslint-comments/disable-enable-pair (custom config to allow whole-file disables)https://mysticatea.github.io/eslint-plugin-eslint-comments/rules/disable-enable-pair.htmlāœ…āœ…āœ…āœ…
eslint-commentseslint-comments/require-descriptionhttps://mysticatea.github.io/eslint-plugin-eslint-comments/rules/require-description.htmlāœ…āœ…āœ…āœ…
no-only-testsno-only-tests/no-only-testshttps://github.com/levibuzolic/eslint-plugin-no-only-tests#usageāœ…āœ…āœ…āœ…
simple-import-sortsimple-import-sort/exportshttps://github.com/lydell/eslint-plugin-simple-import-sortāœ…āœ…āœ…āœ…
simple-import-sortsimple-import-sort/imports (with custom config)https://github.com/lydell/eslint-plugin-simple-import-sortāœ…āœ…āœ…āœ…
5.1.2

22 days ago

5.1.1

22 days ago

5.1.0

23 days ago

5.0.0

1 month ago

5.0.0-beta.0

1 month ago

4.12.0

5 months ago

4.13.0

5 months ago

4.11.0

10 months ago

4.11.1

10 months ago

4.10.0

12 months ago

4.9.0

1 year ago

4.8.0

1 year ago

4.7.0

1 year ago

4.6.0

1 year ago

4.5.0

1 year ago

4.4.0

1 year ago

4.3.0

1 year ago

4.2.0

1 year ago

4.1.0

1 year ago

4.0.0

2 years ago

3.2.1

2 years ago

3.2.0

2 years ago

3.1.0

2 years ago

3.0.0

2 years ago

2.4.3

2 years ago

2.4.2

2 years ago

2.4.1

2 years ago

2.4.0

2 years ago

2.3.0

2 years ago

2.2.0

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

1.0.0

2 years ago