Skip to content

Commit

Permalink
fix(#337): wrap require prettier in a try-catch so that we don't thro…
Browse files Browse the repository at this point in the history
…w when dep tree has no prettier
  • Loading branch information
NullVoxPopuli committed Jun 2, 2021
1 parent b566699 commit f7594ff
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/rules/decorator-position.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,16 @@ function configuredDecoratorsInOptions(options) {

function lineLength(userOptions, filePath) {
if (!prettier) {
// we acknowledge that this might not exist
// eslint-disable-next-line node/no-unpublished-require
prettier = require('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'] || [];
Expand All @@ -323,17 +330,18 @@ function lineLength(userOptions, filePath) {

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

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

if (!('printWidth' in prettierOptions)) {
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*
Expand Down

0 comments on commit f7594ff

Please sign in to comment.