Skip to content

Commit

Permalink
feat(upgrade): drop automatic detection of printWidth
Browse files Browse the repository at this point in the history
  • Loading branch information
Святослав Зайцев committed Jul 8, 2022
1 parent 1648e88 commit deb4c07
Showing 1 changed file with 1 addition and 53 deletions.
54 changes: 1 addition & 53 deletions lib/rules/decorator-position.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
'use strict';

const { CLIEngine } = require('eslint/lib/cli-engine');

const cli = new CLIEngine();

module.exports = {
meta: {
type: 'layout',
Expand Down Expand Up @@ -138,9 +134,7 @@ function decoratorPositionRule(context) {
}

const userOptions = context.options[0] || {};
const filePath = context.getFilename();
const { printWidth } = lineLength(userOptions, filePath);
const options = normalizeOptions({ ...userOptions, printWidth });
const options = normalizeOptions(userOptions);

return {
'ClassDeclaration > ClassBody > PropertyDefinition[decorators.length=1]'(node) {
Expand Down Expand Up @@ -307,52 +301,6 @@ function configuredDecoratorsInOptions(options) {
return allConfigs.map((config) => config[0]);
}

function lineLength(userOptions, filePath) {
if (!prettier) {
try {
// we acknowledge that this might not exist
// eslint-disable-next-line node/no-unpublished-require
prettier = require('prettier');
} catch (error) {
// throw an all errors that aren't "Cannot find module"
if (!error.message.includes('Cannot find module')) {
throw error;
}
}
}

const eslintPrettierRules = cli.getConfigForFile(filePath).rules['prettier/prettier'] || [];
const isEnabled = eslintPrettierRules[0] === 'error';

if (!isEnabled) {
return { printWidth: Infinity };
}

const eslintPrettierOptions = eslintPrettierRules[1] || {};
const usePrettierrc = !eslintPrettierOptions || eslintPrettierOptions.usePrettierrc !== false;
const prettierRcOptions =
prettier && usePrettierrc
? prettier.resolveConfig.sync(filePath, {
editorconfig: true,
})
: {};

const prettierOptions = Object.assign({}, prettierRcOptions, eslintPrettierOptions, {
filePath,
});

if (prettier && !('printWidth' in prettierOptions)) {
// available at prettier/src/main/core-options
// keep an eye on this. There may have been an issue with prettier 2.2.0
// but I've been unable to get the tests to pass prior to this change with *any*
// prettier version (issue-reproduction/196 reproduces issue #214)
const defaultPrintWidth = prettier.__internal.coreOptions.options.printWidth.default;
return { ...prettierOptions, ...userOptions, printWidth: defaultPrintWidth };
}

return Object.assign({}, prettierOptions, userOptions);
}

function normalizeConfig(config, intent) {
let name;
let options = {};
Expand Down

0 comments on commit deb4c07

Please sign in to comment.