Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
fix(core): fix eslint linting other file types
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrsskls committed Sep 7, 2021
1 parent 0ed19e2 commit db2c212
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 84 deletions.
65 changes: 5 additions & 60 deletions packages/stack-core/__tests__/lib/tasks/lint.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,69 +34,11 @@ describe("lib/tasks/lint", () => {
});
});

describe("without a given type", () => {
test("lint runs all tasks", async () => {
jest.mock("../../../../../packages/stack-javascript", () => {
return {
type: "javascript",
tasks: {
lint: jest.fn(),
},
};
});
jest.mock("../../../../../packages/stack-css", () => {
return {
type: "css",
tasks: {
lint: jest.fn(),
},
};
});

const js = require("../../../../stack-javascript");
const css = require("../../../../stack-css");
const lint = require("../../../lib/tasks/lint");

await lint({
config: {
use: [js, css],
},
});

expect(css.tasks.lint).toHaveBeenCalledTimes(1);
expect(js.tasks.lint).toHaveBeenCalledTimes(1);
});
});

describe("with a given file extension", () => {
test("runs the related task", async () => {
jest.mock("../../../../../packages/stack-css", () => {
return {
extensions: ["css"],
tasks: {
lint: jest.fn(),
},
};
});

const lint = require("../../../lib/tasks/lint");
const css = require("../../../../stack-css");

await lint({
config: {
use: [css],
},
fileExtension: "css",
});

expect(css.tasks.lint).toHaveBeenCalledTimes(1);
});
});

describe("with a failing lint task", () => {
test("lint returns false", async () => {
jest.mock("../../../../../packages/stack-css", () => {
return {
type: "css",
tasks: {
lint: () => Promise.reject(),
},
Expand All @@ -105,11 +47,12 @@ describe("lib/tasks/lint", () => {

const lint = require("../../../lib/tasks/lint");
const css = require("../../../../stack-css");

console.log(css.tasks.lint.toString());
const valid = await lint({
config: {
use: [css],
},
types: ["css"],
});

expect(valid).toBe(false);
Expand All @@ -120,6 +63,7 @@ describe("lib/tasks/lint", () => {
test("lint returns false", async () => {
jest.mock("../../../../../packages/stack-css", () => {
return {
type: "css",
tasks: {
lint: () => Promise.resolve(),
},
Expand All @@ -133,6 +77,7 @@ describe("lib/tasks/lint", () => {
config: {
use: [css],
},
types: ["css"],
});

expect(valid).toBe(true);
Expand Down
32 changes: 8 additions & 24 deletions packages/stack-core/lib/tasks/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ const chalk = require("chalk");
* @param {object} obj
* @param {object} obj.config
* @param {string[]} [obj.types] - the types of the lint task
* @param {string} [obj.fileExtension] - the type of the file that has been changed
* @returns {Promise} - gets resolved with a boolean, describes if linting failed or not
*/
module.exports = function lint({ config, types, fileExtension }) {
module.exports = function lint({ config, types }) {
console.log(chalk.magenta.bold("\nLinting files…"));

const allTasks = [];
Expand All @@ -29,39 +28,24 @@ module.exports = function lint({ config, types, fileExtension }) {
}
});
}

// if a task type is passed to the linter,
// add the corresponding task to the list of tasks to run
console.log(types);
if (Array.isArray(types) && types.length > 0) {
const tasks = allTasks.filter((t) => types.includes(t.type));

if (tasks.length > 0) {
tasks.forEach((task) => {
tasksToRun.push(task.task(config, task.config, types));
tasksToRun.push(task.task(config, task.config, task.extensions));
});
} else {
console.log("\nNo lint task found, skipping…");
tasksToRun.push(Promise.resolve());
}
} else if (fileExtension) {
const task = allTasks.find(
(t) => t.extensions && t.extensions.includes(fileExtension)
);

if (task) {
tasksToRun.push(task.task(config, task.config, types));
} else {
console.log("\nNo lint task found, skipping…");
tasksToRun.push(Promise.resolve());
}
// else simply run all tasks
} else {
allTasks.forEach((task) =>
tasksToRun.push(task.task(config, task.config, types))
);
console.log("\nNo lint task found, skipping…");
tasksToRun.push(Promise.resolve());
}

return Promise.all(tasksToRun)
.then(() => true)
.catch(() => false);
return Promise.allSettled(tasksToRun).then((results) =>
results.every(({ status }) => status === "fulfilled")
);
};

0 comments on commit db2c212

Please sign in to comment.