Skip to content

Commit

Permalink
Breaking: Switch to ESM (fixes #35) (#39)
Browse files Browse the repository at this point in the history
* Breaking: Switch to ESM

* Fix: CJS changes

* Fix: Though tests in prev. commit passed, change all CJS to cjs extension, except tests/fixtures/rules/make-syntax-error-rule.js given that ModuleResolve uses the Node Resolution Algorithm

* Fix: Rollup did not like requiring of default module.exports (without a plugin at least), so create require

* New: Add exports; also update devDeps. specify external modules in Rollup config, and align it with eslint-scope

* Fix: Add `dist` file

* Fix: We can use cjs for our final file after all

* Test: CommonJS

* Refactor: Drop Node overrides while still using non-ESM config

* Refactor: Drop `Module.createRequireFromPath`

* Test: Supply Node version which won't fail with dev.

* Fix: Windows needs file: for absolute URLs

* Refactor: Remove line break

* Fix: Convert further paths to file: imports for Windows

* Fix: Point `main` to CJS for older browsers

* Chore: update devDeps

* Update .github/workflows/ci.yml

Co-authored-by: Milos Djermanovic <[email protected]>

* Update tests/_utils/index.js

Co-authored-by: Milos Djermanovic <[email protected]>

* Update package.json

Co-authored-by: Milos Djermanovic <[email protected]>

* Refactor: Switch two of the `conf` files to ESM

Co-authored-by: Milos Djermanovic <[email protected]>
  • Loading branch information
brettz9 and mdjermanovic authored Aug 4, 2021
1 parent 9bf2ba1 commit bdce01a
Show file tree
Hide file tree
Showing 60 changed files with 555 additions and 431 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; EditorConfig file: https://EditorConfig.org
; Install the "EditorConfig" plugin into your editor to use

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[package.json]
indent_size = 2
3 changes: 1 addition & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
/coverage/
/docs/
/jsdoc/


/dist/
9 changes: 8 additions & 1 deletion .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ module.exports = {
"no-restricted-syntax": ["error", {
selector: "CallExpression[callee.object.name='assert'][callee.property.name='doesNotThrow']",
message: "`assert.doesNotThrow()` should be replaced with a comment next to the code."
}],

// Overcome https://github.com/mysticatea/eslint-plugin-node/issues/250
"node/no-unsupported-features/es-syntax": ["error", {
ignores: [
"modules",
"dynamicImport"
]
}]
}
}

]
};
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: '14.x'
- name: Install Packages
run: npm install
- name: Lint Files
Expand All @@ -22,7 +24,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
node: [16.x, 15.x, 14.x, 13.x, 12.x, 10.x, "10.12.0"]
node: [16.x, 14.x, 12.x, "12.22.0"]
include:
- os: windows-latest
node: "12.x"
Expand Down
4 changes: 1 addition & 3 deletions conf/config-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
* @author Sylvan Mably
*/

"use strict";

const baseConfigProperties = {
$schema: { type: "string" },
env: { type: "object" },
Expand Down Expand Up @@ -78,4 +76,4 @@ const configSchema = {
$ref: "#/definitions/objectConfig"
};

module.exports = configSchema;
export default configSchema;
5 changes: 2 additions & 3 deletions conf/environments.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
* @fileoverview Defines environment settings and globals.
* @author Elan Shanker
*/
"use strict";

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

const globals = require("globals");
import globals from "globals";

//------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -55,7 +54,7 @@ const newGlobals2021 = {
//------------------------------------------------------------------------------

/** @type {Map<string, import("../lib/shared/types").Environment>} */
module.exports = new Map(Object.entries({
export default new Map(Object.entries({

// Language
builtin: {
Expand Down
File renamed without changes.
File renamed without changes.
19 changes: 10 additions & 9 deletions lib/cascading-config-array-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@
*
* @author Toru Nagashima <https://github.com/mysticatea>
*/
"use strict";

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

const os = require("os");
const path = require("path");
const ConfigValidator = require("./shared/config-validator");
const { emitDeprecationWarning } = require("./shared/deprecation-warnings");
const { ConfigArrayFactory } = require("./config-array-factory");
const { ConfigArray, ConfigDependency, IgnorePattern } = require("./config-array");
const debug = require("debug")("eslintrc:cascading-config-array-factory");
import os from "os";
import path from "path";
import ConfigValidator from "./shared/config-validator.js";
import { emitDeprecationWarning } from "./shared/deprecation-warnings.js";
import { ConfigArrayFactory } from "./config-array-factory.js";
import { ConfigArray, ConfigDependency, IgnorePattern } from "./config-array/index.js";
import debugOrig from "debug";

const debug = debugOrig("eslintrc:cascading-config-array-factory");

//------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -516,4 +517,4 @@ class CascadingConfigArrayFactory {
// Public Interface
//------------------------------------------------------------------------------

module.exports = { CascadingConfigArrayFactory };
export { CascadingConfigArrayFactory };
28 changes: 16 additions & 12 deletions lib/config-array-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,30 @@
*
* @author Toru Nagashima <https://github.com/mysticatea>
*/
"use strict";

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

const fs = require("fs");
const path = require("path");
const importFresh = require("import-fresh");
const stripComments = require("strip-json-comments");
const ConfigValidator = require("./shared/config-validator");
const naming = require("./shared/naming");
const ModuleResolver = require("./shared/relative-module-resolver");
const {
import fs from "fs";
import path from "path";
import importFresh from "import-fresh";
import stripComments from "strip-json-comments";
import ConfigValidator from "./shared/config-validator.js";
import * as naming from "./shared/naming.js";
import * as ModuleResolver from "./shared/relative-module-resolver.js";
import {
ConfigArray,
ConfigDependency,
IgnorePattern,
OverrideTester
} = require("./config-array");
const debug = require("debug")("eslintrc:config-array-factory");
} from "./config-array/index.js";
import debugOrig from "debug";

import { createRequire } from "module";
const require = createRequire(import.meta.url);

const debug = debugOrig("eslintrc:config-array-factory");

//------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -1096,4 +1100,4 @@ class ConfigArrayFactory {
}
}

module.exports = { ConfigArrayFactory, createContext };
export { ConfigArrayFactory, createContext };
35 changes: 17 additions & 18 deletions lib/config-array/config-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@
*
* @author Toru Nagashima <https://github.com/mysticatea>
*/
"use strict";

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

const { ExtractedConfig } = require("./extracted-config");
const { IgnorePattern } = require("./ignore-pattern");
import { ExtractedConfig } from "./extracted-config.js";
import { IgnorePattern } from "./ignore-pattern.js";

//------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -504,21 +503,21 @@ class ConfigArray extends Array {
}
}

const exportObject = {
ConfigArray,
/**
* Get the used extracted configs.
* CLIEngine will use this method to collect used deprecated rules.
* @param {ConfigArray} instance The config array object to get.
* @returns {ExtractedConfig[]} The used extracted configs.
* @private
*/
function getUsedExtractedConfigs(instance) {
const { cache } = internalSlotsMap.get(instance);

/**
* Get the used extracted configs.
* CLIEngine will use this method to collect used deprecated rules.
* @param {ConfigArray} instance The config array object to get.
* @returns {ExtractedConfig[]} The used extracted configs.
* @private
*/
getUsedExtractedConfigs(instance) {
const { cache } = internalSlotsMap.get(instance);
return Array.from(cache.values());
}

return Array.from(cache.values());
}
};

module.exports = exportObject;
export {
ConfigArray,
getUsedExtractedConfigs
};
5 changes: 2 additions & 3 deletions lib/config-array/config-dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
*
* @author Toru Nagashima <https://github.com/mysticatea>
*/
"use strict";

const util = require("util");
import util from "util";

/**
* The class is to store parsers or plugins.
Expand Down Expand Up @@ -113,4 +112,4 @@ class ConfigDependency {
/** @typedef {ConfigDependency<import("../../shared/types").Parser>} DependentParser */
/** @typedef {ConfigDependency<import("../../shared/types").Plugin>} DependentPlugin */

module.exports = { ConfigDependency };
export { ConfigDependency };
5 changes: 2 additions & 3 deletions lib/config-array/extracted-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
*
* @author Toru Nagashima <https://github.com/mysticatea>
*/
"use strict";

const { IgnorePattern } = require("./ignore-pattern");
import { IgnorePattern } from "./ignore-pattern.js";

// For VSCode intellisense
/** @typedef {import("../../shared/types").ConfigData} ConfigData */
Expand Down Expand Up @@ -143,4 +142,4 @@ class ExtractedConfig {
}
}

module.exports = { ExtractedConfig };
export { ExtractedConfig };
13 changes: 7 additions & 6 deletions lib/config-array/ignore-pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@
*
* @author Toru Nagashima <https://github.com/mysticatea>
*/
"use strict";

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

const assert = require("assert");
const path = require("path");
const ignore = require("ignore");
const debug = require("debug")("eslintrc:ignore-pattern");
import assert from "assert";
import path from "path";
import ignore from "ignore";
import debugOrig from "debug";

const debug = debugOrig("eslintrc:ignore-pattern");

/** @typedef {ReturnType<import("ignore").default>} Ignore */

Expand Down Expand Up @@ -234,4 +235,4 @@ class IgnorePattern {
}
}

module.exports = { IgnorePattern };
export { IgnorePattern };
13 changes: 6 additions & 7 deletions lib/config-array/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
* @fileoverview `ConfigArray` class.
* @author Toru Nagashima <https://github.com/mysticatea>
*/
"use strict";

const { ConfigArray, getUsedExtractedConfigs } = require("./config-array");
const { ConfigDependency } = require("./config-dependency");
const { ExtractedConfig } = require("./extracted-config");
const { IgnorePattern } = require("./ignore-pattern");
const { OverrideTester } = require("./override-tester");
import { ConfigArray, getUsedExtractedConfigs } from "./config-array.js";
import { ConfigDependency } from "./config-dependency.js";
import { ExtractedConfig } from "./extracted-config.js";
import { IgnorePattern } from "./ignore-pattern.js";
import { OverrideTester } from "./override-tester.js";

module.exports = {
export {
ConfigArray,
ConfigDependency,
ExtractedConfig,
Expand Down
14 changes: 8 additions & 6 deletions lib/config-array/override-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
*
* @author Toru Nagashima <https://github.com/mysticatea>
*/
"use strict";

const assert = require("assert");
const path = require("path");
const util = require("util");
const { Minimatch } = require("minimatch");
import assert from "assert";
import path from "path";
import util from "util";
import minimatch from "minimatch";

const { Minimatch } = minimatch;

const minimatchOpts = { dot: true, matchBase: true };

/**
Expand Down Expand Up @@ -220,4 +222,4 @@ class OverrideTester {
}
}

module.exports = { OverrideTester };
export { OverrideTester };
20 changes: 11 additions & 9 deletions lib/flat-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
* @author Nicholas C. Zakas
*/

"use strict";

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

const path = require("path");
const environments = require("../conf/environments");
const createDebug = require("debug");
const { ConfigArrayFactory } = require("./config-array-factory");
import path from "path";
import { fileURLToPath } from "url";
import createDebug from "debug";

import { ConfigArrayFactory } from "./config-array-factory.js";
import environments from "../conf/environments.js";

const dirname = path.dirname(fileURLToPath(import.meta.url));

//-----------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -223,8 +225,8 @@ class FlatCompat {
this[cafactory] = new ConfigArrayFactory({
cwd: baseDirectory,
resolvePluginsRelativeTo,
eslintAllPath: path.resolve(__dirname, "../conf/eslint-all.js"),
eslintRecommendedPath: path.resolve(__dirname, "../conf/eslint-recommended.js")
eslintAllPath: path.resolve(dirname, "../conf/eslint-all.cjs"),
eslintRecommendedPath: path.resolve(dirname, "../conf/eslint-recommended.cjs")
});
}

Expand Down Expand Up @@ -305,4 +307,4 @@ class FlatCompat {
}
}

exports.FlatCompat = FlatCompat;
export { FlatCompat };
Loading

0 comments on commit bdce01a

Please sign in to comment.