NullVoxPopuli / eslint-plugin-decorator-position

ESLint plugin for enforcing decorator position
MIT License
51 stars 9 forks source link

Does not seem to support using Prettier 3.x #711

Closed rossketron closed 9 months ago

rossketron commented 10 months ago

Description

Linting fails when using this plugin with Prettier 3.x.

Reproduction

I've created a small example application where Prettier 2.8.8 is used in the main branch and Prettier 3.0.3 is used in the non-working-prettier-3.x branch.

Run pnpm lint:js in each and the main branch will be successful, while the 3.x branch will throw the error below:

❯ pnpm lint:js

> decorator-position-example@0.0.0 lint:js /home/ross/decorator-position-example
> eslint . --cache

Uncaught error occurred while trying to load prettier config. Please open an issue at https://github.com/NullVoxPopuli/eslint-plugin-decorator-position with the following stack trace:
TypeError: prettier.resolveConfig.sync is not a function
    at lineLength (/home/ross/decorator-position-example/node_modules/.pnpm/eslint-plugin-decorator-position@5.0.2_eslint@7.32.0/node_modules/eslint-plugin-decorator-position/lib/rules/decorator-position.js:411:34)
    at Object.decoratorPositionRule [as create] (/home/ross/decorator-position-example/node_modules/.pnpm/eslint-plugin-decorator-position@5.0.2_eslint@7.32.0/node_modules/eslint-plugin-decorator-position/lib/rules/decorator-position.js:168:26)
    at createRuleListeners (/home/ross/decorator-position-example/node_modules/.pnpm/eslint@7.32.0/node_modules/eslint/lib/linter/linter.js:765:21)
    at /home/ross/decorator-position-example/node_modules/.pnpm/eslint@7.32.0/node_modules/eslint/lib/linter/linter.js:937:31
    at Array.forEach (<anonymous>)
    at runRules (/home/ross/decorator-position-example/node_modules/.pnpm/eslint@7.32.0/node_modules/eslint/lib/linter/linter.js:882:34)
    at Linter._verifyWithoutProcessors (/home/ross/decorator-position-example/node_modules/.pnpm/eslint@7.32.0/node_modules/eslint/lib/linter/linter.js:1181:31)
    at Linter._verifyWithConfigArray (/home/ross/decorator-position-example/node_modules/.pnpm/eslint@7.32.0/node_modules/eslint/lib/linter/linter.js:1280:21)
    at Linter.verify (/home/ross/decorator-position-example/node_modules/.pnpm/eslint@7.32.0/node_modules/eslint/lib/linter/linter.js:1235:25)
    at Linter.verifyAndFix (/home/ross/decorator-position-example/node_modules/.pnpm/eslint@7.32.0/node_modules/eslint/lib/linter/linter.js:1428:29)

Oops! Something went wrong! :(

ESLint: 7.32.0

TypeError: Error while loading rule 'decorator-position/decorator-position': Cannot read properties of undefined (reading 'coreOptions')
Occurred while linting /home/ross/decorator-position-example/.eslintrc.js
    at lineLength (/home/ross/decorator-position-example/node_modules/.pnpm/eslint-plugin-decorator-position@5.0.2_eslint@7.32.0/node_modules/eslint-plugin-decorator-position/lib/rules/decorator-position.js:435:51)
    at Object.decoratorPositionRule [as create] (/home/ross/decorator-position-example/node_modules/.pnpm/eslint-plugin-decorator-position@5.0.2_eslint@7.32.0/node_modules/eslint-plugin-decorator-position/lib/rules/decorator-position.js:168:26)
    at createRuleListeners (/home/ross/decorator-position-example/node_modules/.pnpm/eslint@7.32.0/node_modules/eslint/lib/linter/linter.js:765:21)
    at /home/ross/decorator-position-example/node_modules/.pnpm/eslint@7.32.0/node_modules/eslint/lib/linter/linter.js:937:31
    at Array.forEach (<anonymous>)
    at runRules (/home/ross/decorator-position-example/node_modules/.pnpm/eslint@7.32.0/node_modules/eslint/lib/linter/linter.js:882:34)
    at Linter._verifyWithoutProcessors (/home/ross/decorator-position-example/node_modules/.pnpm/eslint@7.32.0/node_modules/eslint/lib/linter/linter.js:1181:31)
    at Linter._verifyWithConfigArray (/home/ross/decorator-position-example/node_modules/.pnpm/eslint@7.32.0/node_modules/eslint/lib/linter/linter.js:1280:21)
    at Linter.verify (/home/ross/decorator-position-example/node_modules/.pnpm/eslint@7.32.0/node_modules/eslint/lib/linter/linter.js:1235:25)
    at Linter.verifyAndFix (/home/ross/decorator-position-example/node_modules/.pnpm/eslint@7.32.0/node_modules/eslint/lib/linter/linter.js:1428:29)

Oops! Something went wrong! :(

ESLint: 7.32.0

TypeError: Invalid host defined options
    at Object.<anonymous> (/home/ross/decorator-position-example/node_modules/.pnpm/prettier@3.0.3/node_modules/prettier/index.cjs:600:23)
    at Module._compile (/home/ross/decorator-position-example/node_modules/.pnpm/v8-compile-cache@2.4.0/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (/home/ross/decorator-position-example/node_modules/.pnpm/v8-compile-cache@2.4.0/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
    at lineLength (/home/ross/decorator-position-example/node_modules/.pnpm/eslint-plugin-decorator-position@5.0.2_eslint@7.32.0/node_modules/eslint-plugin-decorator-position/lib/rules/decorator-position.js:376:18)
    at Object.decoratorPositionRule [as create] (/home/ross/decorator-position-example/node_modules/.pnpm/eslint-plugin-decorator-position@5.0.2_eslint@7.32.0/node_modules/eslint-plugin-decorator-position/lib/rules/decorator-position.js:168:26)
    at createRuleListeners (/home/ross/decorator-position-example/node_modules/.pnpm/eslint@7.32.0/node_modules/eslint/lib/linter/linter.js:765:21)

Cause

It seems that this line in decorator-position.js along with this line are the culprits.

Prettier 3 moves the synchronous API to the @prettier/sync module instead of being included in the default module. Details from Prettier documentation can be found here.

Potential Solution

I am able to modify this segment of the lineLength function in decorator-position.js to the following and it is able to lint successfully, but I'm not sure if it is the best solution.

  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');
        if (!prettier.resolveConf.sync) {
          prettier = require('@prettier/sync');
        }
      } catch (error) {
        // throw an all errors that aren't "Cannot find module"
        if (!error.message.includes('Cannot find module')) {
          throw error;
        }
      }
    }
    ...
NullVoxPopuli commented 10 months ago

Can you try eslint 8?

rossketron commented 9 months ago

Nonissue when using eslint 8, everything works as expected. Thanks!

I'll go ahead and close this, as the workout is pretty simple: either upgrade to eslint 8 so the plugin works with prettier 3.x locally or downgrade to prettier 2.x. If trailing commas are needed when using prettier 2.x locally to match some other formatter that is using prettier 3.x defaults (CI in or case), your locally referenced .prettierrc can be updated to set the trailingComma parameter to all.