prettier-eslint
Advanced tools
Comparing version
"use strict"; | ||
var _fs = _interopRequireDefault(require("fs")); | ||
var _path = _interopRequireDefault(require("path")); | ||
var _requireRelative = _interopRequireDefault(require("require-relative")); | ||
var _prettyFormat = _interopRequireDefault(require("pretty-format")); | ||
var _commonTags = require("common-tags"); | ||
var _indentString = _interopRequireDefault(require("indent-string")); | ||
var _loglevelColoredLevelPrefix = _interopRequireDefault(require("loglevel-colored-level-prefix")); | ||
var _lodash = _interopRequireDefault(require("lodash.merge")); | ||
var _utils = require("./utils"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/* eslint no-console:0, global-require:0, import/no-dynamic-require:0 */ | ||
/* eslint complexity: [1, 13] */ | ||
/* eslint complexity: [1, 13] */ | ||
const logger = (0, _loglevelColoredLevelPrefix.default)({ | ||
prefix: 'prettier-eslint' | ||
}); // CommonJS + ES6 modules... is it worth it? Probably not... | ||
}); | ||
// CommonJS + ES6 modules... is it worth it? Probably not... | ||
module.exports = format; | ||
/** | ||
* Formats the text with prettier and then eslint based on the given options | ||
* @param {String} options.filePath - the path of the file being formatted | ||
* can be used in leu of `eslintConfig` (eslint will be used to find the | ||
* can be used in lieu of `eslintConfig` (eslint will be used to find the | ||
* relevant config for the file). Will also be used to load the `text` if | ||
@@ -41,3 +33,3 @@ * `text` is not provided. | ||
* @param {String} options.prettierPath - the path to the prettier module. | ||
* Will default to require.resovlve('prettier') | ||
* Will default to require.resolve('prettier') | ||
* @param {Object} options.eslintConfig - the config to use for formatting | ||
@@ -56,3 +48,2 @@ * with ESLint. | ||
*/ | ||
async function format(options) { | ||
@@ -73,4 +64,9 @@ const { | ||
const eslintConfig = (0, _lodash.default)({}, options.eslintConfig, await getESLintConfig(filePath, eslintPath, options.eslintConfig || {})); | ||
const prettierOptions = (0, _lodash.default)({}, filePath && { | ||
const prettierOptions = (0, _lodash.default)({}, | ||
// Let prettier infer the parser using the filepath, if present. Otherwise | ||
// assume the file is JS and default to the babel parser. | ||
filePath ? { | ||
filepath: filePath | ||
} : { | ||
parser: 'babel' | ||
}, getPrettierConfig(filePath, prettierPath), options.prettierOptions); | ||
@@ -89,24 +85,20 @@ const formattingOptions = (0, _utils.getOptionsForFormatting)(eslintConfig, prettierOptions, fallbackPrettierOptions, eslintPath); | ||
const eslintExtensions = eslintConfig.extensions || ['.js', '.jsx', '.ts', '.tsx', '.mjs', '.vue']; | ||
const fileExtension = _path.default.extname(filePath || ''); | ||
const fileExtension = _path.default.extname(filePath || ''); // If we don't get filePath run eslint on text, otherwise only run eslint | ||
// If we don't get filePath run eslint on text, otherwise only run eslint | ||
// if it's a configured extension or fall back to a "supported" file type. | ||
const onlyPrettier = filePath ? !eslintExtensions.includes(fileExtension) : false; | ||
const prettify = createPrettify(formattingOptions.prettier, prettierPath); | ||
if (onlyPrettier) { | ||
return prettify(text); | ||
} | ||
if (['.ts', '.tsx'].includes(fileExtension)) { | ||
formattingOptions.eslint.parser = formattingOptions.eslint.parser || require.resolve('@typescript-eslint/parser'); | ||
var _formattingOptions$es; | ||
(_formattingOptions$es = formattingOptions.eslint).parser || (_formattingOptions$es.parser = require.resolve('@typescript-eslint/parser')); | ||
} | ||
if (['.vue'].includes(fileExtension)) { | ||
formattingOptions.eslint.parser = formattingOptions.eslint.parser || require.resolve('vue-eslint-parser'); | ||
var _formattingOptions$es2; | ||
(_formattingOptions$es2 = formattingOptions.eslint).parser || (_formattingOptions$es2.parser = require.resolve('vue-eslint-parser')); | ||
} | ||
const eslintFix = await createEslintFix(formattingOptions.eslint, eslintPath); | ||
if (prettierLast) { | ||
@@ -116,8 +108,6 @@ const eslintFixed = await eslintFix(text, filePath); | ||
} | ||
return eslintFix(prettify(text), filePath); | ||
return eslintFix(await prettify(text), filePath); | ||
} | ||
function createPrettify(formatOptions, prettierPath) { | ||
return function prettify(text) { | ||
return async function prettify(text) { | ||
logger.debug('calling prettier on text'); | ||
@@ -130,6 +120,5 @@ logger.trace((0, _commonTags.stripIndent)` | ||
const prettier = (0, _utils.requireModule)(prettierPath, 'prettier'); | ||
try { | ||
logger.trace('calling prettier.format with the text and prettierOptions'); | ||
const output = prettier.format(text, formatOptions); | ||
const output = await prettier.format(text, formatOptions); | ||
logger.trace('prettier: output === input', output === text); | ||
@@ -148,3 +137,2 @@ logger.trace((0, _commonTags.stripIndent)` | ||
} | ||
function createEslintFix(eslintConfig, eslintPath) { | ||
@@ -160,3 +148,2 @@ return async function eslintFix(text, filePath) { | ||
} | ||
eslintConfig.overrideConfig = { | ||
@@ -185,3 +172,2 @@ rules: eslintConfig.rules, | ||
const eslint = (0, _utils.getESLint)(eslintPath, eslintConfig); | ||
try { | ||
@@ -193,13 +179,13 @@ logger.trace('calling cliEngine.executeOnText with the text'); | ||
}); | ||
logger.trace('executeOnText returned the following report:', (0, _prettyFormat.default)(report)); // default the output to text because if there's nothing | ||
logger.trace('executeOnText returned the following report:', (0, _prettyFormat.default)(report)); | ||
// default the output to text because if there's nothing | ||
// to fix, eslint doesn't provide `output` | ||
const [{ | ||
output = text | ||
}] = await report; | ||
logger.trace('eslint --fix: output === input', output === text); // NOTE: We're ignoring linting errors/warnings here and | ||
logger.trace('eslint --fix: output === input', output === text); | ||
// NOTE: We're ignoring linting errors/warnings here and | ||
// defaulting to the given text if there are any | ||
// because all we're trying to do is fix what we can. | ||
// We don't care about what we can't | ||
logger.trace((0, _commonTags.stripIndent)` | ||
@@ -217,3 +203,2 @@ eslint --fix output: | ||
} | ||
function getTextFromFilePath(filePath) { | ||
@@ -234,3 +219,2 @@ try { | ||
} | ||
function getESLintApiOptions(eslintConfig) { | ||
@@ -252,3 +236,2 @@ // https://eslint.org/docs/developer-guide/nodejs-api | ||
} | ||
async function getESLintConfig(filePath, eslintPath, eslintOptions) { | ||
@@ -258,3 +241,2 @@ if (filePath) { | ||
} | ||
logger.trace((0, _commonTags.oneLine)` | ||
@@ -265,3 +247,2 @@ creating ESLint CLI Engine to get the config for | ||
const eslint = (0, _utils.getESLint)(eslintPath, getESLintApiOptions(eslintOptions)); | ||
try { | ||
@@ -271,3 +252,4 @@ logger.debug(`getting eslint config for file at "${filePath}"`); | ||
logger.trace(`eslint config for "${filePath}" received`, (0, _prettyFormat.default)(config)); | ||
return { ...eslintOptions, | ||
return { | ||
...eslintOptions, | ||
...config | ||
@@ -283,3 +265,2 @@ }; | ||
} | ||
function getPrettierConfig(filePath, prettierPath) { | ||
@@ -289,3 +270,2 @@ const prettier = (0, _utils.requireModule)(prettierPath, 'prettier'); | ||
} | ||
function getModulePath(filePath = __filename, moduleName) { | ||
@@ -302,5 +282,4 @@ try { | ||
} | ||
function getDefaultLogLevel() { | ||
return process.env.LOG_LEVEL || 'warn'; | ||
} |
@@ -9,14 +9,9 @@ "use strict"; | ||
exports.requireModule = requireModule; | ||
var _commonTags = require("common-tags"); | ||
var _dlv = _interopRequireDefault(require("dlv")); | ||
var _loglevelColoredLevelPrefix = _interopRequireDefault(require("loglevel-colored-level-prefix")); | ||
var _eslint = require("eslint"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/* eslint import/no-dynamic-require:0 */ | ||
/* eslint import/no-dynamic-require:0 */ | ||
const logger = (0, _loglevelColoredLevelPrefix.default)({ | ||
@@ -27,5 +22,3 @@ prefix: 'prettier-eslint' | ||
const RULE_NOT_CONFIGURED = 'RULE_NOT_CONFIGURED'; | ||
const ruleValueExists = prettierRuleValue => prettierRuleValue !== RULE_NOT_CONFIGURED && prettierRuleValue !== RULE_DISABLED && typeof prettierRuleValue !== 'undefined'; | ||
const OPTION_GETTERS = { | ||
@@ -39,7 +32,5 @@ printWidth: { | ||
let value = getRuleValue(rules, 'indent'); | ||
if (value === 'tab') { | ||
value = getRuleValue(rules, 'max-len', 'tabWidth'); | ||
} | ||
return value; | ||
@@ -78,2 +69,3 @@ }, | ||
}; | ||
/* eslint import/prefer-default-export:0 */ | ||
@@ -89,3 +81,2 @@ | ||
} | ||
function getRelevantESLintConfig(eslintConfig) { | ||
@@ -102,3 +93,2 @@ const linter = new _eslint.Linter(); | ||
} = rule; | ||
if (!fixable) { | ||
@@ -117,3 +107,4 @@ logger.trace('turning off rule:', JSON.stringify({ | ||
// overrides | ||
rules: { ...eslintConfig.rules, | ||
rules: { | ||
...eslintConfig.rules, | ||
...relevantRules | ||
@@ -125,2 +116,3 @@ }, | ||
} | ||
/** | ||
@@ -130,4 +122,2 @@ * This accepts an eslintConfig object and converts | ||
*/ | ||
function getPrettierOptionsFromESLintRules(eslintConfig, prettierOptions, fallbackPrettierOptions) { | ||
@@ -138,18 +128,16 @@ const { | ||
const prettierPluginOptions = getRuleValue(rules, 'prettier/prettier', []); | ||
if (ruleValueExists(prettierPluginOptions)) { | ||
prettierOptions = { ...prettierPluginOptions, | ||
prettierOptions = { | ||
...prettierPluginOptions, | ||
...prettierOptions | ||
}; | ||
} | ||
return Object.keys(OPTION_GETTERS).reduce((options, key) => configureOptions(prettierOptions, fallbackPrettierOptions, key, options, rules), prettierOptions); | ||
} | ||
return Object.keys(OPTION_GETTERS).reduce((options, key) => configureOptions(prettierOptions, fallbackPrettierOptions, key, options, rules), prettierOptions); | ||
} // If an ESLint rule that prettier can be configured with is enabled create a | ||
// If an ESLint rule that prettier can be configured with is enabled create a | ||
// prettier configuration object that reflects the ESLint rule configuration. | ||
function configureOptions(prettierOptions, fallbackPrettierOptions, key, options, rules) { | ||
const givenOption = prettierOptions[key]; | ||
const optionIsGiven = givenOption !== undefined; | ||
if (optionIsGiven) { | ||
@@ -164,3 +152,2 @@ options[key] = givenOption; | ||
const option = ruleValueToPrettierOption(eslintRuleValue, fallbackPrettierOptions, rules); | ||
if (option !== undefined) { | ||
@@ -170,17 +157,12 @@ options[key] = option; | ||
} | ||
return options; | ||
} | ||
function getPrintWidth(eslintValue, fallbacks) { | ||
return makePrettierOption('printWidth', eslintValue, fallbacks); | ||
} | ||
function getTabWidth(eslintValue, fallbacks) { | ||
return makePrettierOption('tabWidth', eslintValue, fallbacks); | ||
} | ||
function getSingleQuote(eslintValue, fallbacks) { | ||
let prettierValue; | ||
if (eslintValue === 'single') { | ||
@@ -195,9 +177,6 @@ prettierValue = true; | ||
} | ||
return makePrettierOption('singleQuote', prettierValue, fallbacks); | ||
} | ||
function getTrailingComma(eslintValue, fallbacks) { | ||
let prettierValue; | ||
if (eslintValue === 'never') { | ||
@@ -212,6 +191,4 @@ prettierValue = 'none'; | ||
} | ||
return makePrettierOption('trailingComma', prettierValue, fallbacks); | ||
} | ||
function getValFromTrailingCommaConfig(objectConfig) { | ||
@@ -225,3 +202,2 @@ const { | ||
const es5 = [arrays, objects].some(isAlways); | ||
if (fns) { | ||
@@ -235,6 +211,4 @@ return 'all'; | ||
} | ||
function getBracketSpacing(eslintValue, fallbacks) { | ||
let prettierValue; | ||
if (eslintValue === 'never') { | ||
@@ -247,9 +221,6 @@ prettierValue = false; | ||
} | ||
return makePrettierOption('bracketSpacing', prettierValue, fallbacks); | ||
} | ||
function getSemi(eslintValue, fallbacks) { | ||
let prettierValue; | ||
if (eslintValue === 'never') { | ||
@@ -262,9 +233,6 @@ prettierValue = false; | ||
} | ||
return makePrettierOption('semi', prettierValue, fallbacks); | ||
} | ||
function getUseTabs(eslintValue, fallbacks) { | ||
let prettierValue; | ||
if (eslintValue === 'tab') { | ||
@@ -275,9 +243,6 @@ prettierValue = true; | ||
} | ||
return makePrettierOption('useTabs', prettierValue, fallbacks); | ||
} | ||
function getBracketSameLine(eslintValue, fallbacks) { | ||
let prettierValue; | ||
if (eslintValue === 'after-props') { | ||
@@ -290,9 +255,6 @@ prettierValue = true; | ||
} | ||
return makePrettierOption('bracketSameLine', prettierValue, fallbacks); | ||
} | ||
function getArrowParens(eslintValue, fallbacks) { | ||
let prettierValue; | ||
if (eslintValue === 'as-needed') { | ||
@@ -303,6 +265,4 @@ prettierValue = 'avoid'; | ||
} | ||
return makePrettierOption('arrowParens', prettierValue, fallbacks); | ||
} | ||
function extractRuleValue(objPath, name, value) { | ||
@@ -313,2 +273,3 @@ // XXX: Ignore code coverage for the following else case | ||
// to infer from. | ||
// istanbul ignore else | ||
@@ -321,5 +282,5 @@ if (objPath) { | ||
return (0, _dlv.default)(value, objPath, RULE_NOT_CONFIGURED); | ||
} // istanbul ignore next | ||
} | ||
// istanbul ignore next | ||
logger.debug((0, _commonTags.oneLine)` | ||
@@ -331,12 +292,13 @@ The ${name} rule is using an object configuration | ||
Please file an issue (and make a pull request?) | ||
`); // istanbul ignore next | ||
`); | ||
// istanbul ignore next | ||
return undefined; | ||
} | ||
function getRuleValue(rules, name, objPath) { | ||
const ruleConfig = rules[name]; | ||
if (Array.isArray(ruleConfig)) { | ||
const [ruleSetting, value] = ruleConfig; | ||
if (Array.isArray(ruleConfig)) { | ||
const [ruleSetting, value] = ruleConfig; // If `ruleSetting` is set to disable the ESLint rule don't use `value` as | ||
// If `ruleSetting` is set to disable the ESLint rule don't use `value` as | ||
// it might be a value provided by an overriden config package e.g. airbnb | ||
@@ -346,7 +308,5 @@ // overriden by config-prettier. The airbnb values are provided even though | ||
// default. | ||
if (ruleSetting === 0 || ruleSetting === 'off') { | ||
return RULE_DISABLED; | ||
} | ||
if (typeof value === 'object') { | ||
@@ -362,10 +322,7 @@ return extractRuleValue(objPath, name, value); | ||
} | ||
return RULE_NOT_CONFIGURED; | ||
} | ||
function isAlways(val) { | ||
return val.indexOf('always') === 0; | ||
} | ||
function makePrettierOption(prettierRuleName, prettierRuleValue, fallbacks) { | ||
@@ -375,5 +332,3 @@ if (ruleValueExists(prettierRuleValue)) { | ||
} | ||
const fallback = fallbacks[prettierRuleName]; | ||
if (typeof fallback !== 'undefined') { | ||
@@ -386,3 +341,2 @@ logger.debug((0, _commonTags.oneLine)` | ||
} | ||
logger.debug((0, _commonTags.oneLine)` | ||
@@ -394,3 +348,2 @@ The ${prettierRuleName} rule is not configured, | ||
} | ||
function requireModule(modulePath, name) { | ||
@@ -408,3 +361,2 @@ try { | ||
} | ||
function getESLint(eslintPath, eslintOptions) { | ||
@@ -414,3 +366,2 @@ const { | ||
} = requireModule(eslintPath, 'eslint'); | ||
try { | ||
@@ -417,0 +368,0 @@ return new ESLint(eslintOptions); |
{ | ||
"name": "prettier-eslint", | ||
"version": "15.0.1", | ||
"version": "16.0.0", | ||
"description": "Formats your JavaScript using prettier followed by eslint --fix", | ||
@@ -8,5 +8,5 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"prepare": "husky install", | ||
"start": "nps", | ||
"test": "nps test", | ||
"precommit": "opt --in pre-commit --exec \"npm start validate\"" | ||
"test": "nps test" | ||
}, | ||
@@ -24,2 +24,3 @@ "files": [ | ||
"dependencies": { | ||
"@changesets/cli": "^2.26.2", | ||
"@types/eslint": "^8.4.2", | ||
@@ -34,27 +35,26 @@ "@types/prettier": "^2.6.0", | ||
"loglevel-colored-level-prefix": "^1.0.0", | ||
"prettier": "^2.5.1", | ||
"prettier": "^3.0.1", | ||
"pretty-format": "^23.0.1", | ||
"require-relative": "^0.8.7", | ||
"typescript": "^4.5.4", | ||
"vue-eslint-parser": "^8.0.1" | ||
"vue-eslint-parser": "^9.1.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.4.4", | ||
"@babel/core": "^7.4.5", | ||
"@babel/preset-env": "^7.4.5", | ||
"@babel/cli": "^7.22.9", | ||
"@babel/core": "^7.22.9", | ||
"@babel/preset-env": "^7.22.9", | ||
"@changesets/changelog-github": "^0.4.8", | ||
"acorn": "^6.1.1", | ||
"ajv": "^6.12.2", | ||
"all-contributors-cli": "^6.7.0", | ||
"babel-jest": "^25.0.0", | ||
"babel-jest": "^29.6.2", | ||
"chalk": "^2.1.0", | ||
"eslint-config-kentcdodds": "^20.0.1", | ||
"husky": "^2.4.1", | ||
"jest": "^25.0.0", | ||
"jest-cli": "^25.0.0", | ||
"eslint-config-kentcdodds": "^20.5.0", | ||
"husky": "^8.0.1", | ||
"jest": "^29.6.2", | ||
"jest-cli": "^29.6.2", | ||
"nps": "^5.7.1", | ||
"nps-utils": "^1.3.0", | ||
"opt-cli": "^1.5.2", | ||
"prettier-eslint-cli": "^5.0.0", | ||
"prettier-eslint-cli": "^7.1.0", | ||
"rimraf": "^2.5.4", | ||
"semantic-release": "^15.13.16", | ||
"strip-indent": "^3.0.0" | ||
@@ -61,0 +61,0 @@ }, |
@@ -254,4 +254,3 @@ # prettier-eslint | ||
checkbox in settings) | ||
- [`prettier-vscode`][vscode-plugin] - Visual Studio Code plugin (set | ||
`prettier.eslintIntegration: true` in settings) | ||
- [`vs-code-prettier-eslint`][vscode-plugin] - Visual Studio Code plugin | ||
- [`eslint-plugin-prettier`](https://github.com/not-an-aardvark/eslint-plugin-prettier) - | ||
@@ -359,2 +358,2 @@ ESLint plugin. While prettier-eslint uses `eslint --fix` to change the output of `prettier`, eslint-plugin-prettier keeps the `prettier` output as-is and integrates it with the regular ESLint workflow. | ||
[atom-dev-tools]: https://discuss.atom.io/t/how-to-make-developer-tools-appear/16232 | ||
[vscode-plugin]: https://github.com/esbenp/prettier-vscode | ||
[vscode-plugin]: https://github.com/idahogurl/vs-code-prettier-eslint |
@@ -11,3 +11,3 @@ // Type definitions for prettier-eslint 12.0 | ||
*/ | ||
type LogLevel = "error" | "warn" | "info" | "debug" | "trace"; | ||
type LogLevel = "error" | "warn" | "info" | "debug" | "trace" | "silent"; | ||
@@ -14,0 +14,0 @@ /** |
53120
0.04%18
-5.26%678
2.73%15
7.14%6
-14.29%358
-0.28%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
Updated
Updated