2020-08-25 23:57:08 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const validators = require("./validators");
|
2021-02-26 03:58:33 +00:00
|
|
|
const config = require("..");
|
|
|
|
const prettier = require("../prettier");
|
|
|
|
|
|
|
|
// Require locally installed eslint, for `npx eslint-config-prettier` support
|
|
|
|
// with no local eslint-config-prettier installation.
|
|
|
|
const { ESLint } = require(require.resolve("eslint", {
|
|
|
|
paths: [process.cwd(), ...require.resolve.paths("eslint")],
|
|
|
|
}));
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
const SPECIAL_RULES_URL =
|
|
|
|
"https://github.com/prettier/eslint-config-prettier#special-rules";
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
const PRETTIER_RULES_URL =
|
|
|
|
"https://github.com/prettier/eslint-config-prettier#arrow-body-style-and-prefer-arrow-callback";
|
|
|
|
|
2020-08-25 23:57:08 +00:00
|
|
|
if (module === require.main) {
|
2021-02-26 03:58:33 +00:00
|
|
|
const args = process.argv.slice(2);
|
|
|
|
|
|
|
|
if (args.length === 0) {
|
|
|
|
console.error(help());
|
2020-08-25 23:57:08 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
const eslint = new ESLint();
|
|
|
|
|
|
|
|
Promise.all(args.map((file) => eslint.calculateConfigForFile(file)))
|
|
|
|
.then((configs) => {
|
|
|
|
const rules = [].concat(
|
|
|
|
...configs.map(({ rules }, index) =>
|
|
|
|
Object.entries(rules).map((entry) => [...entry, args[index]])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const result = processRules(rules);
|
2020-08-25 23:57:08 +00:00
|
|
|
if (result.stderr) {
|
|
|
|
console.error(result.stderr);
|
|
|
|
}
|
|
|
|
if (result.stdout) {
|
|
|
|
console.error(result.stdout);
|
|
|
|
}
|
|
|
|
process.exit(result.code);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2021-02-26 03:58:33 +00:00
|
|
|
console.error(error.message);
|
2020-08-25 23:57:08 +00:00
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
function help() {
|
|
|
|
return `
|
|
|
|
Usage: npx eslint-config-prettier FILE...
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
Resolves an ESLint configuration for every given FILE and checks if they
|
|
|
|
contain rules that are unnecessary or conflict with Prettier. Example:
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
npx eslint-config-prettier index.js test/index.js other/file/to/check.js
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
Exit codes:
|
|
|
|
|
|
|
|
0: No automatically detectable problems found.
|
|
|
|
1: General error.
|
|
|
|
2: Conflicting rules found.
|
|
|
|
|
|
|
|
For more information, see:
|
|
|
|
https://github.com/prettier/eslint-config-prettier#cli-helper-tool
|
|
|
|
`.trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
function processRules(configRules) {
|
|
|
|
const regularRules = filterRules(config.rules, (_, value) => value === "off");
|
2020-08-25 23:57:08 +00:00
|
|
|
const optionsRules = filterRules(
|
2021-02-26 03:58:33 +00:00
|
|
|
config.rules,
|
2020-08-25 23:57:08 +00:00
|
|
|
(ruleName, value) => value === 0 && ruleName in validators
|
|
|
|
);
|
|
|
|
const specialRules = filterRules(
|
2021-02-26 03:58:33 +00:00
|
|
|
config.rules,
|
2020-08-25 23:57:08 +00:00
|
|
|
(ruleName, value) => value === 0 && !(ruleName in validators)
|
|
|
|
);
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
const enabledRules = configRules
|
|
|
|
.map(([ruleName, value, source]) => {
|
2020-08-25 23:57:08 +00:00
|
|
|
const arrayValue = Array.isArray(value) ? value : [value];
|
2021-02-26 03:58:33 +00:00
|
|
|
const [level, ...options] = arrayValue;
|
2020-08-25 23:57:08 +00:00
|
|
|
const isOff = level === "off" || level === 0;
|
2021-02-26 03:58:33 +00:00
|
|
|
return isOff ? null : { ruleName, options, source };
|
2020-08-25 23:57:08 +00:00
|
|
|
})
|
|
|
|
.filter(Boolean);
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
const flaggedRules = enabledRules.filter(
|
|
|
|
({ ruleName }) => ruleName in config.rules
|
|
|
|
);
|
|
|
|
|
2020-08-25 23:57:08 +00:00
|
|
|
const regularFlaggedRuleNames = filterRuleNames(
|
|
|
|
flaggedRules,
|
2021-02-26 03:58:33 +00:00
|
|
|
({ ruleName }) => ruleName in regularRules
|
2020-08-25 23:57:08 +00:00
|
|
|
);
|
|
|
|
const optionsFlaggedRuleNames = filterRuleNames(
|
|
|
|
flaggedRules,
|
2021-02-26 03:58:33 +00:00
|
|
|
({ ruleName, ...rule }) =>
|
|
|
|
ruleName in optionsRules && !validators[ruleName](rule)
|
2020-08-25 23:57:08 +00:00
|
|
|
);
|
|
|
|
const specialFlaggedRuleNames = filterRuleNames(
|
|
|
|
flaggedRules,
|
2021-02-26 03:58:33 +00:00
|
|
|
({ ruleName }) => ruleName in specialRules
|
|
|
|
);
|
|
|
|
const prettierFlaggedRuleNames = filterRuleNames(
|
|
|
|
enabledRules,
|
|
|
|
({ ruleName, source }) =>
|
|
|
|
ruleName in prettier.rules &&
|
|
|
|
enabledRules.some(
|
|
|
|
(rule) =>
|
|
|
|
rule.ruleName === "prettier/prettier" && rule.source === source
|
|
|
|
)
|
2020-08-25 23:57:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const regularMessage = [
|
|
|
|
"The following rules are unnecessary or might conflict with Prettier:",
|
|
|
|
"",
|
|
|
|
printRuleNames(regularFlaggedRuleNames),
|
|
|
|
].join("\n");
|
|
|
|
|
|
|
|
const optionsMessage = [
|
2021-02-26 03:58:33 +00:00
|
|
|
"The following rules are enabled with config that might conflict with Prettier. See:",
|
2020-08-25 23:57:08 +00:00
|
|
|
SPECIAL_RULES_URL,
|
|
|
|
"",
|
|
|
|
printRuleNames(optionsFlaggedRuleNames),
|
|
|
|
].join("\n");
|
|
|
|
|
|
|
|
const specialMessage = [
|
|
|
|
"The following rules are enabled but cannot be automatically checked. See:",
|
|
|
|
SPECIAL_RULES_URL,
|
|
|
|
"",
|
|
|
|
printRuleNames(specialFlaggedRuleNames),
|
|
|
|
].join("\n");
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
const prettierMessage = [
|
|
|
|
"The following rules can cause issues when using eslint-plugin-prettier at the same time.",
|
|
|
|
"Only enable them if you know what you are doing! See:",
|
|
|
|
PRETTIER_RULES_URL,
|
|
|
|
"",
|
|
|
|
printRuleNames(prettierFlaggedRuleNames),
|
|
|
|
].join("\n");
|
|
|
|
|
|
|
|
if (
|
|
|
|
regularFlaggedRuleNames.length === 0 &&
|
|
|
|
optionsFlaggedRuleNames.length === 0
|
|
|
|
) {
|
|
|
|
const message =
|
|
|
|
specialFlaggedRuleNames.length === 0 &&
|
|
|
|
prettierFlaggedRuleNames.length === 0
|
|
|
|
? "No rules that are unnecessary or conflict with Prettier were found."
|
|
|
|
: [
|
|
|
|
specialFlaggedRuleNames.length === 0 ? null : specialMessage,
|
|
|
|
prettierFlaggedRuleNames.length === 0 ? null : prettierMessage,
|
|
|
|
"Other than that, no rules that are unnecessary or conflict with Prettier were found.",
|
|
|
|
]
|
|
|
|
.filter(Boolean)
|
|
|
|
.join("\n\n");
|
|
|
|
|
|
|
|
return {
|
|
|
|
stdout: message,
|
|
|
|
code: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-25 23:57:08 +00:00
|
|
|
const message = [
|
|
|
|
regularFlaggedRuleNames.length === 0 ? null : regularMessage,
|
|
|
|
optionsFlaggedRuleNames.length === 0 ? null : optionsMessage,
|
|
|
|
specialFlaggedRuleNames.length === 0 ? null : specialMessage,
|
2021-02-26 03:58:33 +00:00
|
|
|
prettierFlaggedRuleNames.length === 0 ? null : prettierMessage,
|
2020-08-25 23:57:08 +00:00
|
|
|
]
|
|
|
|
.filter(Boolean)
|
|
|
|
.join("\n\n");
|
|
|
|
|
|
|
|
return {
|
|
|
|
stdout: message,
|
|
|
|
code: 2,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function filterRules(rules, fn) {
|
2021-02-26 03:58:33 +00:00
|
|
|
return Object.entries(rules)
|
|
|
|
.filter(([ruleName, value]) => fn(ruleName, value))
|
|
|
|
.reduce((obj, [ruleName]) => {
|
2020-08-25 23:57:08 +00:00
|
|
|
obj[ruleName] = true;
|
|
|
|
return obj;
|
|
|
|
}, Object.create(null));
|
|
|
|
}
|
|
|
|
|
|
|
|
function filterRuleNames(rules, fn) {
|
2021-02-26 03:58:33 +00:00
|
|
|
return [
|
|
|
|
...new Set(rules.filter((rule) => fn(rule)).map((rule) => rule.ruleName)),
|
|
|
|
];
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function printRuleNames(ruleNames) {
|
|
|
|
return ruleNames
|
|
|
|
.slice()
|
|
|
|
.sort()
|
|
|
|
.map((ruleName) => `- ${ruleName}`)
|
|
|
|
.join("\n");
|
|
|
|
}
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
exports.processRules = processRules;
|