Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix most "systematic" lint issues #995

Merged
merged 12 commits into from
Jan 25, 2019
Merged

Fix most "systematic" lint issues #995

merged 12 commits into from
Jan 25, 2019

Conversation

wincent
Copy link
Contributor

@wincent wincent commented Jan 24, 2019

These are the ones where I was mostly able to fix things by tweaking the config. After this I'll move on to the next tranche (which will be ones that require edits, but at least form a consistent pattern).

@wincent wincent requested a review from julien January 24, 2019 15:19
Good news and bad news:

The good news is that the linter is no longer choking on stuff in our
JSX files.

The bad news is that our error count has gone up as a result:

Before:

    ✖ 436 problems (272 errors, 164 warnings)

After:

    ✖ 926 problems (691 errors, 235 warnings)

I'll continue whittling away the warnings in subsequent commits.
These are files we couldn't parse until the previous commit added
babel-eslint.

Before:

    ✖ 926 problems (691 errors, 235 warnings)

After:

    ✖ 778 problems (543 errors, 235 warnings)
We have a bunch of lints like:

    A constructor name should not start with a lowercase letter  new-cap

Caused because of patterns like:

    new CKEDITOR.dom.walker(...);

This commit sets up exceptions for those. Note that I listed the
offenders explicitly instead of using a pattern because I didn't want
typos like "CKEDITOR.dom.wakler(...)" to slip through.

Before:

    ✖ 778 problems (543 errors, 235 warnings)

After:

    ✖ 688 problems (453 errors, 235 warnings)
To make it at least somewhat discoverable. You can already achieve the
same with `npm run lint -- --quiet`, but this makes the availability of
this functionality more obvious to anybody reading the package.json
file.

This is important because we currently have 235 warnings (related to
JSDoc) which, realistically, we won't be able to fix for a long time.

Bonus: moved `--fix` to right after the executable name in the
neighboring "fix" task for consistency.
These are our HOC wrapper functions that all start with capital letters
even though they are not called with `new`. We may remove some (or all)
of these in favor of other abstractions, so rather than change their
names, I am suppressing the lints related to these functions.

Fixed a couple of capitalization inconsistencies along the way as a
result of doing this lint:

- ButtonKeyStroke -> ButtonKeystroke (to match other usages)
- columnResizer -> ColumnResizer (because it really is a constructor)

Before:

    ✖ 688 problems (453 errors, 235 warnings)

After:

    ✖ 572 problems (337 errors, 235 warnings)
ESLint will get confused without this plug-in, for example, thinking
that an import is unused if it appears as a JSX component.

So, we add the plug-in and that takes us from:

    ✖ 572 problems (337 errors, 235 warnings)

to:

    ✖ 305 problems (305 errors, 0 warnings)

Only cheating slightly here by turning "react/prop-types" down from
"error" to "warn". Without this the error count skyrockets and the whole
thing becomes unworkable; making these warnings means that we can chew
through them slowly over time.
This takes us down from:

    ✖ 305 problems (305 errors, 0 warnings)

to:

    ✖ 298 problems (298 errors, 0 warnings)

and additionally gets rid of this console noise at the beginning of the
run:

    Warning: React version not specified
    in eslint-plugin-react settings. See
    https://github.com/yannickcr/eslint-plugin-react#configuration .
I haven't been running the "format" task on this file as I edit it; I
will set it up to run automatically in the future.
Fixes these lints:

```
   71:43  error  Unnecessary escape character: \/                    no-useless-escape
   75:35  error  Unnecessary escape character: \?                    no-useless-escape
   80:3   error  Expected an object to be thrown                     no-throw-literal
  199:4   error  Unexpected 'this'                                   no-invalid-this
  260:7   error  'regexBasePath' is assigned a value but never used  no-unused-vars
  260:32  error  Unnecessary escape character: \/                    no-useless-escape
```

The bug:

`regexBasePath` was indeed unused, because it wasn't exported in the
refactor done in bdbd6b0. This is all a
bit magical, but here is how it works:

Our webpack build uses "adapter/main" as an entry point with
configuration `library: 'AlloyEditor'` and `libraryTarget: 'window'`;
this has the effect of exposing everything exported by the adapter as
properties of `window.AlloyEditor` (but note, not `regexBasePath`
because it wasn't exported).

Another webpack build target uses "scripts/build/index" as an entry
point, which in turn imports `*` from the adapter and exposes it as
under `window.AlloyEditor`.

So that's how the exports of the build magically makes the adapter's
exports available on `AlloyEditor`, so that calls like
`AlloyEditor.[something]` can work.

But as I said above, this is *not* happening for `regexBasePath` even
though the adapter itself uses it (`getBasePath` is called by `getUrl`
which is called by `loadLanguageResources`). Why isn't this blowing up
in the release? Two reasons:

1.  In the open source release when we hit the line that does
    `match(AlloyEditor.regexBasePath)`, that's really equivalent to
    `match(undefined)`, which doesn't blow up and instead just matches
    against the beginning of the string. There is more code below this
    that I didn't analyze: maybe it gets through somehow; or maybe it
    blows up when we throw an error ("installation path could not be
    automatically detected"). The error instructs people how to work
    around the error anyway (by setting the `ALLOYEDITOR_BASEPATH`
    global variable), so perhaps that is how people are dealing with it.
2.  In Liferay Portal, we overwrite `AlloyEditor.regexBasePath` at
    runtime because our built bundle has a different name (ie. with a
    "liferay" prefix), and so the default regex wouldn't work for us.

The fix, then, is to export `regexBasePath`.

At this point, src/adapter/main.js is lint-free.
This whole thing is fragile, but the truth is in the codebase we only
ever call `AlloyEditor.[something}` so this is safe.
Without this plugin, ESLint spuriously warns about a bunch of legitimate
stuff, such as the use of `this` arrow functions in class instance
methods:

```
class extends WrappedComponent {
    applyStyle = () => {
        if (this.isActive()) { // <-- complains about "no-invalid-this"
...
```

So we turn *on* the "babel/no-invalid-this" rule and turn *off* the
built-in "no-invalid-this".

Before:

    ✖ 292 problems (292 errors, 0 warnings)

After:

    ✖ 165 problems (165 errors, 0 warnings)
The lint:

  29:29  error  'nextProps' is defined but never used  no-unused-vars

We were calling `super()` without forwarding the argument.

Test plan: `npm run test` and also `npm run dev && npm run start` and
manually test demo.
@julien julien merged commit d632e39 into liferay:2.x-develop Jan 25, 2019
@wincent wincent deleted the lint-fest branch January 25, 2019 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants