在 vsocde 中 eslint 对 .prettierrc.js 报错,请问如何解决这个问题?
File is a CommonJS module; it may be converted to an ES module.ts(80001)
Parsing error: ESLint was configured to run on `<tsconfigRootDir>/.prettierrc.js` using `parserOptions.project`: <tsconfigRootDir>/tsconfig.json
However, that TSConfig does not include this file. Either:
- Change ESLint's list of included files to not include this file
- Change that TSConfig to include this file
- Create a new TSConfig that includes this file and include it in your parserOptions.project
相关的 eslint 日志
[Warn - 11:09:31 AM] Linting file file:///c%3A/***/.prettierrc.js took 5048ms. Please check the ESLint rules for performance issues.
对应的 eslint.config.mjs 文件
import angularEslintEslintPlugin from '@angular-eslint/eslint-plugin';
import { fixupPluginRules } from '@eslint/compat';
import { FlatCompat } from '@eslint/eslintrc';
import js from '@eslint/js';
import typescriptEslint from '@typescript-eslint/eslint-plugin';
import tsParser from '@typescript-eslint/parser';
import _import from 'eslint-plugin-import';
import prettier from 'eslint-plugin-prettier';
import globals from 'globals';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all,
});
export default [
{
files: ['**/*.ts'],
ignores: ['**/.prettierrc.js', '**/apiMocks', '**/src/assets/'],
},
...compat.extends('prettier'),
{
plugins: {
import: fixupPluginRules(_import),
'@angular-eslint': angularEslintEslintPlugin,
'@typescript-eslint': typescriptEslint,
prettier,
},
languageOptions: {
globals: {
...globals.browser,
...globals.node,
},
parser: tsParser,
ecmaVersion: 5,
sourceType: 'module',
parserOptions: {
project: 'tsconfig.json',
},
},
rules: {
// ...
},
},
];
与项目中使用兼容旧版的 eslint 配置有关,换成全新的 flag config,就没这个问题了
eslint.config.mjs 配置文件内容如下,参考自 thingsboard
import eslintJS from '@eslint/js';
import angular from 'angular-eslint';
import tsEslint from 'typescript-eslint';
export default tsEslint.config({
files: ['**/*.ts'],
ignores: ['**/.prettierrc.js', '**/apiMocks', '**/src/assets/'],
languageOptions: {
parserOptions: {
project: true,
tsconfigRootDir: import.meta.dirname,
},
},
extends: [
eslintJS.configs.recommended,
...tsEslint.configs.recommended,
...tsEslint.configs.stylistic,
...angular.configs.tsRecommended,
],
processor: angular.processInlineTemplates,
rules: {
'@typescript-eslint/explicit-member-accessibility': [
'off',
{
accessibility: 'explicit',
},
],
'arrow-parens': ['off', 'always'],
'@angular-eslint/component-selector': [
'error',
{
prefix: ['tb'],
},
],
'import/order': 'off',
'@typescript-eslint/member-ordering': 'off',
'no-underscore-dangle': 'off',
'@typescript-eslint/naming-convention': 'off',
'jsdoc/newline-after-description': 0,
'@typescript-eslint/consistent-indexed-object-style': 'off',
'@typescript-eslint/array-type': 'off',
'no-extra-boolean-cast': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-inferrable-types': 'off',
'@typescript-eslint/no-unused-vars': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'no-case-declarations': 'off',
'no-prototype-builtins': 'off',
'@typescript-eslint/consistent-type-definitions': 'off',
'@angular-eslint/contextual-lifecycle': 'off',
'@typescript-eslint/adjacent-overload-signatures': 'off',
'@angular-eslint/prefer-standalone': 'off',
'@angular-eslint/no-empty-lifecycle-method': 'off',
},
});