⚠️ Theeslint-config-liferay
package will be deprecated and this repo archived (ie. switched to read-only mode). Development will continue in the@liferay/eslint-config
package and inliferay/liferay-frontend-projects
monorepo.
An ESLint shareable config that helps enforce the Liferay Frontend Guidelines.
Preset | Extends | Description |
---|---|---|
liferay |
eslint:recommended, prettier | Base configuration, suitable for general projects |
react |
liferay |
liferay , plus rules from eslint-plugin-react and react-hooks, suitable for projects that use React |
metal |
react |
Like react , but turns off rules that cause false positives in Metal components |
portal |
react |
Default for projects inside liferay-portal itself |
$ npm install --save-dev eslint eslint-config-liferay
Once the eslint-config-liferay
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.
In liferay-portal itself we extend the liferay/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 eslint-plugin-liferay-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.
For React projects outside of liferay-portal, you can extend liferay/react
instead:
module.exports = {
extends: ['liferay/react'],
};
For legacy projects inside liferay-portal that use metal-jsx, we have a "metal" preset:
module.exports = {
extends: ['liferay/metal'],
};
Use this preset to stop ESLint from spuriously warning that variables that are used as JSX components are unused.
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.
The bundled eslint-plugin-liferay
plugin includes the following rules:
- liferay/array-is-array: Enforces (and autofixes) the use of
Array.isArray()
overinstanceof Array
. - liferay/destructure-requires: Enforces (and autofixes) that
require
statements use destructuring. - liferay/group-imports: Enforces (and autofixes)
import
andrequire
grouping. - liferay/import-extensions: Enforces consistent usage/omission of file extensions in imports.
- liferay/imports-first: Enforces that imports come first in the file.
- liferay/no-absolute-import: Enforces that imports do not use absolute paths.
- liferay/no-arrow: Bans arrow functions (for IE; not on by default).
- liferay/no-duplicate-class-names: Enforces (and autofixes) uniqueness of class names inside JSX
className
attributes. - liferay/no-duplicate-imports: Enforces at most one
import
of any given module per file. - liferay/no-dynamic-require: Enforces that
require()
calls use static arguments. - liferay/no-it-should: Enforces that
it()
descriptions start with a verb, not with "should". - liferay/no-require-and-call: Enforces that the result of a
require()
call at the top level is not immediately called. - liferay/padded-test-blocks: Enforces blank lines between test blocks (
it()
etc). - liferay/sort-class-names: Enforces (and autofixes) ordering of class names inside JSX
className
attributes. - liferay/sort-imports: Enforces (and autofixes)
import
andrequire
ordering. - liferay/sort-import-destructures: Enforces (and autofixes) ordering of destructured names in
import
statements. - liferay/trim-class-names: Enforces (and autofixes) that class names inside JSX
className
attributes do not have leading or trailing whitespace.
The bundled eslint-plugin-liferay-portal
plugin includes the following rules:
- liferay-portal/deprecation: Enforces standard formatting of
@deprecated
annotations. - liferay-portal/no-global-fetch: Prevents usage of unwrapped fetch to avoid possible issues related to security misconfiguration.
- liferay-portal/no-explicit-extend: Prevents unnecessary extensions in ESLint and Babel configuration files.
- liferay-portal/no-loader-import-specifier: Ensures that ".scss" files imported via the loader are used only for side-effects.
- liferay-portal/no-metal-plugins: Prevents usage of deprecated
metal-*
plugins and utilities. - liferay-portal/no-react-dom-render: Prevents direct usage of
ReactDOM.render
in favor of our wrapper. - liferay-portal/no-side-navigation: Guards against the use of the legacy jQuery
sideNavigation
plugin.
MIT