From 2109bf71309e4a13b72475d393069d7fa7f34640 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Fri, 25 Jun 2021 19:13:47 +0800 Subject: [PATCH 01/20] Breaking: Switch to ESM --- .editorconfig | 15 ++ .eslintignore | 3 +- .eslintrc.js => .eslintrc.cjs | 21 ++ .github/workflows/ci.yml | 2 +- conf/{config-schema.js => config-schema.cjs} | 0 conf/{environments.js => environments.cjs} | 0 conf/{eslint-all.js => eslint-all.cjs} | 0 ...-recommended.js => eslint-recommended.cjs} | 0 lib/cascading-config-array-factory.js | 19 +- lib/config-array-factory.js | 28 ++- lib/config-array/config-array.js | 35 ++- lib/config-array/config-dependency.js | 5 +- lib/config-array/extracted-config.js | 5 +- lib/config-array/ignore-pattern.js | 13 +- lib/config-array/index.js | 13 +- lib/config-array/override-tester.js | 14 +- lib/flat-compat.js | 20 +- lib/index.js | 72 +++--- lib/shared/ajv.js | 11 +- lib/shared/config-ops.js | 219 +++++++++--------- lib/shared/config-validator.js | 21 +- lib/shared/deprecation-warnings.js | 5 +- lib/shared/naming.js | 3 +- lib/shared/relative-module-resolver.js | 55 +++-- lib/shared/types.js | 3 +- package.json | 20 +- rollup.config.js | 9 + tests/_utils/index.js | 6 +- tests/lib/cascading-config-array-factory.js | 74 +++--- tests/lib/config-array-factory.js | 55 +++-- tests/lib/config-array/config-array.js | 55 ++--- tests/lib/config-array/config-dependency.js | 9 +- tests/lib/config-array/extracted-config.js | 5 +- tests/lib/config-array/ignore-pattern.js | 9 +- tests/lib/config-array/override-tester.js | 11 +- tests/lib/flat-compat.js | 40 ++-- tests/lib/shared/config-ops.js | 9 +- tests/lib/shared/naming.js | 6 +- tests/lib/shared/relative-module-resolver.js | 14 +- 39 files changed, 490 insertions(+), 414 deletions(-) create mode 100644 .editorconfig rename .eslintrc.js => .eslintrc.cjs (58%) rename conf/{config-schema.js => config-schema.cjs} (100%) rename conf/{environments.js => environments.cjs} (100%) rename conf/{eslint-all.js => eslint-all.cjs} (100%) rename conf/{eslint-recommended.js => eslint-recommended.cjs} (100%) create mode 100644 rollup.config.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..9fe93ed0 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.eslintignore b/.eslintignore index c9f54560..9526af38 100644 --- a/.eslintignore +++ b/.eslintignore @@ -4,5 +4,4 @@ /coverage/ /docs/ /jsdoc/ - - +/dist/ diff --git a/.eslintrc.js b/.eslintrc.cjs similarity index 58% rename from .eslintrc.js rename to .eslintrc.cjs index e8c1af62..10615da8 100644 --- a/.eslintrc.js +++ b/.eslintrc.cjs @@ -20,6 +20,19 @@ module.exports = { }, overrides: [ + { + files: [".eslintrc.js", "conf/**"], + env: { + node: true + }, + globals: { + require: true, + module: true + }, + parserOptions: { + sourceType: "script" + } + }, { files: ["tests/**/*"], env: { mocha: true }, @@ -27,6 +40,14 @@ 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" + ] }] } } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 61ef9374..b282639c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,7 +22,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" diff --git a/conf/config-schema.js b/conf/config-schema.cjs similarity index 100% rename from conf/config-schema.js rename to conf/config-schema.cjs diff --git a/conf/environments.js b/conf/environments.cjs similarity index 100% rename from conf/environments.js rename to conf/environments.cjs diff --git a/conf/eslint-all.js b/conf/eslint-all.cjs similarity index 100% rename from conf/eslint-all.js rename to conf/eslint-all.cjs diff --git a/conf/eslint-recommended.js b/conf/eslint-recommended.cjs similarity index 100% rename from conf/eslint-recommended.js rename to conf/eslint-recommended.cjs diff --git a/lib/cascading-config-array-factory.js b/lib/cascading-config-array-factory.js index c991c24b..553ca0a9 100644 --- a/lib/cascading-config-array-factory.js +++ b/lib/cascading-config-array-factory.js @@ -17,19 +17,20 @@ * * @author Toru Nagashima */ -"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 @@ -516,4 +517,4 @@ class CascadingConfigArrayFactory { // Public Interface //------------------------------------------------------------------------------ -module.exports = { CascadingConfigArrayFactory }; +export { CascadingConfigArrayFactory }; diff --git a/lib/config-array-factory.js b/lib/config-array-factory.js index 406601c2..0bef3477 100644 --- a/lib/config-array-factory.js +++ b/lib/config-array-factory.js @@ -33,26 +33,30 @@ * * @author Toru Nagashima */ -"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 @@ -1096,4 +1100,4 @@ class ConfigArrayFactory { } } -module.exports = { ConfigArrayFactory, createContext }; +export { ConfigArrayFactory, createContext }; diff --git a/lib/config-array/config-array.js b/lib/config-array/config-array.js index 42a73627..133f5a24 100644 --- a/lib/config-array/config-array.js +++ b/lib/config-array/config-array.js @@ -24,14 +24,13 @@ * * @author Toru Nagashima */ -"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 @@ -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 +}; diff --git a/lib/config-array/config-dependency.js b/lib/config-array/config-dependency.js index 0d5f6f71..2883c3a2 100644 --- a/lib/config-array/config-dependency.js +++ b/lib/config-array/config-dependency.js @@ -14,9 +14,8 @@ * * @author Toru Nagashima */ -"use strict"; -const util = require("util"); +import util from "util"; /** * The class is to store parsers or plugins. @@ -113,4 +112,4 @@ class ConfigDependency { /** @typedef {ConfigDependency} DependentParser */ /** @typedef {ConfigDependency} DependentPlugin */ -module.exports = { ConfigDependency }; +export { ConfigDependency }; diff --git a/lib/config-array/extracted-config.js b/lib/config-array/extracted-config.js index b27d6ffb..e93b0b67 100644 --- a/lib/config-array/extracted-config.js +++ b/lib/config-array/extracted-config.js @@ -14,9 +14,8 @@ * * @author Toru Nagashima */ -"use strict"; -const { IgnorePattern } = require("./ignore-pattern"); +import { IgnorePattern } from "./ignore-pattern.js"; // For VSCode intellisense /** @typedef {import("../../shared/types").ConfigData} ConfigData */ @@ -143,4 +142,4 @@ class ExtractedConfig { } } -module.exports = { ExtractedConfig }; +export { ExtractedConfig }; diff --git a/lib/config-array/ignore-pattern.js b/lib/config-array/ignore-pattern.js index 4c16278e..c0a86359 100644 --- a/lib/config-array/ignore-pattern.js +++ b/lib/config-array/ignore-pattern.js @@ -27,16 +27,17 @@ * * @author Toru Nagashima */ -"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} Ignore */ @@ -234,4 +235,4 @@ class IgnorePattern { } } -module.exports = { IgnorePattern }; +export { IgnorePattern }; diff --git a/lib/config-array/index.js b/lib/config-array/index.js index 928d76c8..647f02b7 100644 --- a/lib/config-array/index.js +++ b/lib/config-array/index.js @@ -2,15 +2,14 @@ * @fileoverview `ConfigArray` class. * @author Toru Nagashima */ -"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, diff --git a/lib/config-array/override-tester.js b/lib/config-array/override-tester.js index e7ba1202..460aafcf 100644 --- a/lib/config-array/override-tester.js +++ b/lib/config-array/override-tester.js @@ -16,12 +16,14 @@ * * @author Toru Nagashima */ -"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 }; /** @@ -220,4 +222,4 @@ class OverrideTester { } } -module.exports = { OverrideTester }; +export { OverrideTester }; diff --git a/lib/flat-compat.js b/lib/flat-compat.js index c0d0ea22..58a0e3b2 100644 --- a/lib/flat-compat.js +++ b/lib/flat-compat.js @@ -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 environments from "../conf/environments.cjs"; +import createDebug from "debug"; +import { ConfigArrayFactory } from "./config-array-factory.js"; + +const dirname = path.dirname(fileURLToPath(import.meta.url)); //----------------------------------------------------------------------------- // Helpers @@ -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") }); } @@ -305,4 +307,4 @@ class FlatCompat { } } -exports.FlatCompat = FlatCompat; +export { FlatCompat }; diff --git a/lib/index.js b/lib/index.js index de2a95d9..812562ee 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,52 +2,52 @@ * @fileoverview Package exports for @eslint/eslintrc * @author Nicholas C. Zakas */ -"use strict"; - //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ -const { +import { ConfigArrayFactory, - createContext: createConfigArrayFactoryContext -} = require("./config-array-factory"); - -const { CascadingConfigArrayFactory } = require("./cascading-config-array-factory"); -const ModuleResolver = require("./shared/relative-module-resolver"); -const { ConfigArray, getUsedExtractedConfigs } = require("./config-array"); -const { ConfigDependency } = require("./config-array/config-dependency"); -const { ExtractedConfig } = require("./config-array/extracted-config"); -const { IgnorePattern } = require("./config-array/ignore-pattern"); -const { OverrideTester } = require("./config-array/override-tester"); -const ConfigOps = require("./shared/config-ops"); -const ConfigValidator = require("./shared/config-validator"); -const naming = require("./shared/naming"); -const { FlatCompat } = require("./flat-compat"); + createContext as createConfigArrayFactoryContext +} from "./config-array-factory.js"; + +import { CascadingConfigArrayFactory } from "./cascading-config-array-factory.js"; +import * as ModuleResolver from "./shared/relative-module-resolver.js"; +import { ConfigArray, getUsedExtractedConfigs } from "./config-array/index.js"; +import { ConfigDependency } from "./config-array/config-dependency.js"; +import { ExtractedConfig } from "./config-array/extracted-config.js"; +import { IgnorePattern } from "./config-array/ignore-pattern.js"; +import { OverrideTester } from "./config-array/override-tester.js"; +import * as ConfigOps from "./shared/config-ops.js"; +import ConfigValidator from "./shared/config-validator.js"; +import * as naming from "./shared/naming.js"; +import { FlatCompat } from "./flat-compat.js"; //----------------------------------------------------------------------------- // Exports //----------------------------------------------------------------------------- -module.exports = { - - Legacy: { - ConfigArray, - createConfigArrayFactoryContext, - CascadingConfigArrayFactory, - ConfigArrayFactory, - ConfigDependency, - ExtractedConfig, - IgnorePattern, - OverrideTester, - getUsedExtractedConfigs, - - // shared - ConfigOps, - ConfigValidator, - ModuleResolver, - naming - }, +const Legacy = { + ConfigArray, + createConfigArrayFactoryContext, + CascadingConfigArrayFactory, + ConfigArrayFactory, + ConfigDependency, + ExtractedConfig, + IgnorePattern, + OverrideTester, + getUsedExtractedConfigs, + + // shared + ConfigOps, + ConfigValidator, + ModuleResolver, + naming +}; + +export { + + Legacy, FlatCompat diff --git a/lib/shared/ajv.js b/lib/shared/ajv.js index 3fb0fbdd..996cd7e7 100644 --- a/lib/shared/ajv.js +++ b/lib/shared/ajv.js @@ -2,20 +2,23 @@ * @fileoverview The instance of Ajv validator. * @author Evgeny Poberezkin */ -"use strict"; //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ -const Ajv = require("ajv"), - metaSchema = require("ajv/lib/refs/json-schema-draft-04.json"); +import Ajv from "ajv"; + +import { createRequire } from "module"; +const require = createRequire(import.meta.url); + +const metaSchema = require("ajv/lib/refs/json-schema-draft-04.json"); //------------------------------------------------------------------------------ // Public Interface //------------------------------------------------------------------------------ -module.exports = (additionalOptions = {}) => { +export default (additionalOptions = {}) => { const ajv = new Ajv({ meta: false, useDefaults: true, diff --git a/lib/shared/config-ops.js b/lib/shared/config-ops.js index 3b4d5699..d203be0e 100644 --- a/lib/shared/config-ops.js +++ b/lib/shared/config-ops.js @@ -3,7 +3,6 @@ * so no Node-specific code can be here. * @author Nicholas C. Zakas */ -"use strict"; //------------------------------------------------------------------------------ // Private @@ -20,111 +19,117 @@ const RULE_SEVERITY_STRINGS = ["off", "warn", "error"], // Public Interface //------------------------------------------------------------------------------ -module.exports = { - - /** - * Normalizes the severity value of a rule's configuration to a number - * @param {(number|string|[number, ...*]|[string, ...*])} ruleConfig A rule's configuration value, generally - * received from the user. A valid config value is either 0, 1, 2, the string "off" (treated the same as 0), - * the string "warn" (treated the same as 1), the string "error" (treated the same as 2), or an array - * whose first element is one of the above values. Strings are matched case-insensitively. - * @returns {(0|1|2)} The numeric severity value if the config value was valid, otherwise 0. - */ - getRuleSeverity(ruleConfig) { - const severityValue = Array.isArray(ruleConfig) ? ruleConfig[0] : ruleConfig; - - if (severityValue === 0 || severityValue === 1 || severityValue === 2) { - return severityValue; - } - - if (typeof severityValue === "string") { - return RULE_SEVERITY[severityValue.toLowerCase()] || 0; - } - - return 0; - }, - - /** - * Converts old-style severity settings (0, 1, 2) into new-style - * severity settings (off, warn, error) for all rules. Assumption is that severity - * values have already been validated as correct. - * @param {Object} config The config object to normalize. - * @returns {void} - */ - normalizeToStrings(config) { - - if (config.rules) { - Object.keys(config.rules).forEach(ruleId => { - const ruleConfig = config.rules[ruleId]; - - if (typeof ruleConfig === "number") { - config.rules[ruleId] = RULE_SEVERITY_STRINGS[ruleConfig] || RULE_SEVERITY_STRINGS[0]; - } else if (Array.isArray(ruleConfig) && typeof ruleConfig[0] === "number") { - ruleConfig[0] = RULE_SEVERITY_STRINGS[ruleConfig[0]] || RULE_SEVERITY_STRINGS[0]; - } - }); - } - }, - - /** - * Determines if the severity for the given rule configuration represents an error. - * @param {int|string|Array} ruleConfig The configuration for an individual rule. - * @returns {boolean} True if the rule represents an error, false if not. - */ - isErrorSeverity(ruleConfig) { - return module.exports.getRuleSeverity(ruleConfig) === 2; - }, - - /** - * Checks whether a given config has valid severity or not. - * @param {number|string|Array} ruleConfig The configuration for an individual rule. - * @returns {boolean} `true` if the configuration has valid severity. - */ - isValidSeverity(ruleConfig) { - let severity = Array.isArray(ruleConfig) ? ruleConfig[0] : ruleConfig; - - if (typeof severity === "string") { - severity = severity.toLowerCase(); - } - return VALID_SEVERITIES.indexOf(severity) !== -1; - }, - - /** - * Checks whether every rule of a given config has valid severity or not. - * @param {Object} config The configuration for rules. - * @returns {boolean} `true` if the configuration has valid severity. - */ - isEverySeverityValid(config) { - return Object.keys(config).every(ruleId => this.isValidSeverity(config[ruleId])); - }, - - /** - * Normalizes a value for a global in a config - * @param {(boolean|string|null)} configuredValue The value given for a global in configuration or in - * a global directive comment - * @returns {("readable"|"writeable"|"off")} The value normalized as a string - * @throws Error if global value is invalid - */ - normalizeConfigGlobal(configuredValue) { - switch (configuredValue) { - case "off": - return "off"; - - case true: - case "true": - case "writeable": - case "writable": - return "writable"; - - case null: - case false: - case "false": - case "readable": - case "readonly": - return "readonly"; - - default: - throw new Error(`'${configuredValue}' is not a valid configuration for a global (use 'readonly', 'writable', or 'off')`); - } +/** + * Normalizes the severity value of a rule's configuration to a number + * @param {(number|string|[number, ...*]|[string, ...*])} ruleConfig A rule's configuration value, generally + * received from the user. A valid config value is either 0, 1, 2, the string "off" (treated the same as 0), + * the string "warn" (treated the same as 1), the string "error" (treated the same as 2), or an array + * whose first element is one of the above values. Strings are matched case-insensitively. + * @returns {(0|1|2)} The numeric severity value if the config value was valid, otherwise 0. + */ +function getRuleSeverity(ruleConfig) { + const severityValue = Array.isArray(ruleConfig) ? ruleConfig[0] : ruleConfig; + + if (severityValue === 0 || severityValue === 1 || severityValue === 2) { + return severityValue; + } + + if (typeof severityValue === "string") { + return RULE_SEVERITY[severityValue.toLowerCase()] || 0; + } + + return 0; +} + +/** + * Converts old-style severity settings (0, 1, 2) into new-style + * severity settings (off, warn, error) for all rules. Assumption is that severity + * values have already been validated as correct. + * @param {Object} config The config object to normalize. + * @returns {void} + */ +function normalizeToStrings(config) { + + if (config.rules) { + Object.keys(config.rules).forEach(ruleId => { + const ruleConfig = config.rules[ruleId]; + + if (typeof ruleConfig === "number") { + config.rules[ruleId] = RULE_SEVERITY_STRINGS[ruleConfig] || RULE_SEVERITY_STRINGS[0]; + } else if (Array.isArray(ruleConfig) && typeof ruleConfig[0] === "number") { + ruleConfig[0] = RULE_SEVERITY_STRINGS[ruleConfig[0]] || RULE_SEVERITY_STRINGS[0]; + } + }); + } +} + +/** + * Determines if the severity for the given rule configuration represents an error. + * @param {int|string|Array} ruleConfig The configuration for an individual rule. + * @returns {boolean} True if the rule represents an error, false if not. + */ +function isErrorSeverity(ruleConfig) { + return getRuleSeverity(ruleConfig) === 2; +} + +/** + * Checks whether a given config has valid severity or not. + * @param {number|string|Array} ruleConfig The configuration for an individual rule. + * @returns {boolean} `true` if the configuration has valid severity. + */ +function isValidSeverity(ruleConfig) { + let severity = Array.isArray(ruleConfig) ? ruleConfig[0] : ruleConfig; + + if (typeof severity === "string") { + severity = severity.toLowerCase(); + } + return VALID_SEVERITIES.indexOf(severity) !== -1; +} + +/** + * Checks whether every rule of a given config has valid severity or not. + * @param {Object} config The configuration for rules. + * @returns {boolean} `true` if the configuration has valid severity. + */ +function isEverySeverityValid(config) { + return Object.keys(config).every(ruleId => isValidSeverity(config[ruleId])); +} + +/** + * Normalizes a value for a global in a config + * @param {(boolean|string|null)} configuredValue The value given for a global in configuration or in + * a global directive comment + * @returns {("readable"|"writeable"|"off")} The value normalized as a string + * @throws Error if global value is invalid + */ +function normalizeConfigGlobal(configuredValue) { + switch (configuredValue) { + case "off": + return "off"; + + case true: + case "true": + case "writeable": + case "writable": + return "writable"; + + case null: + case false: + case "false": + case "readable": + case "readonly": + return "readonly"; + + default: + throw new Error(`'${configuredValue}' is not a valid configuration for a global (use 'readonly', 'writable', or 'off')`); } +} + +export { + getRuleSeverity, + normalizeToStrings, + isErrorSeverity, + isValidSeverity, + isEverySeverityValid, + normalizeConfigGlobal }; diff --git a/lib/shared/config-validator.js b/lib/shared/config-validator.js index 90869739..e0755e97 100644 --- a/lib/shared/config-validator.js +++ b/lib/shared/config-validator.js @@ -3,22 +3,21 @@ * @author Brandon Mills */ -"use strict"; - /* eslint class-methods-use-this: "off" */ //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ -const - util = require("util"), - configSchema = require("../../conf/config-schema"), - BuiltInEnvironments = require("../../conf/environments"), - ConfigOps = require("./config-ops"), - { emitDeprecationWarning } = require("./deprecation-warnings"); +import util from "util"; +import configSchema from "../../conf/config-schema.cjs"; +import BuiltInEnvironments from "../../conf/environments.cjs"; +import * as ConfigOps from "./config-ops.js"; +import { emitDeprecationWarning } from "./deprecation-warnings.js"; +import ajvOrig from "./ajv.js"; + +const ajv = ajvOrig(); -const ajv = require("./ajv")(); const ruleValidators = new WeakMap(); const noop = Function.prototype; @@ -38,7 +37,7 @@ const validated = new WeakSet(); // Exports //----------------------------------------------------------------------------- -module.exports = class ConfigValidator { +export default class ConfigValidator { constructor({ builtInRules = new Map() } = {}) { this.builtInRules = builtInRules; } @@ -323,4 +322,4 @@ module.exports = class ConfigValidator { } } -}; +} diff --git a/lib/shared/deprecation-warnings.js b/lib/shared/deprecation-warnings.js index 136fc255..91907b13 100644 --- a/lib/shared/deprecation-warnings.js +++ b/lib/shared/deprecation-warnings.js @@ -2,13 +2,12 @@ * @fileoverview Provide the function that emits deprecation warnings. * @author Toru Nagashima */ -"use strict"; //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ -const path = require("path"); +import path from "path"; //------------------------------------------------------------------------------ // Private @@ -59,6 +58,6 @@ function emitDeprecationWarning(source, errorCode) { // Public Interface //------------------------------------------------------------------------------ -module.exports = { +export { emitDeprecationWarning }; diff --git a/lib/shared/naming.js b/lib/shared/naming.js index 32cff945..93df5fc4 100644 --- a/lib/shared/naming.js +++ b/lib/shared/naming.js @@ -1,7 +1,6 @@ /** * @fileoverview Common helpers for naming of plugins, formatters and configs */ -"use strict"; const NAMESPACE_REGEX = /^@.*\//iu; @@ -90,7 +89,7 @@ function getNamespaceFromTerm(term) { // Public Interface //------------------------------------------------------------------------------ -module.exports = { +export { normalizePackageName, getShorthandName, getNamespaceFromTerm diff --git a/lib/shared/relative-module-resolver.js b/lib/shared/relative-module-resolver.js index 80335c5c..7bce10b5 100644 --- a/lib/shared/relative-module-resolver.js +++ b/lib/shared/relative-module-resolver.js @@ -3,42 +3,41 @@ * @author Teddy Katz */ -"use strict"; - -const Module = require("module"); +import Module from "module"; /* * `Module.createRequire` is added in v12.2.0. It supports URL as well. * We only support the case where the argument is a filepath, not a URL. */ -// eslint-disable-next-line node/no-unsupported-features/node-builtins, node/no-deprecated-api +// eslint-disable-next-line node/no-deprecated-api const createRequire = Module.createRequire || Module.createRequireFromPath; -module.exports = { - - /** - * Resolves a Node module relative to another module - * @param {string} moduleName The name of a Node module, or a path to a Node module. - * @param {string} relativeToPath An absolute path indicating the module that `moduleName` should be resolved relative to. This must be - * a file rather than a directory, but the file need not actually exist. - * @returns {string} The absolute path that would result from calling `require.resolve(moduleName)` in a file located at `relativeToPath` - */ - resolve(moduleName, relativeToPath) { - try { - return createRequire(relativeToPath).resolve(moduleName); - } catch (error) { +/** + * Resolves a Node module relative to another module + * @param {string} moduleName The name of a Node module, or a path to a Node module. + * @param {string} relativeToPath An absolute path indicating the module that `moduleName` should be resolved relative to. This must be + * a file rather than a directory, but the file need not actually exist. + * @returns {string} The absolute path that would result from calling `require.resolve(moduleName)` in a file located at `relativeToPath` + */ +function resolve(moduleName, relativeToPath) { + try { + return createRequire(relativeToPath).resolve(moduleName); + } catch (error) { - // This `if` block is for older Node.js than 12.0.0. We can remove this block in the future. - if ( - typeof error === "object" && - error !== null && - error.code === "MODULE_NOT_FOUND" && - !error.requireStack && - error.message.includes(moduleName) - ) { - error.message += `\nRequire stack:\n- ${relativeToPath}`; - } - throw error; + // This `if` block is for older Node.js than 12.0.0. We can remove this block in the future. + if ( + typeof error === "object" && + error !== null && + error.code === "MODULE_NOT_FOUND" && + !error.requireStack && + error.message.includes(moduleName) + ) { + error.message += `\nRequire stack:\n- ${relativeToPath}`; } + throw error; } +} + +export { + resolve }; diff --git a/lib/shared/types.js b/lib/shared/types.js index d4ac14e2..a32c35e3 100644 --- a/lib/shared/types.js +++ b/lib/shared/types.js @@ -2,10 +2,9 @@ * @fileoverview Define common types for input completion. * @author Toru Nagashima */ -"use strict"; /** @type {any} */ -module.exports = {}; +export default {}; /** @typedef {boolean | "off" | "readable" | "readonly" | "writable" | "writeable"} GlobalConf */ /** @typedef {0 | 1 | 2 | "off" | "warn" | "error"} SeverityConf */ diff --git a/package.json b/package.json index 65c216c6..162530d8 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,12 @@ "name": "@eslint/eslintrc", "version": "0.4.2", "description": "The legacy ESLintRC config file format for ESLint", + "type": "module", "main": "lib/index.js", + "exports": { + "import": "./lib/index.js", + "require": "./dist/eslintrc.cjs" + }, "files": [ "lib", "conf", @@ -12,6 +17,8 @@ "access": "public" }, "scripts": { + "prepare": "npm run build", + "build": "rollup -c", "lint": "eslint . --report-unused-disable-directives", "fix": "npm run lint -- --fix", "test": "mocha -R progress -c 'tests/lib/**/*.js'", @@ -34,16 +41,17 @@ }, "homepage": "https://github.com/eslint/eslintrc#readme", "devDependencies": { - "chai": "^4.2.0", - "eslint": "^7.21.0", + "chai": "^4.3.4", + "eslint": "^7.29.0", "eslint-config-eslint": "^7.0.0", - "eslint-plugin-jsdoc": "^32.2.0", + "eslint-plugin-jsdoc": "^35.3.2", "eslint-plugin-node": "^11.1.0", "eslint-release": "^3.1.2", "fs-teardown": "0.1.1", - "mocha": "^8.1.1", + "mocha": "^9.0.1", + "rollup": "^2.52.1", "shelljs": "^0.8.4", - "sinon": "^9.2.0", + "sinon": "^11.1.1", "temp-dir": "^2.0.0" }, "dependencies": { @@ -58,6 +66,6 @@ "strip-json-comments": "^3.1.1" }, "engines": { - "node": "^10.12.0 || >=12.0.0" + "node": "^12.22.0 || ^14.17.0 || >=16.0.0" } } diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 00000000..3c54e1eb --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,9 @@ +export default { + input: "./lib/index.js", + external: ["fs"], + output: { + exports: "default", + format: "cjs", + file: "dist/eslintrc.cjs" + } +}; diff --git a/tests/_utils/index.js b/tests/_utils/index.js index 1a78e855..b0216531 100644 --- a/tests/_utils/index.js +++ b/tests/_utils/index.js @@ -2,13 +2,11 @@ * @fileoverview Utilities used in tests */ -"use strict"; - //----------------------------------------------------------------------------- // Requirements //----------------------------------------------------------------------------- -const { createTeardown, addFile } = require("fs-teardown"); +import { createTeardown, addFile } from "fs-teardown"; //----------------------------------------------------------------------------- // Helpers @@ -52,7 +50,7 @@ function createCustomTeardown({ cwd, files = {} }) { // Exports //----------------------------------------------------------------------------- -module.exports = { +export { unIndent, createCustomTeardown }; diff --git a/tests/lib/cascading-config-array-factory.js b/tests/lib/cascading-config-array-factory.js index ca931246..2a92dc36 100644 --- a/tests/lib/cascading-config-array-factory.js +++ b/tests/lib/cascading-config-array-factory.js @@ -2,28 +2,30 @@ * @fileoverview Tests for CascadingConfigArrayFactory class. * @author Toru Nagashima */ -"use strict"; //----------------------------------------------------------------------------- // Requirements //----------------------------------------------------------------------------- -const fs = require("fs"); -const path = require("path"); -const os = require("os"); -const { assert } = require("chai"); -const sh = require("shelljs"); -const sinon = require("sinon"); -const systemTempDir = require("temp-dir"); +import fs from "fs"; +import path from "path"; +import { fileURLToPath } from "url"; +import os from "os"; +import { assert } from "chai"; +import sh from "shelljs"; +import sinon from "sinon"; +import systemTempDir from "temp-dir"; + +import { Legacy } from "../../lib/index.js"; +import { createCustomTeardown } from "../_utils/index.js"; + +const dirname = path.dirname(fileURLToPath(import.meta.url)); const { - Legacy: { - ConfigArrayFactory, - CascadingConfigArrayFactory, - ExtractedConfig - } -} = require("../../lib/"); -const { createCustomTeardown } = require("../_utils"); + ConfigArrayFactory, + CascadingConfigArrayFactory, + ExtractedConfig +} = Legacy; //----------------------------------------------------------------------------- // Helpers @@ -36,8 +38,8 @@ const cwdIgnorePatterns = new ConfigArrayFactory() .ignorePattern .patterns; -const eslintAllPath = path.resolve(__dirname, "../fixtures/eslint-all.js"); -const eslintRecommendedPath = path.resolve(__dirname, "../fixtures/eslint-recommended.js"); +const eslintAllPath = path.resolve(dirname, "../fixtures/eslint-all.js"); +const eslintRecommendedPath = path.resolve(dirname, "../fixtures/eslint-recommended.js"); //----------------------------------------------------------------------------- // Tests @@ -152,7 +154,7 @@ describe("CascadingConfigArrayFactory", () => { beforeEach(() => { uniqueHomeDirName = `home_${++uid}`; - homeDir = path.join(__dirname, `../../../${uniqueHomeDirName}`); + homeDir = path.join(dirname, `../../../${uniqueHomeDirName}`); warnings = []; sinon.stub(os, "homedir").returns(homeDir); process.on("warning", onWarning); @@ -530,7 +532,9 @@ describe("CascadingConfigArrayFactory", () => { // hack to avoid needing to hand-rewrite file-structure.json const DIRECTORY_CONFIG_HIERARCHY = (() => { - const rawData = require("../fixtures/config-hierarchy/file-structure.json"); + const rawData = JSON.parse( + fs.readFileSync(new URL("../fixtures/config-hierarchy/file-structure.json", import.meta.url)) + ); // key is path, value is file content (string) const flattened = {}; @@ -647,7 +651,7 @@ describe("CascadingConfigArrayFactory", () => { it("should create config object when using baseConfig with extends", () => { const customBaseConfig = { - extends: path.resolve(__dirname, "../fixtures/config-extends/array/.eslintrc") + extends: path.resolve(dirname, "../fixtures/config-extends/array/.eslintrc") }; const factory = new CascadingConfigArrayFactory({ cwd: fixtureDir, @@ -682,8 +686,8 @@ describe("CascadingConfigArrayFactory", () => { }); it("should not retain configs from previous directories when called multiple times", () => { - const firstpath = path.resolve(__dirname, "../fixtures/configurations/single-quotes/subdir/.eslintrc"); - const secondpath = path.resolve(__dirname, "../fixtures/configurations/single-quotes/.eslintrc"); + const firstpath = path.resolve(dirname, "../fixtures/configurations/single-quotes/subdir/.eslintrc"); + const secondpath = path.resolve(dirname, "../fixtures/configurations/single-quotes/.eslintrc"); const factory = new CascadingConfigArrayFactory({ eslintAllPath, eslintRecommendedPath @@ -697,7 +701,7 @@ describe("CascadingConfigArrayFactory", () => { }); it("should throw error when a configuration file doesn't exist", () => { - const configPath = path.resolve(__dirname, "../fixtures/configurations/.eslintrc"); + const configPath = path.resolve(dirname, "../fixtures/configurations/.eslintrc"); const factory = new CascadingConfigArrayFactory({ eslintAllPath, eslintRecommendedPath @@ -727,7 +731,7 @@ describe("CascadingConfigArrayFactory", () => { }); it("should cache config when the same directory is passed twice", () => { - const configPath = path.resolve(__dirname, "../fixtures/configurations/single-quotes/.eslintrc"); + const configPath = path.resolve(dirname, "../fixtures/configurations/single-quotes/.eslintrc"); const configArrayFactory = new ConfigArrayFactory(); const factory = new CascadingConfigArrayFactory({ configArrayFactory, @@ -748,7 +752,7 @@ describe("CascadingConfigArrayFactory", () => { // make sure JS-style comments don't throw an error it("should load the config file when there are JS-style comments in the text", () => { - const specificConfigPath = path.resolve(__dirname, "../fixtures/configurations/comments.json"); + const specificConfigPath = path.resolve(dirname, "../fixtures/configurations/comments.json"); const factory = new CascadingConfigArrayFactory({ specificConfigPath, useEslintrc: false, @@ -764,7 +768,7 @@ describe("CascadingConfigArrayFactory", () => { // make sure YAML files work correctly it("should load the config file when a YAML file is used", () => { - const specificConfigPath = path.resolve(__dirname, "../fixtures/configurations/env-browser.yaml"); + const specificConfigPath = path.resolve(dirname, "../fixtures/configurations/env-browser.yaml"); const factory = new CascadingConfigArrayFactory({ specificConfigPath, useEslintrc: false, @@ -779,7 +783,7 @@ describe("CascadingConfigArrayFactory", () => { }); it("should contain the correct value for parser when a custom parser is specified", () => { - const configPath = path.resolve(__dirname, "../fixtures/configurations/parser/.eslintrc.json"); + const configPath = path.resolve(dirname, "../fixtures/configurations/parser/.eslintrc.json"); const factory = new CascadingConfigArrayFactory({ eslintAllPath, eslintRecommendedPath @@ -1176,7 +1180,7 @@ describe("CascadingConfigArrayFactory", () => { it("should load user config globals", () => { - const configPath = path.resolve(__dirname, "../fixtures/globals/conf.yaml"); + const configPath = path.resolve(dirname, "../fixtures/globals/conf.yaml"); const factory = new CascadingConfigArrayFactory({ specificConfigPath: configPath, useEslintrc: false, @@ -1195,7 +1199,7 @@ describe("CascadingConfigArrayFactory", () => { }); it("should not load disabled environments", () => { - const configPath = path.resolve(__dirname, "../fixtures/environments/disable.yaml"); + const configPath = path.resolve(dirname, "../fixtures/environments/disable.yaml"); const factory = new CascadingConfigArrayFactory({ specificConfigPath: configPath, useEslintrc: false, @@ -1208,19 +1212,19 @@ describe("CascadingConfigArrayFactory", () => { }); it("should gracefully handle empty files", () => { - const configPath = path.resolve(__dirname, "../fixtures/configurations/env-node.json"); + const configPath = path.resolve(dirname, "../fixtures/configurations/env-node.json"); const factory = new CascadingConfigArrayFactory({ specificConfigPath: configPath, eslintAllPath, eslintRecommendedPath }); - getConfig(factory, path.resolve(__dirname, "../fixtures/configurations/empty/empty.json")); + getConfig(factory, path.resolve(dirname, "../fixtures/configurations/empty/empty.json")); }); // Meaningful stack-traces it("should include references to where an `extends` configuration was loaded from", () => { - const configPath = path.resolve(__dirname, "../fixtures/config-extends/error.json"); + const configPath = path.resolve(dirname, "../fixtures/config-extends/error.json"); assert.throws(() => { const factory = new CascadingConfigArrayFactory({ @@ -1236,7 +1240,7 @@ describe("CascadingConfigArrayFactory", () => { // Keep order with the last array element taking highest precedence it("should make the last element in an array take the highest precedence", () => { - const configPath = path.resolve(__dirname, "../fixtures/config-extends/array/.eslintrc"); + const configPath = path.resolve(dirname, "../fixtures/config-extends/array/.eslintrc"); const factory = new CascadingConfigArrayFactory({ useEslintrc: false, specificConfigPath: configPath, @@ -1562,7 +1566,7 @@ describe("CascadingConfigArrayFactory", () => { }); it("should not merge override config when the pattern matches the absolute file path", () => { - const resolvedPath = path.resolve(__dirname, "../fixtures/config-hierarchy/overrides/bar.js"); + const resolvedPath = path.resolve(dirname, "../fixtures/config-hierarchy/overrides/bar.js"); assert.throws(() => new CascadingConfigArrayFactory({ cwd: getPath(), @@ -1790,7 +1794,7 @@ describe("CascadingConfigArrayFactory", () => { }); describe("deprecation warnings", () => { - const cwd = path.resolve(__dirname, "../fixtures/config-file/"); + const cwd = path.resolve(dirname, "../fixtures/config-file/"); let warning = null; /** diff --git a/tests/lib/config-array-factory.js b/tests/lib/config-array-factory.js index 8f4a29ef..4c2d3a6e 100644 --- a/tests/lib/config-array-factory.js +++ b/tests/lib/config-array-factory.js @@ -2,33 +2,40 @@ * @fileoverview Tests for ConfigArrayFactory class. * @author Toru Nagashima */ -"use strict"; //----------------------------------------------------------------------------- // Requirements //----------------------------------------------------------------------------- -const path = require("path"); -const fs = require("fs"); -const { assert } = require("chai"); -const { spy } = require("sinon"); +import path from "path"; +import { fileURLToPath } from "url"; +import fs from "fs"; +import { createRequire } from "module"; +import { assert } from "chai"; +import sinon from "sinon"; +import { Legacy } from "../../lib/index.js"; +import { createCustomTeardown } from "../_utils/index.js"; +import systemTempDir from "temp-dir"; + +const require = createRequire(import.meta.url); + +const fileName = fileURLToPath(import.meta.url); +const dirname = path.dirname(fileName); +const { spy } = sinon; + const { - Legacy: { - ConfigArray, - ConfigArrayFactory, - OverrideTester, - createConfigArrayFactoryContext: createContext - } -} = require("../../lib/"); -const { createCustomTeardown } = require("../_utils"); -const systemTempDir = require("temp-dir"); + ConfigArray, + ConfigArrayFactory, + OverrideTester, + createConfigArrayFactoryContext: createContext +} = Legacy; //----------------------------------------------------------------------------- // Helpers //----------------------------------------------------------------------------- -const eslintAllPath = path.resolve(__dirname, "../fixtures/eslint-all.js"); -const eslintRecommendedPath = path.resolve(__dirname, "../fixtures/eslint-recommended.js"); +const eslintAllPath = path.resolve(dirname, "../fixtures/eslint-all.js"); +const eslintRecommendedPath = path.resolve(dirname, "../fixtures/eslint-recommended.js"); const tempDir = path.join(systemTempDir, "eslintrc/config-array-factory"); /** @@ -133,7 +140,7 @@ describe("ConfigArrayFactory", () => { it("should call '_normalizeConfigData(configData, ctx)' with given arguments.", () => { const configData = {}; const basePath = tempDir; - const filePath = __filename; + const filePath = fileName; const name = "example"; const normalizeConfigData = spy(factory, "_normalizeConfigData"); @@ -910,11 +917,11 @@ describe("ConfigArrayFactory", () => { assert.strictEqual(configArray.length, 2); }); - it("should have the config data of 'eslint:all' at the first element.", () => { + it("should have the config data of 'eslint:all' at the first element.", async () => { assertConfigArrayElement(configArray[0], { name: ".eslintrc » eslint:all", filePath: eslintAllPath, - ...require(eslintAllPath) + ...(await import(eslintAllPath)).default }); }); @@ -940,11 +947,11 @@ describe("ConfigArrayFactory", () => { assert.strictEqual(configArray.length, 2); }); - it("should have the config data of 'eslint:recommended' at the first element.", () => { + it("should have the config data of 'eslint:recommended' at the first element.", async () => { assertConfigArrayElement(configArray[0], { name: ".eslintrc » eslint:recommended", filePath: eslintRecommendedPath, - ...require(eslintRecommendedPath) + ...(await import(eslintRecommendedPath)).default }); }); @@ -1552,10 +1559,12 @@ describe("ConfigArrayFactory", () => { }, /Failed to load parser 'nonexistent-parser' declared in 'whatever » plugin:invalid-parser\/foo'/u); }); - it("should fall back to default parser when a parser called 'espree' is not found", () => { + it("should fall back to default parser when a parser called 'espree' is not found", async () => { const config = applyExtends({ parser: "espree" }); assertConfig(config, { + + // parser: await import.meta.resolve("espree") parser: require.resolve("espree") }); }); @@ -1893,6 +1902,8 @@ describe("ConfigArrayFactory", () => { const config = load(factory, "js/.eslintrc.parser3.js"); assertConfig(config, { + + // parser: await import.meta.resolve("espree"), parser: require.resolve("espree"), rules: { semi: [2, "always"] diff --git a/tests/lib/config-array/config-array.js b/tests/lib/config-array/config-array.js index 2a38005e..d0808ee8 100644 --- a/tests/lib/config-array/config-array.js +++ b/tests/lib/config-array/config-array.js @@ -2,11 +2,14 @@ * @fileoverview Tests for ConfigArray class. * @author Toru Nagashima */ -"use strict"; -const path = require("path"); -const { assert } = require("chai"); -const { ConfigArray, OverrideTester, getUsedExtractedConfigs } = require("../../../lib/config-array"); +import path from "path"; +import { fileURLToPath } from "url"; +import { assert } from "chai"; +import { ConfigArray, OverrideTester, getUsedExtractedConfigs } from "../../../lib/config-array/index.js"; + +const filename = fileURLToPath(import.meta.url); +const dirname = path.dirname(filename); describe("ConfigArray", () => { it("should be a sub class of Array.", () => { @@ -227,7 +230,7 @@ describe("ConfigArray", () => { { parser: { error: new Error("Failed to load a parser.") } } - ).extractConfig(__filename); + ).extractConfig(filename); }, "Failed to load a parser."); }); @@ -240,7 +243,7 @@ describe("ConfigArray", () => { { parser } - ).extractConfig(__filename); + ).extractConfig(filename); assert.strictEqual(config.parser, parser); }); @@ -251,7 +254,7 @@ describe("ConfigArray", () => { criteria: OverrideTester.create(["*.ts"], [], process.cwd()), parser: { error: new Error("Failed to load a parser.") } } - ).extractConfig(__filename); + ).extractConfig(filename); assert.strictEqual(config.parser, null); }); @@ -264,7 +267,7 @@ describe("ConfigArray", () => { foo: { error: new Error("Failed to load a plugin.") } } } - ).extractConfig(__filename); + ).extractConfig(filename); }, "Failed to load a plugin."); }); @@ -276,7 +279,7 @@ describe("ConfigArray", () => { foo: { error: new Error("Failed to load a plugin.") } } } - ).extractConfig(__filename); + ).extractConfig(filename); assert.deepStrictEqual(config.plugins, {}); }); @@ -295,7 +298,7 @@ describe("ConfigArray", () => { } }, { - criteria: OverrideTester.create(["*.js"], [path.basename(__filename)], process.cwd()), + criteria: OverrideTester.create(["*.js"], [path.basename(filename)], process.cwd()), rules: { "no-use-before-define": "error" } @@ -306,7 +309,7 @@ describe("ConfigArray", () => { "no-unused-vars": "error" } } - ).extractConfig(__filename); + ).extractConfig(filename); assert.deepStrictEqual(config.rules, { "no-redeclare": ["error"], @@ -328,7 +331,7 @@ describe("ConfigArray", () => { } }, { - criteria: OverrideTester.create(["*.js"], [path.basename(__filename)], process.cwd()), + criteria: OverrideTester.create(["*.js"], [path.basename(filename)], process.cwd()), rules: { "no-use-before-define": "error" } @@ -342,8 +345,8 @@ describe("ConfigArray", () => { ); assert.strictEqual( - configArray.extractConfig(path.join(__dirname, "a.js")), - configArray.extractConfig(path.join(__dirname, "b.js")) + configArray.extractConfig(path.join(dirname, "a.js")), + configArray.extractConfig(path.join(dirname, "b.js")) ); }); @@ -362,7 +365,7 @@ describe("ConfigArray", () => { * @returns {Object} The merged config data. */ function merge(target, source) { - return new ConfigArray(target, source).extractConfig(__filename); + return new ConfigArray(target, source).extractConfig(filename); } it("should combine two objects when passed two objects with different top-level properties", () => { @@ -676,7 +679,7 @@ describe("ConfigArray", () => { } }, { - criteria: OverrideTester.create(["*.js"], [path.basename(__filename)], process.cwd()), + criteria: OverrideTester.create(["*.js"], [path.basename(filename)], process.cwd()), rules: { "no-use-before-define": "error" } @@ -695,9 +698,9 @@ describe("ConfigArray", () => { }); for (const { filePaths } of [ - { filePaths: [__filename] }, - { filePaths: [__filename, `${__filename}.ts`] }, - { filePaths: [__filename, `${__filename}.ts`, path.join(__dirname, "foo.js")] } + { filePaths: [filename] }, + { filePaths: [filename, `${filename}.ts`] }, + { filePaths: [filename, `${filename}.ts`, path.join(dirname, "foo.js")] } ]) { describe(`after it called 'extractConfig(filePath)' ${filePaths.length} time(s) with ${JSON.stringify(filePaths, null, 4)}, the returned array`, () => { // eslint-disable-line no-loop-func let configs; @@ -723,13 +726,13 @@ describe("ConfigArray", () => { it("should not contain duplicate values.", () => { // Call some times, including with the same arguments. - configArray.extractConfig(__filename); - configArray.extractConfig(`${__filename}.ts`); - configArray.extractConfig(path.join(__dirname, "foo.js")); - configArray.extractConfig(__filename); - configArray.extractConfig(path.join(__dirname, "foo.js")); - configArray.extractConfig(path.join(__dirname, "bar.js")); - configArray.extractConfig(path.join(__dirname, "baz.js")); + configArray.extractConfig(filename); + configArray.extractConfig(`${filename}.ts`); + configArray.extractConfig(path.join(dirname, "foo.js")); + configArray.extractConfig(filename); + configArray.extractConfig(path.join(dirname, "foo.js")); + configArray.extractConfig(path.join(dirname, "bar.js")); + configArray.extractConfig(path.join(dirname, "baz.js")); const usedConfigs = getUsedExtractedConfigs(configArray); diff --git a/tests/lib/config-array/config-dependency.js b/tests/lib/config-array/config-dependency.js index 94a54638..3087b8ca 100644 --- a/tests/lib/config-array/config-dependency.js +++ b/tests/lib/config-array/config-dependency.js @@ -2,12 +2,11 @@ * @fileoverview Tests for ConfigDependency class. * @author Toru Nagashima */ -"use strict"; -const assert = require("assert"); -const { Console } = require("console"); -const { Writable } = require("stream"); -const { ConfigDependency } = require("../../../lib/config-array/config-dependency"); +import assert from "assert"; +import { Console } from "console"; +import { Writable } from "stream"; +import { ConfigDependency } from "../../../lib/config-array/config-dependency.js"; describe("ConfigDependency", () => { describe("'constructor(data)' should initialize properties.", () => { diff --git a/tests/lib/config-array/extracted-config.js b/tests/lib/config-array/extracted-config.js index 9bbe7ed7..cf2a8c71 100644 --- a/tests/lib/config-array/extracted-config.js +++ b/tests/lib/config-array/extracted-config.js @@ -2,10 +2,9 @@ * @fileoverview Tests for ExtractedConfig class. * @author Toru Nagashima */ -"use strict"; -const assert = require("assert"); -const { ExtractedConfig } = require("../../../lib/config-array/extracted-config"); +import assert from "assert"; +import { ExtractedConfig } from "../../../lib/config-array/extracted-config.js"; describe("'ExtractedConfig' class", () => { describe("'constructor()' should create an instance.", () => { diff --git a/tests/lib/config-array/ignore-pattern.js b/tests/lib/config-array/ignore-pattern.js index 8486cf4a..9be97452 100644 --- a/tests/lib/config-array/ignore-pattern.js +++ b/tests/lib/config-array/ignore-pattern.js @@ -2,12 +2,11 @@ * @fileoverview Tests for IgnorePattern class. * @author Toru Nagashima */ -"use strict"; -const assert = require("assert"); -const path = require("path"); -const sinon = require("sinon"); -const { IgnorePattern } = require("../../../lib/config-array/ignore-pattern"); +import assert from "assert"; +import path from "path"; +import sinon from "sinon"; +import { IgnorePattern } from "../../../lib/config-array/ignore-pattern.js"; describe("IgnorePattern", () => { describe("constructor(patterns, basePath)", () => { diff --git a/tests/lib/config-array/override-tester.js b/tests/lib/config-array/override-tester.js index 6105d454..ba54cfe5 100644 --- a/tests/lib/config-array/override-tester.js +++ b/tests/lib/config-array/override-tester.js @@ -2,13 +2,12 @@ * @fileoverview Tests for OverrideTester class. * @author Toru Nagashima */ -"use strict"; -const assert = require("assert"); -const { Console } = require("console"); -const path = require("path"); -const { Writable } = require("stream"); -const { OverrideTester } = require("../../../lib/config-array/override-tester"); +import assert from "assert"; +import { Console } from "console"; +import path from "path"; +import { Writable } from "stream"; +import { OverrideTester } from "../../../lib/config-array/override-tester.js"; describe("OverrideTester", () => { describe("'create(files, excludedFiles, basePath)' should create a tester.", () => { diff --git a/tests/lib/flat-compat.js b/tests/lib/flat-compat.js index 968e112f..8bc3bb4a 100644 --- a/tests/lib/flat-compat.js +++ b/tests/lib/flat-compat.js @@ -2,22 +2,24 @@ * @fileoverview Tests for FlatCompat class. * @author Nicholas C. Zakas */ -"use strict"; //----------------------------------------------------------------------------- // Requirements //----------------------------------------------------------------------------- -const path = require("path"); -const { assert } = require("chai"); -const { FlatCompat } = require("../../lib"); -const environments = require("../../conf/environments"); +import path from "path"; +import { fileURLToPath } from "url"; +import { assert } from "chai"; +import { FlatCompat } from "../../lib/index.js"; +import environments from "../../conf/environments.cjs"; + +const dirname = path.dirname(fileURLToPath(import.meta.url)); //----------------------------------------------------------------------------- // Helpers //----------------------------------------------------------------------------- -const FIXTURES_BASE_PATH = path.resolve(__dirname, "../fixtures/flat-compat/"); +const FIXTURES_BASE_PATH = path.resolve(dirname, "../fixtures/flat-compat/"); /** * Normalizes a plugin object to have all available keys. This matches what @@ -50,13 +52,13 @@ function getFixturePath(dirName) { describe("FlatCompat", () => { - describe("config()", () => { + describe("config()", async () => { let compat; const baseDirectory = getFixturePath("config"); - const pluginFixture1 = normalizePlugin(require(path.join(baseDirectory, "node_modules/eslint-plugin-fixture1"))); - const pluginFixture2 = normalizePlugin(require(path.join(baseDirectory, "node_modules/eslint-plugin-fixture2"))); - const pluginFixture3 = normalizePlugin(require(path.join(baseDirectory, "node_modules/eslint-plugin-fixture3"))); + const pluginFixture1 = normalizePlugin((await import(path.join(baseDirectory, "node_modules/eslint-plugin-fixture1.js"))).default); + const pluginFixture2 = normalizePlugin((await import(path.join(baseDirectory, "node_modules/eslint-plugin-fixture2.js"))).default); + const pluginFixture3 = normalizePlugin((await import(path.join(baseDirectory, "node_modules/eslint-plugin-fixture3.js"))).default); beforeEach(() => { compat = new FlatCompat({ @@ -693,7 +695,7 @@ describe("FlatCompat", () => { assert.notStrictEqual(result[0].languageOptions.parserOptions, parserOptions); }); - it("should translate parser string into an object", () => { + it("should translate parser string into an object", async () => { const result = compat.config({ parser: "my-parser" }); @@ -701,7 +703,7 @@ describe("FlatCompat", () => { assert.strictEqual(result.length, 1); assert.deepStrictEqual(result[0], { languageOptions: { - parser: require(getFixturePath("config/node_modules/my-parser")) + parser: (await import(getFixturePath("config/node_modules/my-parser.js"))).default } }); }); @@ -916,7 +918,7 @@ describe("FlatCompat", () => { }); }); - it("should translate plugins without processors", () => { + it("should translate plugins without processors", async () => { const result = compat.plugins("fixture1"); assert.strictEqual(result.length, 1); @@ -927,7 +929,7 @@ describe("FlatCompat", () => { rules: {}, environments: {}, processors: {}, - ...require(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture1")) + ...(await import(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture1.js"))).default } } }); @@ -939,9 +941,9 @@ describe("FlatCompat", () => { }, /Failed to load plugin 'missing'/u); }); - it("should translate plugins with processors", () => { + it("should translate plugins with processors", async () => { const result = compat.plugins("fixture2"); - const plugin = require(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture2")); + const plugin = (await import(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture2.js"))).default; assert.strictEqual(result.length, 2); assert.deepStrictEqual(result[0], { @@ -961,9 +963,9 @@ describe("FlatCompat", () => { }); }); - it("should translate multiple plugins", () => { + it("should translate multiple plugins", async () => { const result = compat.plugins("fixture1", "fixture2"); - const plugin = require(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture2")); + const plugin = (await import(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture2.js"))).default; assert.strictEqual(result.length, 2); assert.deepStrictEqual(result[0], { @@ -977,7 +979,7 @@ describe("FlatCompat", () => { rules: {}, environments: {}, processors: {}, - ...require(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture1")) + ...(await import(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture1.js"))).default }, fixture2: { configs: {}, diff --git a/tests/lib/shared/config-ops.js b/tests/lib/shared/config-ops.js index 519d5a2f..574918e3 100644 --- a/tests/lib/shared/config-ops.js +++ b/tests/lib/shared/config-ops.js @@ -2,15 +2,16 @@ * @fileoverview Tests for ConfigOps * @author Nicholas C. Zakas */ -"use strict"; //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ -const assert = require("chai").assert, - util = require("util"), - ConfigOps = require("../../../lib/shared/config-ops"); +import { assert } from "chai"; + +import util from "util"; + +import * as ConfigOps from "../../../lib/shared/config-ops.js"; //------------------------------------------------------------------------------ // Tests diff --git a/tests/lib/shared/naming.js b/tests/lib/shared/naming.js index 84bec232..8bc7f951 100644 --- a/tests/lib/shared/naming.js +++ b/tests/lib/shared/naming.js @@ -1,14 +1,14 @@ /** * @fileoverview Tests for naming util */ -"use strict"; //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ -const assert = require("chai").assert, - naming = require("../../../lib/shared/naming"); +import { assert } from "chai"; + +import * as naming from "../../../lib/shared/naming.js"; //------------------------------------------------------------------------------ // Tests diff --git a/tests/lib/shared/relative-module-resolver.js b/tests/lib/shared/relative-module-resolver.js index 32ebc40b..92eaa6ed 100644 --- a/tests/lib/shared/relative-module-resolver.js +++ b/tests/lib/shared/relative-module-resolver.js @@ -1,15 +1,15 @@ /** * @fileoverview Tests for relative module resolver. */ -"use strict"; + +import { assert } from "chai"; +import path from "path"; + +import { Legacy } from "../../../lib/index.js"; const { - Legacy: { - ModuleResolver - } -} = require("../../.."); -const { assert } = require("chai"); -const path = require("path"); + ModuleResolver +} = Legacy; describe("ModuleResolver", () => { describe("resolve()", () => { From 21dd692bc7fcfdbf7f0bf939b37cfbe1298c84fd Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Fri, 25 Jun 2021 19:27:27 +0800 Subject: [PATCH 02/20] Fix: CJS changes --- tests/fixtures/configurations/parser/.eslintrc.json | 2 +- .../fixtures/configurations/parser/{custom.js => custom.cjs} | 0 tests/fixtures/{eslint-all.js => eslint-all.cjs} | 0 .../{eslint-recommended.js => eslint-recommended.cjs} | 0 tests/fixtures/rules/.jshintignore | 1 - tests/lib/cascading-config-array-factory.js | 4 ++-- tests/lib/config-array-factory.js | 4 ++-- 7 files changed, 5 insertions(+), 6 deletions(-) rename tests/fixtures/configurations/parser/{custom.js => custom.cjs} (100%) rename tests/fixtures/{eslint-all.js => eslint-all.cjs} (100%) rename tests/fixtures/{eslint-recommended.js => eslint-recommended.cjs} (100%) delete mode 100644 tests/fixtures/rules/.jshintignore diff --git a/tests/fixtures/configurations/parser/.eslintrc.json b/tests/fixtures/configurations/parser/.eslintrc.json index 4f8c8f57..ac742521 100644 --- a/tests/fixtures/configurations/parser/.eslintrc.json +++ b/tests/fixtures/configurations/parser/.eslintrc.json @@ -1,6 +1,6 @@ { "root": true, - "parser": "./custom.js", + "parser": "./custom.cjs", "rules": { "quotes": ["error", "double"] } diff --git a/tests/fixtures/configurations/parser/custom.js b/tests/fixtures/configurations/parser/custom.cjs similarity index 100% rename from tests/fixtures/configurations/parser/custom.js rename to tests/fixtures/configurations/parser/custom.cjs diff --git a/tests/fixtures/eslint-all.js b/tests/fixtures/eslint-all.cjs similarity index 100% rename from tests/fixtures/eslint-all.js rename to tests/fixtures/eslint-all.cjs diff --git a/tests/fixtures/eslint-recommended.js b/tests/fixtures/eslint-recommended.cjs similarity index 100% rename from tests/fixtures/eslint-recommended.js rename to tests/fixtures/eslint-recommended.cjs diff --git a/tests/fixtures/rules/.jshintignore b/tests/fixtures/rules/.jshintignore deleted file mode 100644 index 91224e5d..00000000 --- a/tests/fixtures/rules/.jshintignore +++ /dev/null @@ -1 +0,0 @@ -**/* diff --git a/tests/lib/cascading-config-array-factory.js b/tests/lib/cascading-config-array-factory.js index 2a92dc36..e9ffe6d5 100644 --- a/tests/lib/cascading-config-array-factory.js +++ b/tests/lib/cascading-config-array-factory.js @@ -39,7 +39,7 @@ const cwdIgnorePatterns = new ConfigArrayFactory() .patterns; const eslintAllPath = path.resolve(dirname, "../fixtures/eslint-all.js"); -const eslintRecommendedPath = path.resolve(dirname, "../fixtures/eslint-recommended.js"); +const eslintRecommendedPath = path.resolve(dirname, "../fixtures/eslint-recommended.cjs"); //----------------------------------------------------------------------------- // Tests @@ -790,7 +790,7 @@ describe("CascadingConfigArrayFactory", () => { }); const config = getConfig(factory, configPath); - assert.strictEqual(config.parser, path.resolve(path.dirname(configPath), "./custom.js")); + assert.strictEqual(config.parser, path.resolve(path.dirname(configPath), "./custom.cjs")); }); /* diff --git a/tests/lib/config-array-factory.js b/tests/lib/config-array-factory.js index 4c2d3a6e..4dbd1d3c 100644 --- a/tests/lib/config-array-factory.js +++ b/tests/lib/config-array-factory.js @@ -34,8 +34,8 @@ const { // Helpers //----------------------------------------------------------------------------- -const eslintAllPath = path.resolve(dirname, "../fixtures/eslint-all.js"); -const eslintRecommendedPath = path.resolve(dirname, "../fixtures/eslint-recommended.js"); +const eslintAllPath = path.resolve(dirname, "../fixtures/eslint-all.cjs"); +const eslintRecommendedPath = path.resolve(dirname, "../fixtures/eslint-recommended.cjs"); const tempDir = path.join(systemTempDir, "eslintrc/config-array-factory"); /** From 48cc8bb2d77103e78fc4009397d91137e34d5b96 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Fri, 25 Jun 2021 19:56:30 +0800 Subject: [PATCH 03/20] 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 --- .../config-file/extends-chain-2/{parser.js => parser.cjs} | 0 .../js/{.eslintrc.broken.js => .eslintrc.broken.cjs} | 0 tests/fixtures/config-file/js/{.eslintrc.js => .eslintrc.cjs} | 0 .../js/{.eslintrc.parser.js => .eslintrc.parser.cjs} | 0 .../js/{.eslintrc.parser2.js => .eslintrc.parser2.cjs} | 0 .../js/{.eslintrc.parser3.js => .eslintrc.parser3.cjs} | 0 .../config-hierarchy/fileexts/{.eslintrc.js => .eslintrc.cjs} | 0 tests/fixtures/config-rule/{schemas.js => schemas.cjs} | 0 tests/fixtures/rules/dir1/{no-strings.js => no-strings.cjs} | 0 tests/fixtures/rules/dir2/{no-literals.js => no-literals.cjs} | 0 .../rules/fix-types-test/{no-program.js => no-program.cjs} | 0 .../{make-syntax-error-rule.js => make-syntax-error-rule.cjs} | 0 .../fixtures/rules/wrong/{custom-rule.js => custom-rule.cjs} | 0 tests/lib/cascading-config-array-factory.js | 4 ++-- 14 files changed, 2 insertions(+), 2 deletions(-) rename tests/fixtures/config-file/extends-chain-2/{parser.js => parser.cjs} (100%) rename tests/fixtures/config-file/js/{.eslintrc.broken.js => .eslintrc.broken.cjs} (100%) rename tests/fixtures/config-file/js/{.eslintrc.js => .eslintrc.cjs} (100%) rename tests/fixtures/config-file/js/{.eslintrc.parser.js => .eslintrc.parser.cjs} (100%) rename tests/fixtures/config-file/js/{.eslintrc.parser2.js => .eslintrc.parser2.cjs} (100%) rename tests/fixtures/config-file/js/{.eslintrc.parser3.js => .eslintrc.parser3.cjs} (100%) rename tests/fixtures/config-hierarchy/fileexts/{.eslintrc.js => .eslintrc.cjs} (100%) rename tests/fixtures/config-rule/{schemas.js => schemas.cjs} (100%) rename tests/fixtures/rules/dir1/{no-strings.js => no-strings.cjs} (100%) rename tests/fixtures/rules/dir2/{no-literals.js => no-literals.cjs} (100%) rename tests/fixtures/rules/fix-types-test/{no-program.js => no-program.cjs} (100%) rename tests/fixtures/rules/{make-syntax-error-rule.js => make-syntax-error-rule.cjs} (100%) rename tests/fixtures/rules/wrong/{custom-rule.js => custom-rule.cjs} (100%) diff --git a/tests/fixtures/config-file/extends-chain-2/parser.js b/tests/fixtures/config-file/extends-chain-2/parser.cjs similarity index 100% rename from tests/fixtures/config-file/extends-chain-2/parser.js rename to tests/fixtures/config-file/extends-chain-2/parser.cjs diff --git a/tests/fixtures/config-file/js/.eslintrc.broken.js b/tests/fixtures/config-file/js/.eslintrc.broken.cjs similarity index 100% rename from tests/fixtures/config-file/js/.eslintrc.broken.js rename to tests/fixtures/config-file/js/.eslintrc.broken.cjs diff --git a/tests/fixtures/config-file/js/.eslintrc.js b/tests/fixtures/config-file/js/.eslintrc.cjs similarity index 100% rename from tests/fixtures/config-file/js/.eslintrc.js rename to tests/fixtures/config-file/js/.eslintrc.cjs diff --git a/tests/fixtures/config-file/js/.eslintrc.parser.js b/tests/fixtures/config-file/js/.eslintrc.parser.cjs similarity index 100% rename from tests/fixtures/config-file/js/.eslintrc.parser.js rename to tests/fixtures/config-file/js/.eslintrc.parser.cjs diff --git a/tests/fixtures/config-file/js/.eslintrc.parser2.js b/tests/fixtures/config-file/js/.eslintrc.parser2.cjs similarity index 100% rename from tests/fixtures/config-file/js/.eslintrc.parser2.js rename to tests/fixtures/config-file/js/.eslintrc.parser2.cjs diff --git a/tests/fixtures/config-file/js/.eslintrc.parser3.js b/tests/fixtures/config-file/js/.eslintrc.parser3.cjs similarity index 100% rename from tests/fixtures/config-file/js/.eslintrc.parser3.js rename to tests/fixtures/config-file/js/.eslintrc.parser3.cjs diff --git a/tests/fixtures/config-hierarchy/fileexts/.eslintrc.js b/tests/fixtures/config-hierarchy/fileexts/.eslintrc.cjs similarity index 100% rename from tests/fixtures/config-hierarchy/fileexts/.eslintrc.js rename to tests/fixtures/config-hierarchy/fileexts/.eslintrc.cjs diff --git a/tests/fixtures/config-rule/schemas.js b/tests/fixtures/config-rule/schemas.cjs similarity index 100% rename from tests/fixtures/config-rule/schemas.js rename to tests/fixtures/config-rule/schemas.cjs diff --git a/tests/fixtures/rules/dir1/no-strings.js b/tests/fixtures/rules/dir1/no-strings.cjs similarity index 100% rename from tests/fixtures/rules/dir1/no-strings.js rename to tests/fixtures/rules/dir1/no-strings.cjs diff --git a/tests/fixtures/rules/dir2/no-literals.js b/tests/fixtures/rules/dir2/no-literals.cjs similarity index 100% rename from tests/fixtures/rules/dir2/no-literals.js rename to tests/fixtures/rules/dir2/no-literals.cjs diff --git a/tests/fixtures/rules/fix-types-test/no-program.js b/tests/fixtures/rules/fix-types-test/no-program.cjs similarity index 100% rename from tests/fixtures/rules/fix-types-test/no-program.js rename to tests/fixtures/rules/fix-types-test/no-program.cjs diff --git a/tests/fixtures/rules/make-syntax-error-rule.js b/tests/fixtures/rules/make-syntax-error-rule.cjs similarity index 100% rename from tests/fixtures/rules/make-syntax-error-rule.js rename to tests/fixtures/rules/make-syntax-error-rule.cjs diff --git a/tests/fixtures/rules/wrong/custom-rule.js b/tests/fixtures/rules/wrong/custom-rule.cjs similarity index 100% rename from tests/fixtures/rules/wrong/custom-rule.js rename to tests/fixtures/rules/wrong/custom-rule.cjs diff --git a/tests/lib/cascading-config-array-factory.js b/tests/lib/cascading-config-array-factory.js index e9ffe6d5..64b70395 100644 --- a/tests/lib/cascading-config-array-factory.js +++ b/tests/lib/cascading-config-array-factory.js @@ -38,7 +38,7 @@ const cwdIgnorePatterns = new ConfigArrayFactory() .ignorePattern .patterns; -const eslintAllPath = path.resolve(dirname, "../fixtures/eslint-all.js"); +const eslintAllPath = path.resolve(dirname, "../fixtures/eslint-all.cjs"); const eslintRecommendedPath = path.resolve(dirname, "../fixtures/eslint-recommended.cjs"); //----------------------------------------------------------------------------- @@ -1566,7 +1566,7 @@ describe("CascadingConfigArrayFactory", () => { }); it("should not merge override config when the pattern matches the absolute file path", () => { - const resolvedPath = path.resolve(dirname, "../fixtures/config-hierarchy/overrides/bar.js"); + const resolvedPath = path.resolve(dirname, "../fixtures/config-hierarchy/overrides/bar.cjs"); assert.throws(() => new CascadingConfigArrayFactory({ cwd: getPath(), From 098614ec469ed297207f3c41aea5fd09c5a92040 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Fri, 25 Jun 2021 20:24:48 +0800 Subject: [PATCH 04/20] Fix: Rollup did not like requiring of default module.exports (without a plugin at least), so create require --- lib/flat-compat.js | 5 ++++- lib/shared/config-validator.js | 8 ++++++-- rollup.config.js | 1 - tests/lib/flat-compat.js | 5 ++++- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/flat-compat.js b/lib/flat-compat.js index 58a0e3b2..fe8d5fc1 100644 --- a/lib/flat-compat.js +++ b/lib/flat-compat.js @@ -9,12 +9,15 @@ import path from "path"; import { fileURLToPath } from "url"; +import { createRequire } from "module"; -import environments from "../conf/environments.cjs"; import createDebug from "debug"; import { ConfigArrayFactory } from "./config-array-factory.js"; const dirname = path.dirname(fileURLToPath(import.meta.url)); +const require = createRequire(import.meta.url); + +const environments = require("../conf/environments.cjs"); //----------------------------------------------------------------------------- // Helpers diff --git a/lib/shared/config-validator.js b/lib/shared/config-validator.js index e0755e97..fe8398b5 100644 --- a/lib/shared/config-validator.js +++ b/lib/shared/config-validator.js @@ -10,12 +10,16 @@ //------------------------------------------------------------------------------ import util from "util"; -import configSchema from "../../conf/config-schema.cjs"; -import BuiltInEnvironments from "../../conf/environments.cjs"; +import { createRequire } from "module"; import * as ConfigOps from "./config-ops.js"; import { emitDeprecationWarning } from "./deprecation-warnings.js"; import ajvOrig from "./ajv.js"; +const require = createRequire(import.meta.url); + +const configSchema = require("../../conf/config-schema.cjs"); +const BuiltInEnvironments = require("../../conf/environments.cjs"); + const ajv = ajvOrig(); const ruleValidators = new WeakMap(); diff --git a/rollup.config.js b/rollup.config.js index 3c54e1eb..d49acad1 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -2,7 +2,6 @@ export default { input: "./lib/index.js", external: ["fs"], output: { - exports: "default", format: "cjs", file: "dist/eslintrc.cjs" } diff --git a/tests/lib/flat-compat.js b/tests/lib/flat-compat.js index 8bc3bb4a..ee671e27 100644 --- a/tests/lib/flat-compat.js +++ b/tests/lib/flat-compat.js @@ -9,11 +9,14 @@ import path from "path"; import { fileURLToPath } from "url"; +import { createRequire } from "module"; import { assert } from "chai"; import { FlatCompat } from "../../lib/index.js"; -import environments from "../../conf/environments.cjs"; const dirname = path.dirname(fileURLToPath(import.meta.url)); +const require = createRequire(import.meta.url); + +const environments = require("../../conf/environments.cjs"); //----------------------------------------------------------------------------- // Helpers From 54238ea88aecd600628287c60a423711efee87ce Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Fri, 25 Jun 2021 21:18:38 +0800 Subject: [PATCH 05/20] New: Add exports; also update devDeps. specify external modules in Rollup config, and align it with eslint-scope --- package.json | 11 +++++++---- rollup.config.js | 9 +++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 162530d8..61f9c9b6 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,11 @@ "type": "module", "main": "lib/index.js", "exports": { - "import": "./lib/index.js", - "require": "./dist/eslintrc.cjs" + ".": { + "import": "./lib/index.js", + "require": "./dist/eslintrc.cjs" + }, + "./package.json": "./package.json" }, "files": [ "lib", @@ -44,12 +47,12 @@ "chai": "^4.3.4", "eslint": "^7.29.0", "eslint-config-eslint": "^7.0.0", - "eslint-plugin-jsdoc": "^35.3.2", + "eslint-plugin-jsdoc": "^35.4.0", "eslint-plugin-node": "^11.1.0", "eslint-release": "^3.1.2", "fs-teardown": "0.1.1", "mocha": "^9.0.1", - "rollup": "^2.52.1", + "rollup": "^2.52.2", "shelljs": "^0.8.4", "sinon": "^11.1.1", "temp-dir": "^2.0.0" diff --git a/rollup.config.js b/rollup.config.js index d49acad1..5ca1329d 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,8 +1,13 @@ export default { input: "./lib/index.js", - external: ["fs"], + external: [ + "module", "util", "os", "path", "debug", "fs", "import-fresh", + "strip-json-comments", "assert", "ignore", "minimatch", "url", "ajv" + ], + treeshake: false, output: { format: "cjs", - file: "dist/eslintrc.cjs" + file: "dist/eslintrc.cjs", + sourcemap: true } }; From 74422b73a08717586bc457b05f3ab3f69947d99a Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Fri, 25 Jun 2021 22:10:28 +0800 Subject: [PATCH 06/20] Fix: Add `dist` file --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 61f9c9b6..ee2999e3 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "files": [ "lib", "conf", - "LICENSE" + "LICENSE", + "dist/eslintrc.cjs" ], "publishConfig": { "access": "public" From a5f67fad98ca570de4b12f2b5d5a8c45af2c5834 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Fri, 25 Jun 2021 22:39:35 +0800 Subject: [PATCH 07/20] Fix: We can use cjs for our final file after all --- .../node_modules/eslint-plugin-example-with-rules-config.js | 2 +- tests/fixtures/rules/{custom-rule.js => custom-rule.cjs} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename tests/fixtures/rules/{custom-rule.js => custom-rule.cjs} (100%) diff --git a/tests/fixtures/config-hierarchy/plugins/node_modules/eslint-plugin-example-with-rules-config.js b/tests/fixtures/config-hierarchy/plugins/node_modules/eslint-plugin-example-with-rules-config.js index b8b846e1..f0cb1df9 100644 --- a/tests/fixtures/config-hierarchy/plugins/node_modules/eslint-plugin-example-with-rules-config.js +++ b/tests/fixtures/config-hierarchy/plugins/node_modules/eslint-plugin-example-with-rules-config.js @@ -1,5 +1,5 @@ module.exports = { - rules: { "example-rule": require("../../../rules/custom-rule") }, + rules: { "example-rule": require("../../../rules/custom-rule.cjs") }, // rulesConfig support removed in 2.0.0, so this should have no effect rulesConfig: { "example-rule": 1 } diff --git a/tests/fixtures/rules/custom-rule.js b/tests/fixtures/rules/custom-rule.cjs similarity index 100% rename from tests/fixtures/rules/custom-rule.js rename to tests/fixtures/rules/custom-rule.cjs From 2b4d3ed599b967b25d60d63e596a8d80c800db38 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sat, 3 Jul 2021 14:05:22 +0800 Subject: [PATCH 08/20] Test: CommonJS --- .eslintrc.cjs | 2 +- lib/shared/config-validator.js | 6 ++-- package.json | 9 +++--- tests/lib/commonjs.cjs | 53 ++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 tests/lib/commonjs.cjs diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 10615da8..cd03debe 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -21,7 +21,7 @@ module.exports = { overrides: [ { - files: [".eslintrc.js", "conf/**"], + files: ["*.cjs", "conf/**"], env: { node: true }, diff --git a/lib/shared/config-validator.js b/lib/shared/config-validator.js index fe8398b5..35c195c4 100644 --- a/lib/shared/config-validator.js +++ b/lib/shared/config-validator.js @@ -9,6 +9,7 @@ // Requirements //------------------------------------------------------------------------------ +import path from "path"; import util from "util"; import { createRequire } from "module"; import * as ConfigOps from "./config-ops.js"; @@ -17,8 +18,9 @@ import ajvOrig from "./ajv.js"; const require = createRequire(import.meta.url); -const configSchema = require("../../conf/config-schema.cjs"); -const BuiltInEnvironments = require("../../conf/environments.cjs"); +// Ensure paths work after Rollup +const configSchema = require(path.resolve("./conf/config-schema.cjs")); +const BuiltInEnvironments = require(path.resolve("./conf/environments.cjs")); const ajv = ajvOrig(); diff --git a/package.json b/package.json index ee2999e3..f2d710f7 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "build": "rollup -c", "lint": "eslint . --report-unused-disable-directives", "fix": "npm run lint -- --fix", - "test": "mocha -R progress -c 'tests/lib/**/*.js'", + "test": "mocha -R progress -c 'tests/lib/*.cjs' && c8 mocha -R progress -c 'tests/lib/**/*.js'", "generate-release": "eslint-generate-release", "generate-alpharelease": "eslint-generate-prerelease alpha", "generate-betarelease": "eslint-generate-prerelease beta", @@ -45,15 +45,16 @@ }, "homepage": "https://github.com/eslint/eslintrc#readme", "devDependencies": { + "c8": "^7.7.3", "chai": "^4.3.4", - "eslint": "^7.29.0", + "eslint": "^7.30.0", "eslint-config-eslint": "^7.0.0", - "eslint-plugin-jsdoc": "^35.4.0", + "eslint-plugin-jsdoc": "^35.4.1", "eslint-plugin-node": "^11.1.0", "eslint-release": "^3.1.2", "fs-teardown": "0.1.1", "mocha": "^9.0.1", - "rollup": "^2.52.2", + "rollup": "^2.52.7", "shelljs": "^0.8.4", "sinon": "^11.1.1", "temp-dir": "^2.0.0" diff --git a/tests/lib/commonjs.cjs b/tests/lib/commonjs.cjs new file mode 100644 index 00000000..3ebac840 --- /dev/null +++ b/tests/lib/commonjs.cjs @@ -0,0 +1,53 @@ +/** + * @fileoverview Tests for checking that the commonjs entry points are still accessible + * @author Mike Reinstein + */ + +"use strict"; + +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +const assert = require("assert"); +const eslintrc = require("../../dist/eslintrc.cjs"); + + +//------------------------------------------------------------------------------ +// Tests +//------------------------------------------------------------------------------ + +describe("commonjs", () => { + it("is an object", () => { + assert.strictEqual(typeof eslintrc, "object"); + }); + + it("has exports", () => { + assert.strictEqual(typeof eslintrc.FlatCompat, "function"); + assert.strictEqual(typeof eslintrc.Legacy, "object"); + + [ + "ConfigArray", + "createConfigArrayFactoryContext", + "CascadingConfigArrayFactory", + "ConfigArrayFactory", + "ConfigDependency", + "ExtractedConfig", + "IgnorePattern", + "OverrideTester", + "getUsedExtractedConfigs", + "ConfigValidator" + ].forEach(prop => { + assert.strictEqual(typeof eslintrc.Legacy[prop], "function"); + }); + + // shared + [ + "ConfigOps", + "ModuleResolver", + "naming" + ].forEach(prop => { + assert.strictEqual(typeof eslintrc.Legacy[prop], "object"); + }); + }); +}); From ea9ecf040ec113c3a7be8a1d9a2a653e757287bf Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sat, 3 Jul 2021 16:37:06 +0800 Subject: [PATCH 09/20] Refactor: Drop Node overrides while still using non-ESM config --- .eslintrc.cjs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index cd03debe..c63bf2bc 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -20,19 +20,6 @@ module.exports = { }, overrides: [ - { - files: ["*.cjs", "conf/**"], - env: { - node: true - }, - globals: { - require: true, - module: true - }, - parserOptions: { - sourceType: "script" - } - }, { files: ["tests/**/*"], env: { mocha: true }, From 1dba2ce8cfb802d41b6d0d10947258ace738d2a8 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sat, 3 Jul 2021 16:39:19 +0800 Subject: [PATCH 10/20] Refactor: Drop `Module.createRequireFromPath` --- lib/shared/relative-module-resolver.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/shared/relative-module-resolver.js b/lib/shared/relative-module-resolver.js index 7bce10b5..1df0ca80 100644 --- a/lib/shared/relative-module-resolver.js +++ b/lib/shared/relative-module-resolver.js @@ -9,8 +9,7 @@ import Module from "module"; * `Module.createRequire` is added in v12.2.0. It supports URL as well. * We only support the case where the argument is a filepath, not a URL. */ -// eslint-disable-next-line node/no-deprecated-api -const createRequire = Module.createRequire || Module.createRequireFromPath; +const createRequire = Module.createRequire; /** * Resolves a Node module relative to another module From cc9201d294976ee5cad4d89d654f9812345a7e80 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sat, 3 Jul 2021 17:31:02 +0800 Subject: [PATCH 11/20] Test: Supply Node version which won't fail with dev. --- .github/workflows/ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b282639c..b1b3f9aa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: '12.22.0' - name: Install Packages run: npm install - name: Lint Files From 8f4da0549779409204b4ea747e7ba1581eacb698 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sat, 3 Jul 2021 17:50:57 +0800 Subject: [PATCH 12/20] Fix: Windows needs file: for absolute URLs --- tests/lib/config-array-factory.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/lib/config-array-factory.js b/tests/lib/config-array-factory.js index 4dbd1d3c..bde2dca3 100644 --- a/tests/lib/config-array-factory.js +++ b/tests/lib/config-array-factory.js @@ -8,7 +8,7 @@ //----------------------------------------------------------------------------- import path from "path"; -import { fileURLToPath } from "url"; +import { fileURLToPath, pathToFileURL } from "url"; import fs from "fs"; import { createRequire } from "module"; import { assert } from "chai"; @@ -921,7 +921,7 @@ describe("ConfigArrayFactory", () => { assertConfigArrayElement(configArray[0], { name: ".eslintrc » eslint:all", filePath: eslintAllPath, - ...(await import(eslintAllPath)).default + ...(await import(pathToFileURL(eslintAllPath))).default }); }); @@ -951,7 +951,7 @@ describe("ConfigArrayFactory", () => { assertConfigArrayElement(configArray[0], { name: ".eslintrc » eslint:recommended", filePath: eslintRecommendedPath, - ...(await import(eslintRecommendedPath)).default + ...(await import(pathToFileURL(eslintRecommendedPath))).default }); }); From 9fc1c1dd22f507e658cba271ea6c419c24dc9e2e Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sat, 10 Jul 2021 09:21:03 +0800 Subject: [PATCH 13/20] Refactor: Remove line break --- .eslintrc.cjs | 1 - 1 file changed, 1 deletion(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index c63bf2bc..de2207d9 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -38,6 +38,5 @@ module.exports = { }] } } - ] }; From 2dc89d380490b29ea041fac16c18b00c74de7c40 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sun, 18 Jul 2021 05:52:36 +0800 Subject: [PATCH 14/20] Fix: Convert further paths to file: imports for Windows --- tests/lib/flat-compat.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/lib/flat-compat.js b/tests/lib/flat-compat.js index ee671e27..194ae2c8 100644 --- a/tests/lib/flat-compat.js +++ b/tests/lib/flat-compat.js @@ -8,7 +8,7 @@ //----------------------------------------------------------------------------- import path from "path"; -import { fileURLToPath } from "url"; +import { fileURLToPath, pathToFileURL } from "url"; import { createRequire } from "module"; import { assert } from "chai"; import { FlatCompat } from "../../lib/index.js"; @@ -59,9 +59,9 @@ describe("FlatCompat", () => { let compat; const baseDirectory = getFixturePath("config"); - const pluginFixture1 = normalizePlugin((await import(path.join(baseDirectory, "node_modules/eslint-plugin-fixture1.js"))).default); - const pluginFixture2 = normalizePlugin((await import(path.join(baseDirectory, "node_modules/eslint-plugin-fixture2.js"))).default); - const pluginFixture3 = normalizePlugin((await import(path.join(baseDirectory, "node_modules/eslint-plugin-fixture3.js"))).default); + const pluginFixture1 = normalizePlugin((await import(pathToFileURL(path.join(baseDirectory, "node_modules/eslint-plugin-fixture1.js")))).default); + const pluginFixture2 = normalizePlugin((await import(pathToFileURL(path.join(baseDirectory, "node_modules/eslint-plugin-fixture2.js")))).default); + const pluginFixture3 = normalizePlugin((await import(pathToFileURL(path.join(baseDirectory, "node_modules/eslint-plugin-fixture3.js")))).default); beforeEach(() => { compat = new FlatCompat({ @@ -706,7 +706,7 @@ describe("FlatCompat", () => { assert.strictEqual(result.length, 1); assert.deepStrictEqual(result[0], { languageOptions: { - parser: (await import(getFixturePath("config/node_modules/my-parser.js"))).default + parser: (await import(pathToFileURL(getFixturePath("config/node_modules/my-parser.js")))).default } }); }); @@ -932,7 +932,7 @@ describe("FlatCompat", () => { rules: {}, environments: {}, processors: {}, - ...(await import(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture1.js"))).default + ...(await import(pathToFileURL(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture1.js")))).default } } }); @@ -946,7 +946,7 @@ describe("FlatCompat", () => { it("should translate plugins with processors", async () => { const result = compat.plugins("fixture2"); - const plugin = (await import(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture2.js"))).default; + const plugin = (await import(pathToFileURL(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture2.js")))).default; assert.strictEqual(result.length, 2); assert.deepStrictEqual(result[0], { @@ -968,7 +968,7 @@ describe("FlatCompat", () => { it("should translate multiple plugins", async () => { const result = compat.plugins("fixture1", "fixture2"); - const plugin = (await import(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture2.js"))).default; + const plugin = (await import(pathToFileURL(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture2.js")))).default; assert.strictEqual(result.length, 2); assert.deepStrictEqual(result[0], { @@ -982,7 +982,7 @@ describe("FlatCompat", () => { rules: {}, environments: {}, processors: {}, - ...(await import(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture1.js"))).default + ...(await import(pathToFileURL(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture1.js")))).default }, fixture2: { configs: {}, From eaf487f0ae128a96fb21aef688d066277a72bb1f Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 28 Jul 2021 08:52:47 +0800 Subject: [PATCH 15/20] Fix: Point `main` to CJS for older browsers --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f2d710f7..b69f0691 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.4.2", "description": "The legacy ESLintRC config file format for ESLint", "type": "module", - "main": "lib/index.js", + "main": "./dist/eslintrc.cjs", "exports": { ".": { "import": "./lib/index.js", From 0b271734745e34a23208d842ac9b8950c4ec78ae Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 28 Jul 2021 09:06:00 +0800 Subject: [PATCH 16/20] Chore: update devDeps --- package.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index b69f0691..500ce5cf 100644 --- a/package.json +++ b/package.json @@ -47,16 +47,16 @@ "devDependencies": { "c8": "^7.7.3", "chai": "^4.3.4", - "eslint": "^7.30.0", + "eslint": "^7.31.0", "eslint-config-eslint": "^7.0.0", "eslint-plugin-jsdoc": "^35.4.1", "eslint-plugin-node": "^11.1.0", "eslint-release": "^3.1.2", - "fs-teardown": "0.1.1", - "mocha": "^9.0.1", - "rollup": "^2.52.7", + "fs-teardown": "0.1.3", + "mocha": "^9.0.3", + "rollup": "^2.54.0", "shelljs": "^0.8.4", - "sinon": "^11.1.1", + "sinon": "^11.1.2", "temp-dir": "^2.0.0" }, "dependencies": { From d0ce859e7df0df1e4a658977359766d7005f9784 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Mon, 2 Aug 2021 08:01:47 +0800 Subject: [PATCH 17/20] Update .github/workflows/ci.yml Co-authored-by: Milos Djermanovic --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b1b3f9aa..acc26131 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,7 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: - node-version: '12.22.0' + node-version: '14.x' - name: Install Packages run: npm install - name: Lint Files From dd843341750fa4e95a941b87f96d5c9fdebc2401 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Mon, 2 Aug 2021 08:02:07 +0800 Subject: [PATCH 18/20] Update tests/_utils/index.js Co-authored-by: Milos Djermanovic --- tests/_utils/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/_utils/index.js b/tests/_utils/index.js index b0216531..b4e6c80b 100644 --- a/tests/_utils/index.js +++ b/tests/_utils/index.js @@ -6,7 +6,9 @@ // Requirements //----------------------------------------------------------------------------- -import { createTeardown, addFile } from "fs-teardown"; +import fsTeardown from "fs-teardown"; + +const { createTeardown, addFile } = fsTeardown; //----------------------------------------------------------------------------- // Helpers From db7d69fa62bdc3473230db839b10e7aa0f93a4c1 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Mon, 2 Aug 2021 08:02:18 +0800 Subject: [PATCH 19/20] Update package.json Co-authored-by: Milos Djermanovic --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 500ce5cf..824af24e 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "eslint-plugin-jsdoc": "^35.4.1", "eslint-plugin-node": "^11.1.0", "eslint-release": "^3.1.2", - "fs-teardown": "0.1.3", + "fs-teardown": "^0.1.3", "mocha": "^9.0.3", "rollup": "^2.54.0", "shelljs": "^0.8.4", From f4014429e7e50a61a2ebc8212b4ac7e7100f2339 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Mon, 2 Aug 2021 08:54:40 +0800 Subject: [PATCH 20/20] Refactor: Switch two of the `conf` files to ESM --- conf/{config-schema.cjs => config-schema.js} | 4 +--- conf/{environments.cjs => environments.js} | 5 ++--- lib/flat-compat.js | 7 ++----- lib/shared/config-validator.js | 10 ++-------- rollup.config.js | 3 ++- tests/lib/flat-compat.js | 5 +---- 6 files changed, 10 insertions(+), 24 deletions(-) rename conf/{config-schema.cjs => config-schema.js} (97%) rename conf/{environments.cjs => environments.js} (97%) diff --git a/conf/config-schema.cjs b/conf/config-schema.js similarity index 97% rename from conf/config-schema.cjs rename to conf/config-schema.js index 712fc423..ada90e13 100644 --- a/conf/config-schema.cjs +++ b/conf/config-schema.js @@ -3,8 +3,6 @@ * @author Sylvan Mably */ -"use strict"; - const baseConfigProperties = { $schema: { type: "string" }, env: { type: "object" }, @@ -78,4 +76,4 @@ const configSchema = { $ref: "#/definitions/objectConfig" }; -module.exports = configSchema; +export default configSchema; diff --git a/conf/environments.cjs b/conf/environments.js similarity index 97% rename from conf/environments.cjs rename to conf/environments.js index 757c9fe6..b3709a25 100644 --- a/conf/environments.cjs +++ b/conf/environments.js @@ -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 @@ -55,7 +54,7 @@ const newGlobals2021 = { //------------------------------------------------------------------------------ /** @type {Map} */ -module.exports = new Map(Object.entries({ +export default new Map(Object.entries({ // Language builtin: { diff --git a/lib/flat-compat.js b/lib/flat-compat.js index fe8d5fc1..7fa111d3 100644 --- a/lib/flat-compat.js +++ b/lib/flat-compat.js @@ -9,15 +9,12 @@ import path from "path"; import { fileURLToPath } from "url"; -import { createRequire } from "module"; - 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)); -const require = createRequire(import.meta.url); - -const environments = require("../conf/environments.cjs"); //----------------------------------------------------------------------------- // Helpers diff --git a/lib/shared/config-validator.js b/lib/shared/config-validator.js index 35c195c4..32174a56 100644 --- a/lib/shared/config-validator.js +++ b/lib/shared/config-validator.js @@ -9,18 +9,12 @@ // Requirements //------------------------------------------------------------------------------ -import path from "path"; import util from "util"; -import { createRequire } from "module"; import * as ConfigOps from "./config-ops.js"; import { emitDeprecationWarning } from "./deprecation-warnings.js"; import ajvOrig from "./ajv.js"; - -const require = createRequire(import.meta.url); - -// Ensure paths work after Rollup -const configSchema = require(path.resolve("./conf/config-schema.cjs")); -const BuiltInEnvironments = require(path.resolve("./conf/environments.cjs")); +import configSchema from "../../conf/config-schema.js"; +import BuiltInEnvironments from "../../conf/environments.js"; const ajv = ajvOrig(); diff --git a/rollup.config.js b/rollup.config.js index 5ca1329d..cf8c85e6 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -2,7 +2,8 @@ export default { input: "./lib/index.js", external: [ "module", "util", "os", "path", "debug", "fs", "import-fresh", - "strip-json-comments", "assert", "ignore", "minimatch", "url", "ajv" + "strip-json-comments", "assert", "ignore", "minimatch", "url", "ajv", + "globals" ], treeshake: false, output: { diff --git a/tests/lib/flat-compat.js b/tests/lib/flat-compat.js index 194ae2c8..22a36b4f 100644 --- a/tests/lib/flat-compat.js +++ b/tests/lib/flat-compat.js @@ -9,14 +9,11 @@ import path from "path"; import { fileURLToPath, pathToFileURL } from "url"; -import { createRequire } from "module"; import { assert } from "chai"; import { FlatCompat } from "../../lib/index.js"; +import environments from "../../conf/environments.js"; const dirname = path.dirname(fileURLToPath(import.meta.url)); -const require = createRequire(import.meta.url); - -const environments = require("../../conf/environments.cjs"); //----------------------------------------------------------------------------- // Helpers