Skip to content

Commit

Permalink
Breaking: Strict package exports (refs #13654) (#14706)
Browse files Browse the repository at this point in the history
* Breaking: Strict package exports (refs #13654)

* Update docs

* Fix linting errors

* Update docs/developer-guide/nodejs-api.md

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update docs/developer-guide/nodejs-api.md

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
  • Loading branch information
nzakas and mdjermanovic committed Aug 5, 2021
1 parent 86d31a4 commit 24c9f2a
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 534 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -110,6 +110,7 @@ module.exports = {
// Restrict relative path imports
{
files: ["lib/*"],
excludedFiles: ["lib/unsupported-api.js"],
rules: {
"node/no-restricted-require": ["error", [
...createInternalFilesPatterns()
Expand Down
516 changes: 2 additions & 514 deletions docs/developer-guide/nodejs-api.md

Large diffs are not rendered by default.

24 changes: 8 additions & 16 deletions lib/api.js
Expand Up @@ -5,30 +5,22 @@

"use strict";

const { CLIEngine } = require("./cli-engine");
//-----------------------------------------------------------------------------
// Requirements
//-----------------------------------------------------------------------------

const { ESLint } = require("./eslint");
const { Linter } = require("./linter");
const { RuleTester } = require("./rule-tester");
const { SourceCode } = require("./source-code");

//-----------------------------------------------------------------------------
// Exports
//-----------------------------------------------------------------------------

module.exports = {
Linter,
CLIEngine,
ESLint,
RuleTester,
SourceCode
};

// DOTO: remove deprecated API.
let deprecatedLinterInstance = null;

Object.defineProperty(module.exports, "linter", {
enumerable: false,
get() {
if (!deprecatedLinterInstance) {
deprecatedLinterInstance = new Linter();
}

return deprecatedLinterInstance;
}
});
23 changes: 23 additions & 0 deletions lib/unsupported-api.js
@@ -0,0 +1,23 @@
/**
* @fileoverview APIs that are not officially supported by ESLint.
* These APIs may change or be removed at any time. Use at your
* own risk.
* @author Nicholas C. Zakas
*/

"use strict";

//-----------------------------------------------------------------------------
// Requirements
//-----------------------------------------------------------------------------

const { FileEnumerator } = require("./cli-engine/file-enumerator");

//-----------------------------------------------------------------------------
// Exports
//-----------------------------------------------------------------------------

module.exports = {
builtinRules: require("./rules"),
FileEnumerator
};
5 changes: 5 additions & 0 deletions package.json
Expand Up @@ -7,6 +7,11 @@
"eslint": "./bin/eslint.js"
},
"main": "./lib/api.js",
"exports": {
"./package.json": "./package.json",
".": "./lib/api.js",
"./use-at-your-own-risk": "./lib/unsupported-api.js"
},
"scripts": {
"test": "node Makefile.js test",
"test:cli": "mocha",
Expand Down
20 changes: 16 additions & 4 deletions tests/lib/api.js
Expand Up @@ -5,21 +5,33 @@

"use strict";

//-----------------------------------------------------------------------------
// Requirements
//-----------------------------------------------------------------------------

const assert = require("chai").assert,
api = require("../../lib/api");

//-----------------------------------------------------------------------------
// Tests
//-----------------------------------------------------------------------------

describe("api", () => {

it("should have RuleTester exposed", () => {
assert.isFunction(api.RuleTester);
});

it("should have CLIEngine exposed", () => {
assert.isFunction(api.CLIEngine);
it("should not have CLIEngine exposed", () => {
assert.isUndefined(api.CLIEngine);
});

it("should not have linter exposed", () => {
assert.isUndefined(api.linter);
});

it("should have linter exposed", () => {
assert.isObject(api.linter);
it("should have Linter exposed", () => {
assert.isFunction(api.Linter);
});

it("should have SourceCode exposed", () => {
Expand Down
30 changes: 30 additions & 0 deletions tests/lib/unsupported-api.js
@@ -0,0 +1,30 @@
/**
* @fileoverview Tests for unsupported-api.
* @author Nicholas C. Zakas
*/

"use strict";

//-----------------------------------------------------------------------------
// Requirements
//-----------------------------------------------------------------------------

const assert = require("chai").assert,
{ LazyLoadingRuleMap } = require("../../lib/rules/utils/lazy-loading-rule-map"),
api = require("../../lib/unsupported-api");

//-----------------------------------------------------------------------------
// Tests
//-----------------------------------------------------------------------------

describe("unsupported-api", () => {

it("should have FileEnumerator exposed", () => {
assert.isFunction(api.FileEnumerator);
});

it("should have builtinRules exposed", () => {
assert.instanceOf(api.builtinRules, LazyLoadingRuleMap);
});

});

0 comments on commit 24c9f2a

Please sign in to comment.