github-pages-deploy-action/node_modules/eslint-config-prettier/bin/cli.js

208 lines
5.5 KiB
JavaScript
Raw Normal View History

2020-03-07 11:45:40 +08:00
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const path = require("path");
const getStdin = require("get-stdin");
const validators = require("./validators");
const SPECIAL_RULES_URL =
"https://github.com/prettier/eslint-config-prettier#special-rules";
if (module === require.main) {
if (process.argv.length > 2 || process.stdin.isTTY) {
console.error(
[
"This tool checks whether an ESLint configuration contains rules that are",
"unnecessary or conflict with Prettier. Its supposed to be run like this:",
"",
2020-03-31 20:40:00 +08:00
" npx eslint --print-config path/to/main.js | npx eslint-config-prettier-check",
" npx eslint --print-config test/index.js | npx eslint-config-prettier-check",
2020-03-07 11:45:40 +08:00
"",
"Exit codes:",
"",
"0: No automatically detectable problems found.",
"1: Unexpected error.",
"2: Conflicting rules found.",
"",
"For more information, see:",
2020-03-31 20:40:00 +08:00
"https://github.com/prettier/eslint-config-prettier#cli-helper-tool",
2020-03-07 11:45:40 +08:00
].join("\n")
);
process.exit(1);
}
getStdin()
2020-03-31 20:40:00 +08:00
.then((string) => {
2020-03-07 11:45:40 +08:00
const result = processString(string);
if (result.stderr) {
console.error(result.stderr);
}
if (result.stdout) {
console.error(result.stdout);
}
process.exit(result.code);
})
2020-03-31 20:40:00 +08:00
.catch((error) => {
2020-03-07 11:45:40 +08:00
console.error("Unexpected error", error);
process.exit(1);
});
}
function processString(string) {
let config;
try {
config = JSON.parse(string);
} catch (error) {
return {
stderr: `Failed to parse JSON:\n${error.message}`,
2020-03-31 20:40:00 +08:00
code: 1,
2020-03-07 11:45:40 +08:00
};
}
if (
!(
Object.prototype.toString.call(config) === "[object Object]" &&
Object.prototype.toString.call(config.rules) === "[object Object]"
)
) {
return {
stderr: `Expected a \`{"rules: {...}"}\` JSON object, but got:\n${string}`,
2020-03-31 20:40:00 +08:00
code: 1,
2020-03-07 11:45:40 +08:00
};
}
// This used to look at "files" in package.json, but that is not reliable due
// to an npm bug. See:
// https://github.com/prettier/eslint-config-prettier/issues/57
const allRules = Object.assign(
Object.create(null),
...fs
.readdirSync(path.join(__dirname, ".."))
2020-03-31 20:40:00 +08:00
.filter((name) => !name.startsWith(".") && name.endsWith(".js"))
.map((ruleFileName) => require(`../${ruleFileName}`).rules)
2020-03-07 11:45:40 +08:00
);
const regularRules = filterRules(
allRules,
(ruleName, value) => value === "off"
);
const optionsRules = filterRules(
allRules,
(ruleName, value) => value === 0 && ruleName in validators
);
const specialRules = filterRules(
allRules,
(ruleName, value) => value === 0 && !(ruleName in validators)
);
const flaggedRules = Object.keys(config.rules)
2020-03-31 20:40:00 +08:00
.map((ruleName) => {
2020-03-07 11:45:40 +08:00
const value = config.rules[ruleName];
const arrayValue = Array.isArray(value) ? value : [value];
const level = arrayValue[0];
const options = arrayValue.slice(1);
const isOff = level === "off" || level === 0;
return !isOff && ruleName in allRules ? { ruleName, options } : null;
})
.filter(Boolean);
const regularFlaggedRuleNames = filterRuleNames(
flaggedRules,
2020-03-31 20:40:00 +08:00
(ruleName) => ruleName in regularRules
2020-03-07 11:45:40 +08:00
);
const optionsFlaggedRuleNames = filterRuleNames(
flaggedRules,
(ruleName, options) =>
ruleName in optionsRules && !validators[ruleName](options)
);
const specialFlaggedRuleNames = filterRuleNames(
flaggedRules,
2020-03-31 20:40:00 +08:00
(ruleName) => ruleName in specialRules
2020-03-07 11:45:40 +08:00
);
if (
regularFlaggedRuleNames.length === 0 &&
optionsFlaggedRuleNames.length === 0
) {
const baseMessage =
"No rules that are unnecessary or conflict with Prettier were found.";
const message =
specialFlaggedRuleNames.length === 0
? baseMessage
: [
baseMessage,
"",
"However, the following rules are enabled but cannot be automatically checked. See:",
SPECIAL_RULES_URL,
"",
2020-03-31 20:40:00 +08:00
printRuleNames(specialFlaggedRuleNames),
2020-03-07 11:45:40 +08:00
].join("\n");
return {
stdout: message,
2020-03-31 20:40:00 +08:00
code: 0,
2020-03-07 11:45:40 +08:00
};
}
const regularMessage = [
"The following rules are unnecessary or might conflict with Prettier:",
"",
2020-03-31 20:40:00 +08:00
printRuleNames(regularFlaggedRuleNames),
2020-03-07 11:45:40 +08:00
].join("\n");
const optionsMessage = [
"The following rules are enabled with options that might conflict with Prettier. See:",
SPECIAL_RULES_URL,
"",
2020-03-31 20:40:00 +08:00
printRuleNames(optionsFlaggedRuleNames),
2020-03-07 11:45:40 +08:00
].join("\n");
const specialMessage = [
"The following rules are enabled but cannot be automatically checked. See:",
SPECIAL_RULES_URL,
"",
2020-03-31 20:40:00 +08:00
printRuleNames(specialFlaggedRuleNames),
2020-03-07 11:45:40 +08:00
].join("\n");
const message = [
regularFlaggedRuleNames.length === 0 ? null : regularMessage,
optionsFlaggedRuleNames.length === 0 ? null : optionsMessage,
2020-03-31 20:40:00 +08:00
specialFlaggedRuleNames.length === 0 ? null : specialMessage,
2020-03-07 11:45:40 +08:00
]
.filter(Boolean)
.join("\n\n");
return {
stdout: message,
2020-03-31 20:40:00 +08:00
code: 2,
2020-03-07 11:45:40 +08:00
};
}
function filterRules(rules, fn) {
return Object.keys(rules)
2020-03-31 20:40:00 +08:00
.filter((ruleName) => fn(ruleName, rules[ruleName]))
2020-03-07 11:45:40 +08:00
.reduce((obj, ruleName) => {
obj[ruleName] = true;
return obj;
}, Object.create(null));
}
function filterRuleNames(rules, fn) {
return rules
2020-03-31 20:40:00 +08:00
.filter((rule) => fn(rule.ruleName, rule.options))
.map((rule) => rule.ruleName);
2020-03-07 11:45:40 +08:00
}
function printRuleNames(ruleNames) {
return ruleNames
.slice()
.sort()
2020-03-31 20:40:00 +08:00
.map((ruleName) => `- ${ruleName}`)
2020-03-07 11:45:40 +08:00
.join("\n");
}
exports.processString = processString;