question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Eslint CLIEngine removes empty line between imports incorrectly, but eslint --fix works

See original GitHub issue

Tell us about your environment

  • ESLint Version: 5.0.1
  • Node Version: 8.11.1
  • yarn version: 1.5.1

What parser (default, Babel-ESLint, etc.) are you using? babel-eslint

Please show your full configuration:

Configuration
const path = require("path");

const config = {
  parser: "babel-eslint",
  env: {
    node: true
  },
  plugins: ["import"],
  rules: {
    "import/order": [
      "error",
      {
        groups: [
          ["builtin", "external"],
          "internal",
          "parent",
          ["sibling", "index"]
        ],
        "newlines-between": "always"
      }
    ]
  },
  settings: {
    "import/resolver": {
      node: {
        root: __dirname
      },
      alias: [["appalias", path.resolve(__dirname, "src/app")]]
    }
  }
};

module.exports = config;

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

Run eslint CLIEngine on a source file using import/order rule that requires newline between import groups.

run-eslint-cli.js

const eslint = require("eslint");

const config = require("./.eslintrc.js");

const CLIEngine = eslint.CLIEngine;

const cli = new CLIEngine({
  ...config,
  useEslintrc: false,
  fix: true
});
const results = cli.executeOnFiles(["./src/example.js"]);
CLIEngine.outputFixes(results);
console.info("Results:\n" + JSON.stringify(results, null, 2));

src/example.js (file being formatted)

const fs = require("fs");
const main = require("src/main");

const app = require("appalias");

const packageJson = require("../package.json");

const mainSibling = require("./main");

Reproduction repository:

https://github.com/amosyuen/prettier-eslint-import-empty-line-removal

What did you expect to happen?

eslint shouldn’t remove the empty line between const main = require("src/main"); and const app = require("appalias");.

What actually happened? Please include the actual, raw output from ESLint.

Eslint CLI engine incorrectly removes the empty line between const main = require("src/main"); and const app = require("appalias");. This causes an eslint error.

However, running the eslint cli directly works. yarn eslint --fix will correctly add back the empty line.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
amosyuencommented, Jul 7, 2018

So after some reading around I think I see the problem. Your 4th case for CLIEngine should correspond to the broken case. I didn’t realize it because you had an extra config: in there, which I think is a typo.

Looking at https://eslint.org/docs/developer-guide/nodejs-api#cliengine, seems like settings isn’t a field on the options, so that would explain why my eslint settings aren’t used. I’m guessing instead of config field, I think you meant to use configFile field.

If that’s true then seems like the problem is I was assuming the CLIEngine options should support all the same fields as a normal eslintrc file. While the documentation doesn’t list all the fields, this seems like an easy and error prone thing for developers to do. Perhaps some extra documentation warning that it doesn’t support all the same fields as eslintrc would be nice. Note: this is exactly how prettier-eslint uses CLIEngine, which is what lead me to file this bug in the first place.

For reference, the bug at prettier-eslint https://github.com/prettier/prettier-eslint/issues/181

0reactions
amosyuencommented, Oct 12, 2018

Hey, sorry didn’t get back to this in a while, but I think the specific problem is eslint supports only a subset of config options directly on the settings or a path to a config file with full settings. The action item is that eslint should support all config options directly on the settings.

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-multiple-empty-lines - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
Code Quality report fails when ESLint is set up (with plugins ...
I'm not entirely sure, but it seems like it tries to use my project's configuration file, but then does not install the dependencies...
Read more >
Eslint ignore - Caritas Castellaneta
A list of regex strings that, if matched by a path, will eslint-plugin-import. yarn start to ignore, add // eslint-disable-next-line to the line...
Read more >
eslint | Yarn - Package Manager
A bug fix to the CLI or core (including formatters). Improvements to documentation. Non-user-facing changes such as refactoring code, adding, deleting, or ...
Read more >
eslint/eslint - Gitter
(The package "eslint-plugin-import" was not found when loaded as a Node module from the directory "/".) It's likely that the plugin isn't installed...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found