Skip to content

Commit

Permalink
--wip-- [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Nov 19, 2021
1 parent 7016997 commit ec751c9
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 67 deletions.
4 changes: 2 additions & 2 deletions packages/create-config/bin/create-config.js
Expand Up @@ -6,5 +6,5 @@
*/

/* eslint no-console:off -- CLI */
import init from "../lib/init/config-initializer.js";
init.initializeConfig();
import { initializeConfig } from "../lib/init/config-initializer.js";
initializeConfig();
16 changes: 8 additions & 8 deletions packages/create-config/lib/init/config-initializer.js
Expand Up @@ -14,11 +14,10 @@ import enquirer from "enquirer";
import semver from "semver";
import { Legacy } from "@eslint/eslintrc";
import { info } from "../shared/logging.js";
import ModuleResolver from "../shared/relative-module-resolver.js";
import * as ConfigFile from "./config-file.js";
import * as npmUtils from "./npm-utils.js";

const { ConfigOps, naming } = Legacy;
const { ConfigOps, naming, ModuleResolver } = Legacy;

//------------------------------------------------------------------------------
// Private
Expand Down Expand Up @@ -563,19 +562,20 @@ function promptUser() {
});
}

/* istanbul ignore next */
function initializeConfig(){
return promptUser();
}

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------

const init = {
export {
getModulesList,
hasESLintVersionConflict,
installModules,
processAnswers,
writeFile,
/* istanbul ignore next */initializeConfig() {
return promptUser();
}
initializeConfig,
};

export default init;
13 changes: 10 additions & 3 deletions packages/create-config/package.json
Expand Up @@ -45,12 +45,17 @@
},
"devDependencies": {
"chai": "^4.3.4",
"eslint": "^8.1.0",
"eslint": "^8.2.0",
"eslint-config-airbnb": "^19.0.0",
"eslint-config-eslint": "^7.0.0",
"eslint-config-google": "^0.14.0",
"eslint-config-xo": "^0.39.0",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-jsdoc": "^37.0.3",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-react": "^7.26.1",
"eslint-plugin-react": "^7.27.0",
"eslint-plugin-react-hooks": "^4.3.0",
"eslint-release": "^3.2.0",
"espree": "^9.0.0",
"fs-teardown": "^0.2.0",
Expand All @@ -72,7 +77,9 @@
"extends": [
"eslint"
],
"parserOptions": {"ecmaVersion": "latest"},
"parserOptions": {
"ecmaVersion": "latest"
},
"rules": {
"node/no-unpublished-require": [
"error",
Expand Down
@@ -0,0 +1 @@
var foo = "doubleQuotes";
@@ -0,0 +1 @@
var name = "ESLint"
@@ -0,0 +1,3 @@
async function fn() {
await Promise.resolve();
}
@@ -0,0 +1 @@
+;
@@ -0,0 +1 @@
var foo = 'singleQuotes';
@@ -0,0 +1 @@
console.log("I'm a log");
@@ -0,0 +1 @@
var foo = "doubleQuotes";
128 changes: 82 additions & 46 deletions packages/create-config/tests/init/config-initializer.js
Expand Up @@ -15,7 +15,9 @@ import os from "os";
import sinon from "sinon";
import sh from "shelljs";
import * as npmUtils from "../../lib/init/npm-utils.js";
import * as log from "../../lib/shared/logging.js";
import proxyquireMod from "proxyquire";
import { Legacy, FlatCompat } from "@eslint/eslintrc";

const originalDir = process.cwd();
const { assert } = chai;
Expand All @@ -29,8 +31,7 @@ let answers = {};
let pkgJSONContents = {};
let pkgJSONPath = "";

// TODO: make the tests passing
describe.skip("configInitializer", () => {
describe("configInitializer", () => {

let fixtureDir;
let npmCheckStub;
Expand All @@ -39,25 +40,6 @@ describe.skip("configInitializer", () => {
let init;
let localESLintVersion = null;

const log = {
info: sinon.spy(),
error: sinon.spy()
};
const requireStubs = {
"../shared/logging": log,
"../shared/relative-module-resolver": {
resolve() {
if (localESLintVersion) {
return `local-eslint-${localESLintVersion}`;
}
throw new Error("Cannot find module");
}
},
"local-eslint-3.18.0": { linter: { version: "3.18.0" }, "@noCallThru": true },
"local-eslint-3.19.0": { linter: { version: "3.19.0" }, "@noCallThru": true },
"local-eslint-4.0.0": { linter: { version: "4.0.0" }, "@noCallThru": true }
};

// copy into clean area so as not to get "infected" by this project's .eslintrc files
before(() => {
fixtureDir = path.join(os.tmpdir(), "eslint/fixtures/config-initializer");
Expand All @@ -66,31 +48,84 @@ describe.skip("configInitializer", () => {
fixtureDir = fs.realpathSync(fixtureDir);
});

beforeEach(() => {
npmInstallStub = sinon.stub(npmUtils, "installSyncSaveDev");
npmCheckStub = sinon.stub(npmUtils, "checkDevDeps").callsFake(packages => packages.reduce((status, pkg) => {
status[pkg] = false;
return status;
}, {}));
npmFetchPeerDependenciesStub = sinon
.stub(npmUtils, "fetchPeerDependencies")
.returns({
beforeEach(async () => {
td.replaceEsm("../../lib/init/npm-utils.js", {
...npmUtils,
checkDevDeps: packages => packages.reduce((status, pkg) => {
status[pkg] = false;
return status;
}, {}),
fetchPeerDependencies: () => ({
eslint: "^3.19.0",
"eslint-plugin-jsx-a11y": "^5.0.1",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-react": "^7.0.1"
});
init = proxyquire("../../lib/init/config-initializer", requireStubs);
});
})
});

afterEach(() => {
log.info.resetHistory();
log.error.resetHistory();
npmInstallStub.restore();
npmCheckStub.restore();
npmFetchPeerDependenciesStub.restore();
td.replaceEsm("@eslint/eslintrc", {
FlatCompat,
Legacy: {
...Legacy,
ModuleResolver: {
resolve: () => {
if (localESLintVersion) {
return `local-eslint-${localESLintVersion}`;
}
throw new Error("Cannot find module");
}
}
}
})
// td.replace(npmUtils, "checkDevDeps",
// packages => packages.reduce((status, pkg) => {
// status[pkg] = false;
// return status;
// }, {})
// );
// td.when(npmUtils.fetchPeerDependencies(td.matchers.anything())).thenReturn({
// eslint: "^3.19.0",
// "eslint-plugin-jsx-a11y": "^5.0.1",
// "eslint-plugin-import": "^2.2.0",
// "eslint-plugin-react": "^7.0.1"
// });
// td.replace(Legacy, "ModuleResolver", {
// resolve: function () {
// if (localESLintVersion) {
// return `local-eslint-${localESLintVersion}`;
// }
// throw new Error("Cannot find module");
// }
// });
td.replace("local-eslint-3.18.0", {linter: { version: "3.18.0" }, "@noCallThru": true });
td.replace("local-eslint-3.19.0", {linter: { version: "3.19.0" }, "@noCallThru": true });
td.replace("local-eslint-4.0.0", {linter: { version: "4.0.0" }, "@noCallThru": true });
init = await import("../../lib/init/npm-utils.js");
// td.when(Legacy.ModuleResolver.resolve("local-eslint-3.18.0")).thenReturn({ linter: { version: "3.18.0" }, "@noCallThru": true });
// td.when(Legacy.ModuleResolver.resolve("local-eslint-3.19.0")).thenReturn({ linter: { version: "3.19.0" }, "@noCallThru": true });
// td.when(Legacy.ModuleResolver.resolve("local-eslint-4.0.0")).thenReturn({ linter: { version: "4.0.0" }, "@noCallThru": true });
// npmInstallStub = sinon.stub(npmUtils, "installSyncSaveDev");
// npmCheckStub = sinon.stub(npmUtils, "checkDevDeps").callsFake(packages => packages.reduce((status, pkg) => {
// status[pkg] = false;
// return status;
// }, {}));
// npmFetchPeerDependenciesStub = sinon
// .stub(npmUtils, "fetchPeerDependencies")
// .returns({
// eslint: "^3.19.0",
// "eslint-plugin-jsx-a11y": "^5.0.1",
// "eslint-plugin-import": "^2.2.0",
// "eslint-plugin-react": "^7.0.1"
// });
init = await import ("../../lib/init/config-initializer.js");
});

// afterEach(() => {
// npmInstallStub.restore();
// npmCheckStub.restore();
// npmFetchPeerDependenciesStub.restore();
// });

after(() => {
sh.rm("-r", fixtureDir);
});
Expand Down Expand Up @@ -193,7 +228,8 @@ describe.skip("configInitializer", () => {
});
});

describe("guide", () => {
// TODO
describe.skip("guide", () => {
it("should support the google style guide", () => {
const config = { extends: "google" };
const modules = init.getModulesList(config);
Expand Down Expand Up @@ -387,13 +423,13 @@ describe.skip("configInitializer", () => {
process.chdir(originalDir);
});

it("should create .eslintrc.json", () => {
it("should create .eslintrc.json", async () => {
const config = init.processAnswers(answers);
const filePath = path.resolve(fixtureDir, ".eslintrc.json");

fs.writeFileSync(pkgJSONPath, JSON.stringify(pkgJSONContents));

init.writeFile(config, answers.format);
await init.writeFile(config, answers.format);

assert.isTrue(fs.existsSync(filePath));

Expand All @@ -417,15 +453,15 @@ describe.skip("configInitializer", () => {
fs.unlinkSync(pkgJSONPath);
});

it("should create .eslintrc.yml", () => {
it("should create .eslintrc.yml", async () => {
answers.format = "YAML";

const config = init.processAnswers(answers);
const filePath = path.resolve(fixtureDir, ".eslintrc.yml");

fs.writeFileSync(pkgJSONPath, JSON.stringify(pkgJSONContents));

init.writeFile(config, answers.format);
await init.writeFile(config, answers.format);

assert.isTrue(fs.existsSync(filePath));

Expand Down Expand Up @@ -453,7 +489,7 @@ describe.skip("configInitializer", () => {
fs.unlinkSync(pkgJSONPath);
});

it("should create .eslintrc.json even with type: 'module'", () => {
it("should create .eslintrc.json even with type: 'module'", async () => {
answers.format = "JSON";

// create package.json with "type": "module"
Expand All @@ -464,7 +500,7 @@ describe.skip("configInitializer", () => {
const config = init.processAnswers(answers);
const filePath = path.resolve(fixtureDir, ".eslintrc.json");

init.writeFile(config, answers.format);
await init.writeFile(config, answers.format);

assert.isTrue(fs.existsSync(filePath));

Expand Down
23 changes: 15 additions & 8 deletions packages/create-config/tests/init/npm-utils.js
Expand Up @@ -39,7 +39,7 @@ function requireNpmUtilsWithInMemoryFileSystem(files) {
// Tests
//------------------------------------------------------------------------------
// TODO: make the tests passing!
describe.skip("npmUtils", () => {
describe("npmUtils", () => {
afterEach(() => {
sinon.verifyAndRestore();
});
Expand Down Expand Up @@ -72,7 +72,8 @@ describe.skip("npmUtils", () => {
assert.isFalse(installStatus.notarealpackage);
});

it("should handle missing devDependencies key", () => {
// TODO
it.skip("should handle missing devDependencies key", () => {
const stubbedNpmUtils = requireNpmUtilsWithInMemoryFileSystem({
"package.json": JSON.stringify({ private: true, dependencies: {} })
});
Expand All @@ -81,7 +82,8 @@ describe.skip("npmUtils", () => {
stubbedNpmUtils.checkDevDeps(["some-package"]);
});

it("should throw with message when parsing invalid package.json", () => {
// TODO
it.skip("should throw with message when parsing invalid package.json", () => {
const stubbedNpmUtils = requireNpmUtilsWithInMemoryFileSystem({
"package.json": "{ \"not: \"valid json\" }"
});
Expand Down Expand Up @@ -131,7 +133,8 @@ describe.skip("npmUtils", () => {
}, "Could not find a package.json file");
});

it("should handle missing dependencies key", () => {
// TODO
it.skip("should handle missing dependencies key", () => {
const stubbedNpmUtils = requireNpmUtilsWithInMemoryFileSystem({
"package.json": JSON.stringify({ private: true, devDependencies: {} })
});
Expand All @@ -140,7 +143,8 @@ describe.skip("npmUtils", () => {
stubbedNpmUtils.checkDeps(["some-package"]);
});

it("should throw with message when parsing invalid package.json", () => {
// TODO
it.skip("should throw with message when parsing invalid package.json", () => {
const stubbedNpmUtils = requireNpmUtilsWithInMemoryFileSystem({
"package.json": "{ \"not: \"valid json\" }"
});
Expand All @@ -157,15 +161,17 @@ describe.skip("npmUtils", () => {
});

describe("checkPackageJson()", () => {
it("should return true if package.json exists", () => {
// TODO
it.skip("should return true if package.json exists", () => {
const stubbedNpmUtils = requireNpmUtilsWithInMemoryFileSystem({
"package.json": "{ \"file\": \"contents\" }"
});

assert.strictEqual(stubbedNpmUtils.checkPackageJson(), true);
});

it("should return false if package.json does not exist", () => {
// TODO
it.skip("should return false if package.json does not exist", () => {
const stubbedNpmUtils = requireNpmUtilsWithInMemoryFileSystem({});

assert.strictEqual(stubbedNpmUtils.checkPackageJson(), false);
Expand Down Expand Up @@ -193,7 +199,8 @@ describe.skip("npmUtils", () => {
stub.restore();
});

it("should log an error message if npm throws ENOENT error", () => {
// TODO
it.skip("should log an error message if npm throws ENOENT error", () => {
const logErrorStub = sinon.stub(log, "error");
const npmUtilsStub = sinon.stub(spawn, "sync").returns({ error: { code: "ENOENT" } });

Expand Down

0 comments on commit ec751c9

Please sign in to comment.