Skip to content

Commit

Permalink
refactor: migrate eslint-config-liferay to @liferay/eslint-config
Browse files Browse the repository at this point in the history
We like named scopes because they give us:

- Easier discovery (eg. at-a-glance view of all of our packages).
- Easier management (eg. access control).
- Clear way of distinguishing packages as official "built-by-Liferay" as
  opposed to just "has-a-liferay-prefix-but-could-be-built-by-anyone".
- Namespace reserved just for us, so no name collisions to worry about.

This is probably about the most difficult kind of migration to named
scopes that you could do, due to the way ESLint conventions apply. Other
packages should be way easier to migrate.

Summary of what happened here:

-   Rename eslint-config-liferay -> eslint-config (ie. any package at
    "projects/*" or "projects/*/packages/*", we can assume it's in the
    `@liferay` scope and omit the prefix).
-   Tweak default tag prefix in `.yarnrc` to match.
-   Update docs (mostly names, some links etc; note that the links to
    issues and pull requests stay the same because those will be
    ever-green, even when we archive the old repo).
-   Plug-ins move from/to:
    -   plugins/eslint-plugin-liferay -> plugins/liferay
    -   plugins/eslint-plugin-liferay-portal -> plugins/portal
-   Using the config via `extends`, before and after (this is dictated by
    ESLint conventions):
    -   liferay -> `@liferay`
    -   liferay/metal -> `@liferay/eslint-config/metal`
    -   liferay/portal -> `@liferay/eslint-config/portal`
    -   liferay/react -> `@liferay/eslint-config/react`
-   Referring to rules, before and after:
    -   liferay/array-is-array -> `@liferay/liferay/array-is-array`
    -   liferay-portal/no-global-fetch -> `@liferay/portal/no-global-fetch`
-   See also references to named scopes in these ESLint docs:
    -   https://eslint.org/docs/2.0.0/developer-guide/working-with-plugins
    -   https://eslint.org/docs/developer-guide/shareable-configs
-   Had to update our `local()` hack which allows us to embed plugins
    inside a config.
-   Fun fact: I had to update an in-house lint suppression (the only one
    in this project) to refer to the new rule name. Related, had to
    reorder some rules due to change from `liferay` to `@liferay`.

Test plan:

1.  Create a scratch directory and `yarn init -y` inside it.

2.  `yarn add path/to/this/project eslint`

3.  Set up some .eslintrc.js and add these lines:

    ```
    "extends": ["@liferay/eslint-config/portal"],
    "rules": {
        "@liferay/portal/no-global-fetch": "warn"
    }
    ```

4.  Add some problems in index.js and run `yarn run eslint index.js`;
    see expected error output:

    ```
    .../index.js
      1:10  error    'test' is defined but never used                                                                                                                                                    no-unused-vars
      3:1   warning  Direct usage of the `fetch` API is discouraged in favour of our wrapped version that takes care of the necessary security configuration; import fetch from frontend-js-web instead  @liferay/portal/no-global-fetch
      5:1   error    import of "crap" must come before other statements                                                                                                                                  @liferay/liferay/imports-first

    ✖ 3 problems (2 errors, 1 warning)
    ```
  • Loading branch information
wincent committed Sep 23, 2020
1 parent df3b537 commit b0174d9
Show file tree
Hide file tree
Showing 107 changed files with 271 additions and 266 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
This monorepo contains multiple projects, each of which specifies its license in:

- The project `package.json` file (for example, see [`projects/eslint-config-liferay/package.json`](./projects/eslint-config-liferay/package.json)); and:
- The project `package.json` file (for example, see [`projects/eslint-config/package.json`](./projects/eslint-config/package.json)); and:

- In header comments in each source code file containing the [SPDX license](https://spdx.org/licenses/) information; for example:

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ This is an experimental exploration of the ideas proposed in [liferay-frontend-g

## Projects

- [eslint-config-liferay](./projects/eslint-config-liferay)
- [@liferay/eslint-config](./projects/eslint-config)
203 changes: 0 additions & 203 deletions projects/eslint-config-liferay/README.md

This file was deleted.

30 changes: 0 additions & 30 deletions projects/eslint-config-liferay/portal.js

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Make `yarn version` produce the right commit message and tag for this package.
version-tag-prefix "v"
version-tag-prefix "eslint-config/v"
version-git-message "chore: prepare v%s release"
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@

6. Update the release notes.

Take the new section from the top of the CHANGELOG and add it to [the release page](https://github.com/liferay/eslint-config-liferay/releases) on GitHub.
Take the new section from the top of the CHANGELOG and add it to [the release page](https://github.com/liferay/liferay-frontend-projects/releases) on GitHub.

7. Sanity check [the eslint-config-liferay page](https://www.npmjs.com/package/eslint-config-liferay) on npmjs.com
7. Sanity check [the @liferay/eslint-config page](https://www.npmjs.com/package/@liferay/eslint-config) on npmjs.com

Specifically, you should see the version you just released under the "Versions" tab on that page.
203 changes: 203 additions & 0 deletions projects/eslint-config/README.md

Large diffs are not rendered by default.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@ const config = {
sourceType: 'module',
},
plugins: [
local('liferay'),
local('@liferay/liferay'),
'no-for-of-loops',
'no-only-tests',
'notice',
'sort-destructure-keys',
],
rules: {
'@liferay/liferay/array-is-array': 'error',
'@liferay/liferay/destructure-requires': 'error',
'@liferay/liferay/group-imports': 'error',
'@liferay/liferay/import-extensions': 'error',
'@liferay/liferay/imports-first': 'error',
'@liferay/liferay/no-absolute-import': 'error',
'@liferay/liferay/no-duplicate-imports': 'error',
'@liferay/liferay/no-dynamic-require': 'error',
'@liferay/liferay/no-it-should': 'error',
'@liferay/liferay/no-require-and-call': 'error',
'@liferay/liferay/padded-test-blocks': 'error',
'@liferay/liferay/sort-import-destructures': 'error',
'@liferay/liferay/sort-imports': 'error',
curly: 'error',
'default-case': 'error',
'liferay/array-is-array': 'error',
'liferay/destructure-requires': 'error',
'liferay/group-imports': 'error',
'liferay/import-extensions': 'error',
'liferay/imports-first': 'error',
'liferay/no-absolute-import': 'error',
'liferay/no-duplicate-imports': 'error',
'liferay/no-dynamic-require': 'error',
'liferay/no-it-should': 'error',
'liferay/no-require-and-call': 'error',
'liferay/padded-test-blocks': 'error',
'liferay/sort-import-destructures': 'error',
'liferay/sort-imports': 'error',
'lines-around-comment': [
'error',
{
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "eslint-config-liferay",
"name": "@liferay/eslint-config",
"version": "21.1.0",
"description": "ESLint shareable config for the Liferay JavaScript Style",
"main": "index.js",
Expand All @@ -21,8 +21,9 @@
"liferay"
],
"repository": {
"directory": "projects/eslint-config",
"type": "git",
"url": "https://github.com/liferay/eslint-config-liferay.git"
"url": "https://github.com/liferay/liferay-frontend-projects.git"
},
"author": "Liferay",
"license": "MIT",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

const badFormat =
'@deprecated requires format: https://github.com/liferay/eslint-config-liferay/blob/master/plugins/eslint-plugin-liferay-portal/docs/rules/deprecation.md';
'@deprecated requires format: https://github.com/liferay/liferay-frontend-projects/blob/master/projects/eslint-config/plugins/portal/docs/rules/deprecation.md';

const blockCommentsOnly =
'@deprecated annotations should only appear in block (/**/) comments';
Expand Down
30 changes: 30 additions & 0 deletions projects/eslint-config/portal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* SPDX-FileCopyrightText: © 2017 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: MIT
*/

'use strict';

const local = require('./utils/local');

const config = {
extends: [require.resolve('./react')],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
plugins: [local('@liferay/portal')],
rules: {
'@liferay/portal/deprecation': 'error',
'@liferay/portal/no-explicit-extend': 'error',
'@liferay/portal/no-global-fetch': 'error',
'@liferay/portal/no-loader-import-specifier': 'error',
'@liferay/portal/no-metal-plugins': 'error',
'@liferay/portal/no-react-dom-render': 'error',
'@liferay/portal/no-side-navigation': 'error',
'no-restricted-globals': ['error', 'event'],
},
};

module.exports = config;
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const config = {
},
plugins: ['react', 'react-hooks'],
rules: {
'liferay/no-duplicate-class-names': 'error',
'liferay/sort-class-names': 'error',
'liferay/trim-class-names': 'error',
'@liferay/liferay/no-duplicate-class-names': 'error',
'@liferay/liferay/sort-class-names': 'error',
'@liferay/liferay/trim-class-names': 'error',

/**
* @see https://github.com/yannickcr/eslint-plugin-react
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const path = require('path');
const LINT_GLOBS = ['**/*.js', '.*.js'];

/**
* In order for eslint-config-liferay to be applied to itself, we need
* In order for @liferay/eslint-config to be applied to itself, we need
* to make a copy of the default config that uses a path-based reference
* to the local config instead of a module name.
*/
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* ESLint doesn't provide a way to bundle a config and rules together
* in one package, so this ghastly helper applies monkey-patches
* to enable us to expose bundled rule plugins from inside
* eslint-config-liferay without actually having to separately publish a
* @liferay/eslint-config without actually having to separately publish a
* "eslint-plugin-liferay" or "eslint-plugin-liferay-portal" package.
*/

Expand Down Expand Up @@ -42,7 +42,7 @@ function patch() {

if (majorVersion > 5) {
if (!originalResolve) {
// eslint-disable-next-line liferay/no-dynamic-require
// eslint-disable-next-line @liferay/liferay/no-dynamic-require
const resolver = require(require.resolve(
'eslint/lib/shared/relative-module-resolver',
{paths: [process.cwd()]}
Expand Down Expand Up @@ -74,20 +74,24 @@ function patch() {
}

/**
* Registers `pluginName` as a local plugin bundled with eslint-config-liferay.
* Registers `pluginName` as a local plugin bundled with @liferay/eslint-config.
*/
function local(pluginName) {
const basename = pluginName.startsWith('eslint-plugin-')
? pluginName
: `eslint-plugin-${pluginName}`;
const [, basename] = pluginName.match(/^@liferay\/(.+)/) || [];

if (!basename) {
throw new Error(
`local(): plug-in '${pluginName}' does not start with '@liferay'`
);
}

const location = path.join(__dirname, '../plugins/', basename);

if (!fs.existsSync(location)) {
throw new Error(`local(): no plug-in found at ${location}`);
}

localPlugins.set(basename, location);
localPlugins.set(`@liferay/eslint-plugin-${basename}`, location);

// Make sure patches are applied.

Expand Down
File renamed without changes.

0 comments on commit b0174d9

Please sign in to comment.