ESLint shareable config for the Liferay JavaScript style guide.
$ 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'],
};
For React projects, 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.
In liferay-portal itself we extend the liferay/portal
preset instead, which activates some additional rules specific to liferay-portal. See the eslint-plugin-liferay-portal
section below for more details.
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 not currently checked by ESLint. Our long-term strategy is to move as much code as possible out of JSP and into React components, but in the interim, our only option is to rely on liferay-portal's existing source formatter for those files.
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).
The bundled eslint-plugin-liferay
plugin includes the following rules:
- liferay/destructure-requires: Enforces (and autofixes) that
require
statements use destructuring. - liferay/group-imports: Enforces (and autofixes)
import
andrequire
grouping. - 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-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-imports: Enforces (and autofixes)
import
andrequire
ordering.
The bundled eslint-plugin-liferay-portal
plugin includes the following rules:
- 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
extends: ["liferay/portal"]
configuration. - 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