Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Error while loading rule '@stylistic/indent': Cannot read properties of undefined (reading 'length') #293

Closed
1 task
pkuczynski opened this issue Oct 17, 2024 · 4 comments
Labels
bug repro:needed This issue should include a reproducible example

Comments

@pkuczynski
Copy link

pkuczynski commented Oct 17, 2024

Environment

ESLint version: 9.12.0
@eslint/markdown version: 6.2.0
Node version: 20.12.2
npm version: 10.8.3
Operating System: macOS 15.0.1

Which language are you using?

gfm

What did you do?

Configuration
import js from '@eslint/js'
import stylistic from '@stylistic/eslint-plugin'
import markdown from '@eslint/markdown'
import ts from 'typescript-eslint'

export default ts.config(
  // general linter settings
  {
    linterOptions: {
      reportUnusedDisableDirectives: 'error'
    }
  },

  // stylistic and formatting
  stylistic.configs.customize({
    braceStyle: '1tbs',
    commaDangle: 'never',
    jsx: false
  }),
  {
    rules: {
      '@stylistic/arrow-parens': ['error', 'as-needed', { requireForBlockBody: true }],
      '@stylistic/no-multiple-empty-lines': ['error', { max: 1, maxEOF: 0, maxBOF: 0 }],
      '@stylistic/object-curly-newline': ['error', {
        ObjectExpression: { multiline: true, consistent: true },
        ObjectPattern: { multiline: true, consistent: true },
        ImportDeclaration: 'never',
        ExportDeclaration: { multiline: true, consistent: true, minProperties: 2 }
      }]
    }
  },

  // typescript
  {
    name: 'typescript',
    files: ['**/*.ts'],
    extends: [
      ...ts.configs.strictTypeChecked,
      ...ts.configs.stylisticTypeChecked
    ]
  },

  // markdown code-blocks
  ...markdown.configs.recommended,
)

My README looks roughly like this:

# My application

## Run application

```bash
npm start
```

## Test

```bash
npm run test
```

What did you expect to happen?

It would work

What actually happened?

I am getting an error:

TypeError: Error while loading rule '@stylistic/indent': Cannot read properties of undefined (reading 'length')
Occurred while linting /xyz/README.md
    at new TokenInfo (/xyz/node_modules/@stylistic/eslint-plugin/dist/rules/indent.js:148:32)
    at create (/xyz/node_modules/@stylistic/eslint-plugin/dist/rules/indent.js:573:23)
    at Object.create (/xyz/node_modules/@stylistic/eslint-plugin/dist/utils.js:474:14)
    at create (/xyz/node_modules/@stylistic/eslint-plugin/dist/rules/indent.js:1383:28)
    at Object.create (/xyz/node_modules/@stylistic/eslint-plugin/dist/utils.js:474:14)
    at createRuleListeners (/xyz/node_modules/eslint/lib/linter/linter.js:943:21)
    at /xyz/node_modules/eslint/lib/linter/linter.js:1068:84
    at Array.forEach (<anonymous>)
    at runRules (/xyz/node_modules/eslint/lib/linter/linter.js:999:34)
    at #flatVerifyWithoutProcessors (/xyz/node_modules/eslint/lib/linter/linter.js:1914:31)

Link to Minimal Reproducible Example

https://stackblitz.com/edit/stackblitz-starters-tu4l2x?file=eslint.config.mjs&view=editor

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

No response

@pkuczynski pkuczynski added bug repro:needed This issue should include a reproducible example labels Oct 17, 2024
@pkuczynski
Copy link
Author

Added example on stackblitz...

@nzakas
Copy link
Member

nzakas commented Oct 17, 2024

@stylistic/indent is a JavaScript rule but you're trying to run it on a Markdown file. The problem is that stylistic.configs.customize() is returning a config that doesn't have a files key, so it's being applied to all files, including Markdown files. You'll need to add a files key. See https://eslint.org/docs/latest/use/configure/combine-configs#apply-a-config-array-to-a-subset-of-files

Note: You can also use --inspect-config to be able to see which configs are being applied to a particular file. That's a good way to debug problems like this.

@nzakas nzakas closed this as not planned Won't fix, can't repro, duplicate, stale Oct 17, 2024
@pkuczynski
Copy link
Author

Thank you for reply @nzakas. I followed your suggestion and changed my config like this: https://stackblitz.com/edit/stackblitz-starters-jf9eon?file=eslint.config.mjs

  // stylistic and formatting
  {
    ...stylisticConfig,
    files: ['**/*.ts', '**/*.js'],
    rules: {
      ...stylisticConfig.rules,
      '@stylistic/arrow-parens': [
        'error',
        'as-needed',
        { requireForBlockBody: true },
      ],
      // ...
  }

where

const stylisticConfig = stylistic.configs.customize({
  braceStyle: '1tbs',
  commaDangle: 'never',
  jsx: false,
})

But this time it started to throw new error:

TypeError: sourceCode.getScope is not a function
    at Object.create (file:///home/projects/stackblitz-starters-mbx7ch/node_modules/eslint/lib/rules/logical-assignment-operators.js#cjs:233:37)
    at createRuleListeners (file:///home/projects/stackblitz-starters-mbx7ch/node_modules/eslint/lib/linter/linter.js#cjs:943:21)
    at eval (file:///home/projects/stackblitz-starters-mbx7ch/node_modules/eslint/lib/linter/linter.js#cjs:1068:84)
    at Array.forEach (<anonymous>)
    at runRules (file:///home/projects/stackblitz-starters-mbx7ch/node_modules/eslint/lib/linter/linter.js#cjs:999:34)
    at #flatVerifyWithoutProcessors (file:///home/projects/stackblitz-starters-mbx7ch/node_modules/eslint/lib/linter/linter.js#cjs:1911:31)
    at Linter._verifyWithFlatConfigArrayAndWithoutProcessors (file:///home/projects/stackblitz-starters-mbx7ch/node_modules/eslint/lib/linter/linter.js#cjs:1992:49)
    at Linter._verifyWithFlatConfigArray (file:///home/projects/stackblitz-starters-mbx7ch/node_modules/eslint/lib/linter/linter.js#cjs:2081:21)
    at Linter.verify (file:///home/projects/stackblitz-starters-mbx7ch/node_modules/eslint/lib/linter/linter.js#cjs:1528:61)
    at Linter.verifyAndFix (file:///home/projects/stackblitz-starters-mbx7ch/node_modules/eslint/lib/linter/linter.js#cjs:2319:29)

It goes away if I comment off markdown at the bottom of the config.

PS: I am not able to reopen this issue, so maybe you could? Thanks...

@nzakas
Copy link
Member

nzakas commented Oct 21, 2024

It's the same problem - you're running JS rules on Markdown files. Use --inspect-config to figure out which rules are being applied to your Markdown files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug repro:needed This issue should include a reproducible example
Projects
Archived in project
Development

No branches or pull requests

2 participants