Skip to content

Commit

Permalink
New: adds flag to skip loading rules from FS
Browse files Browse the repository at this point in the history
This is a backward compatible change of the public API.
By default the Linter class' constructor instantiates a Rules instance
which (in its own constructor) has the side-effect of reading from the
file-system to obtain rule files that may have been put there by the
user. The issue with this is side-effect is that it breaks WebPack
bundles (at runtime they crash when the Linter gets instantiated). The
particular use-case that spawned this PR is running ESLint inside a
WebPack bundle deployed onto an AWS Lambda function. The Linter is
configured inline so there is no need to load any rules from the
file-system.
  • Loading branch information
petermetz committed Jan 16, 2019
1 parent 3ffcf26 commit cbfab0a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
13 changes: 13 additions & 0 deletions docs/developer-guide/nodejs-api.md
Expand Up @@ -9,6 +9,7 @@ While ESLint is designed to be run on the command line, it's possible to use ESL
* [SourceCode](#sourcecode)
* [splitLines()](#sourcecodesplitlines)
* [Linter](#linter)
* [constructor()](#linterconstructor)
* [verify()](#linterverify)
* [verifyAndFix()](#linterverifyandfix)
* [defineRule()](#linterdefinerule)
Expand Down Expand Up @@ -87,6 +88,18 @@ var Linter = require("eslint").Linter;
var linter = new Linter();
```

### Linter#constructor

* `config.skipRulesLoadFromFs` If you are using the ESLint NodeJS API in code that you are packaging with a module bundler such as WebPack, then you may experience crashes when instantiating the Linter class due to it's default behavior to attempt to read the rule files from the file-system from a directory that does not exist (because the ESLint code was moved out of context by the bundler software). In a scenario like the above one, you will need to configure your Linter instance to skip the loading of the rules which you can do so by setting the `config.skipRulesLoadFromFs` parameter to boolean `true`. This allows you to configure your Linter instance "manually" from code. Note that you can still configure your Linter instance this way if your execution environment does have the correct directories in place on the file-system.

```js
const Linter = require("eslint").Linter;
const config = {
skipRulesLoadFromFs: true // causes the Linter to skip reading rules from the file-system.
};
const linter = new Linter(config);
```

### Linter#verify

The most important method on `Linter` is `verify()`, which initiates linting of the given text. This method accepts three arguments:
Expand Down
4 changes: 2 additions & 2 deletions lib/linter.js
Expand Up @@ -769,12 +769,12 @@ const loadedParserMaps = new WeakMap();
*/
module.exports = class Linter {

constructor() {
constructor(config = { skipRulesLoadFromFs: false }) {
lastSourceCodes.set(this, null);
loadedParserMaps.set(this, new Map());
this.version = pkg.version;

this.rules = new Rules();
this.rules = new Rules({ skipRulesLoadFromFs: config.skipRulesLoadFromFs });
this.environments = new Environments();
}

Expand Down
6 changes: 4 additions & 2 deletions lib/rules.js
Expand Up @@ -57,10 +57,12 @@ function normalizeRule(rule) {
//------------------------------------------------------------------------------

class Rules {
constructor() {
constructor(config = { skipRulesLoadFromFs: false }) {
this._rules = Object.create(null);

this.load();
if (!config.skipRulesLoadFromFs) {
this.load();
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/linter.js
Expand Up @@ -2771,6 +2771,16 @@ describe("Linter", () => {
});
});

describe("skips loading of rules from file-system if flag is passed in to constructor config", () => {
it("should return no loaded rules", () => {
const config = { skipRulesLoadFromFs: true };
const emptyLinter = new Linter(config);
const rules = emptyLinter.getRules();

assert.strictEqual(rules.size, 0);
});
});

describe("when calling getRules", () => {
it("should return all loaded rules", () => {
const rules = linter.getRules();
Expand Down

0 comments on commit cbfab0a

Please sign in to comment.