27.0.1 • Published 3 years ago

@liferay/eslint-config v27.0.1

Weekly downloads
254
License
MIT
Repository
github
Last release
3 years ago

@liferay/eslint-config

An ESLint shareable config that helps enforce the Liferay Frontend Guidelines.

Overview

PresetExtendsDescription
liferayeslint:recommended, prettierBase configuration, suitable for general projects
reactliferayliferay, plus rules from eslint-plugin-react and react-hooks, suitable for projects that use React
metalreactLike react, but turns off rules that cause false positives in Metal components
portalreactDefault for projects inside liferay-portal itself

Installation

$ npm install --save-dev eslint @liferay/eslint-config

Usage

Once the @liferay/eslint-config package is installed, you can use it by specifying @liferay in the extends section of your ESLint configuration.

module.exports = {
	extends: ['@liferay'],
};

This preset provides a reasonable starting point for an independent open source project.

liferay-portal

In liferay-portal itself we extend the @liferay/eslint-config/portal preset instead, which activates some rules specific to liferay-portal. This preset assumes the use of React, and also provides a set of custom rules that are described in detail in the @liferay/eslint-plugin/portal section below.

This extension is applied automatically by @liferay/npm-scripts, so you don't have to configure it explicitly.

An important disclaimer about the use of ESLint in liferay-portal

JavaScript code that appears inline inside JSP files and other templates is only lightly checked by ESLint, because JSP is an impoverished environment where we have to work with context-free snippets of text as opposed to fully-formed, valid JS modules. Our long-term strategy is to move as much code as possible out of JSP and into React components, but in the interim, please be aware that the level of safety provided by the linter inside JSP is greatly reduced.

React

For React projects outside of liferay-portal, you can extend @liferay/eslint-plugin/react instead:

module.exports = {
	extends: ['@liferay/eslint-config/react'],
};

metal-jsx

For legacy projects inside liferay-portal that use metal-jsx, we have a "metal" preset:

module.exports = {
	extends: ['@liferay/eslint-config/metal'],
};

Use this preset to stop ESLint from spuriously warning that variables that are used as JSX components are unused.

Copyright headers

The included eslint-plugin-notice plug-in can be used to enforce the use of uniform copyright headers across a project by placing a template named copyright.js in the project root (for example, see the file defining the headers used in eslint-config-liferay itself) and configuring the rule:

const path = require('path');

module.exports = {
	extends: ['@liferay'],
	rules: {
		'notice/notice': [
			'error',
			{
				templateFile: path.join(__dirname, 'copyright.js'),
			},
		],
	},
};

Explicit configuration is required in order to make overrides possible; for example:

  • top-level/
    • .eslintrc.js
    • copyright.js
    • mid-level/
      • .eslintrc.js
      • copyright.js
      • bottom-level/
        • .eslintrc.js

If we were to provide configuration by default, then if bottom-level/.eslintrc.js does an extends: ['@liferay'], then the default configuration would be considered more local than the one provided by mid-level, causing the wrong copyright.js to be used.

Base rules

Rule or presetWhere we use itNotes
eslint-config-prettier@liferayPreset that turns off ESLint rules that conflict with Prettier
eslint:recommended@liferayPreset bundled with ESLint
default-case@liferay#30
@liferay/portal/deprecation@liferay/eslint-config/portal#55
@liferay/portal/no-explicit-extend@liferay/eslint-config/portal#54
@liferay/portal/no-global-fetch@liferay/eslint-config/portal#62
@liferay/portal/no-loader-import-specifier@liferay/eslint-config/portal#122
@liferay/portal/no-metal-plugins@liferay/eslint-config/portal#61
@liferay/portal/no-react-dom-render@liferay/eslint-config/portal#71
@liferay/portal/no-side-navigation@liferay/eslint-config/portal#44
@liferay/liferay/array-is-array@liferay#139
@liferay/liferay/destructure-requires@liferay#94
@liferay/liferay/group-imports@liferay#60
@liferay/liferay/import-extensions@liferay#137
@liferay/liferay/imports-first@liferay#60
@liferay/liferay/no-abbreviations@liferay#437
@liferay/liferay/no-absolute-import@liferay#60
@liferay/liferay/no-arrow@liferay#179
@liferay/liferay/no-duplicate-class-names@liferay#108
@liferay/liferay/no-duplicate-imports@liferay#60
@liferay/liferay/no-dynamic-require@liferay#60
@liferay/liferay/no-it-should@liferay#43
@liferay/liferay/no-require-and-call@liferay#94
@liferay/liferay/padded-test-blocks@liferay#75
@liferay/liferay/sort-imports@liferay#60
@liferay/liferay/sort-import-destructures@liferay#124
@liferay/liferay/sort-class-names@liferay#108
@liferay/liferay/trim-class-names@liferay#108
no-console@liferay#79
no-eval@liferay#432
no-for-of-loops/no-for-of-loops (default: off)@liferay#30
no-only-tests/no-only-tests@liferay#22
no-restricted-globals@liferay/eslint-config/portal#109
no-return-assign@liferay#30
no-unused-expressions@liferay#19
no-unused-vars@liferay#30
notice/notice@liferay#26
object-shorthand@liferay#30
prefer-const@liferay#30
quote-props@liferay#30
radix@liferay#66
react-hooks/exhaustive-deps@liferay/eslint-config/reactRules of Hooks
react-hooks/rules-of-hooks@liferay/eslint-config/reactRules of Hooks
react/forbid-foreign-prop-types@liferay/eslint-config/react#301
react/jsx-curly-brace-presence@liferay/eslint-config/react#421
react/jsx-fragments@liferay/eslint-config/react#58
react/jsx-key@liferay/eslint-config/react#42
react/jsx-no-comment-textnodes@liferay/eslint-config/react#42
react/jsx-no-duplicate-props@liferay/eslint-config/react#42
react/jsx-no-undef@liferay/eslint-config/react#42
react/jsx-sort-props@liferay/eslint-config/react#58
react/jsx-uses-react@liferay/eslint-config/react#42
react/jsx-uses-vars@liferay/eslint-config/react, @liferay/eslint-config/metal#42, #50
react/no-children-prop@liferay/eslint-config/react#42
react/no-danger-with-children@liferay/eslint-config/react#42
react/no-direct-mutation-state@liferay/eslint-config/react#42
react/no-is-mounted@liferay/eslint-config/react#42
react/no-render-return-value@liferay/eslint-config/react#42
react/no-string-refs@liferay/eslint-config/react#42
react/no-unescaped-entities@liferay/eslint-config/react#42
react/no-unknown-property@liferay/eslint-config/react#42
react/require-render-return@liferay/eslint-config/react#42
sort-destructure-keys/sort-destructure-keys@liferay#60
sort-keys@liferay#63
spaced-comment@liferay#225

Custom rules

@liferay/liferay

The bundled @liferay/liferay plugin includes the following rules:

@liferay/portal

The bundled @liferay/portal plugin includes the following rules:

@liferay/aui

The bundled @liferay/aui plugin includes the following rules:

License

MIT