代码之家  ›  专栏  ›  技术社区  ›  Lance Pollard

eslint.lint关于文件(``)扩展名的文本错误是非标准的吗?

  •  0
  • Lance Pollard  · 技术社区  · 2 年前

    我正在关注 ESLint docs 尝试以编程方式运行此操作:

    import { ESLint, Linter } from "eslint";
    import path from "path";
    import { fileURLToPath } from "url";
    
    const __filename = fileURLToPath(import.meta.url);
    
    const __dirname = path.dirname(__filename);
    
    const ESLINT: Linter.Config = {
      env: {
        browser: true,
        es2021: true,
      },
      extends: ["prettier"],
      // },
      parser: "@typescript-eslint/parser",
      parserOptions: {
        ecmaVersion: "latest" as const,
        project: [`${__dirname}/tsconfig.lib.json`],
        sourceType: "module" as const,
      },
      plugins: [
        "@typescript-eslint",
        "import",
        "simple-import-sort",
        "typescript-sort-keys",
        "sort-keys",
        "prettier",
      ],
      rules: {
        "@typescript-eslint/array-type": [
          2,
          {
            default: "generic",
          },
        ],
        "@typescript-eslint/await-thenable": "error",
        "@typescript-eslint/consistent-type-definitions": [2, "type"],
        "@typescript-eslint/consistent-type-exports": "error",
        "@typescript-eslint/lines-between-class-members": "error",
        "@typescript-eslint/method-signature-style": "error",
        "@typescript-eslint/naming-convention": 0,
        "@typescript-eslint/no-array-constructor": "error",
        "@typescript-eslint/no-explicit-any": "error",
        "@typescript-eslint/no-for-in-array": "error",
        "@typescript-eslint/no-namespace": "off",
        "@typescript-eslint/no-non-null-assertion": "error",
        "@typescript-eslint/no-require-imports": "error",
        "@typescript-eslint/no-this-alias": "error",
        "@typescript-eslint/no-throw-literal": "error",
        "@typescript-eslint/no-unnecessary-condition": 0,
        "@typescript-eslint/no-unsafe-argument": "error",
        "@typescript-eslint/no-unsafe-assignment": "error",
        "@typescript-eslint/no-unsafe-member-access": "error",
        "@typescript-eslint/no-unsafe-return": "error",
        "@typescript-eslint/no-unused-vars": "off",
        "@typescript-eslint/no-useless-empty-export": "error",
        "@typescript-eslint/object-curly-spacing": [2, "always"],
        "@typescript-eslint/padding-line-between-statements": [
          "error",
          {
            blankLine: "always",
            next: ["type"],
            prev: "*",
          },
        ],
        "@typescript-eslint/prefer-function-type": "error",
        "@typescript-eslint/quotes": [
          "error",
          "single",
          {
            allowTemplateLiterals: true,
            avoidEscape: true,
          },
        ],
        "@typescript-eslint/space-before-blocks": ["error", "always"],
        "@typescript-eslint/type-annotation-spacing": ["error", { after: true }],
        curly: 2,
        "default-case": "error",
        "default-case-last": "error",
        "import/first": "error",
        "import/newline-after-import": "error",
        "import/no-duplicates": "error",
        "lines-between-class-members": "off",
        "no-array-constructor": "off",
        "no-throw-literal": "off",
        "object-curly-spacing": "off",
        "padding-line-between-statements": "off",
        "prettier/prettier": 2,
        "sort-keys": 0,
        "sort-keys/sort-keys-fix": 2,
        "space-before-blocks": "off",
        "typescript-sort-keys/interface": "error",
        "typescript-sort-keys/string-enum": "error",
      },
    };
    
    const text = `export namespace Back {
      export namespace Form {
        export type Post = {
          authorId: string
          content: string
          createdAt: string
          id: string
          title: string
        }
        export type User = {
          email?: string | null | undefined
          id: string
          name: string
        }
      }
      export type Base = {
        post: Form.Post
        user: Form.User
      }
      export type Name = keyof Base
    }`;
    
    const eslint = new ESLint({
      overrideConfig: ESLINT,
      useEslintrc: false,
    });
    
    const results = await eslint.lintText(text);
    const formatter = await eslint.loadFormatter("stylish");
    const resultText = await formatter.format(results);
    console.log(resultText)
    

    这是错误的,说:

    $ npx ts-node-esm -P tsconfig.json test/make.ts
    
    <text>
      0:0  error  Parsing error: ESLint was configured to run on `<tsconfigRootDir>/<text>` using `parserOptions.project`: ./tsconfig.lib.json
    The extension for the file (``) is non-standard. You should add `parserOptions.extraFileExtensions` to your config
    
    ✖ 1 problem (1 error, 0 warnings)
    

    我试着添加 extraFileExtensions: [""] 在中 parserOptions 第节,但随后我得到:

    $ npx ts-node-esm -P tsconfig.json test/make.ts
    
    <text>
      0:0  error  Parsing error: ESLint was configured to run on `<tsconfigRootDir>/<text>` using `parserOptions.project`: ./tsconfig.lib.json
    Found unexpected extension `` specified with the `parserOptions.extraFileExtensions` option. Did you mean `.`?
    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
    See the typescript-eslint docs for more info: https://typescript-eslint.io/linting/troubleshooting#i-get-errors-telling-me-eslint-was-configured-to-run--however-that-tsconfig-does-not--none-of-those-tsconfigs-include-this-file
    
    ✖ 1 problem (1 error, 0 warnings)
    

    我如何让它与我所有的插件一起工作?

    我的 tsconfig.lib.json 是:

    {
      "compilerOptions": {
        "module": "ESNext",
        "target": "ESNext",
        "lib": ["es2020"],
        "sourceMap": true,
        "moduleResolution": "node",
        "strictNullChecks": true,
        "strict": true,
        "esModuleInterop": true,
        "noUncheckedIndexedAccess": true,
        "noErrorTruncation": true,
        "noImplicitAny": true,
        "declaration": true
      },
      "include": [""]
    }
    
    0 回复  |  直到 2 年前
        1
  •  0
  •   Lance Pollard    2 年前

    必须这样做,如果有更好的方法,请告诉我:

    async function makeText(text: string) {
      const tmp = tmpFile({ suffix: '.ts', tmpdir: './hold' })
      const eslint = new ESLint({
        fix: true,
        overrideConfig: {
          ...ESLINT,
        },
        useEslintrc: false,
      })
    
      fs.writeFileSync(tmp, text)
    
      const results = await eslint.lintFiles([tmp])
      await ESLint.outputFixes(results)
      // const formatter = await eslint.loadFormatter('stylish')
      // await formatter.format(results)
      const result = fs.readFileSync(tmp, 'utf-8')
      return prettier.format(result, {
        ...PRETTIER,
        parser: 'typescript',
      })
    }
    

    与:

    {
      "compilerOptions": {
        "module": "ESNext",
        "target": "ESNext",
        "lib": ["es2020"],
        "sourceMap": true,
        "moduleResolution": "node",
        "strictNullChecks": true,
        "strict": true,
        "esModuleInterop": true,
        "noUncheckedIndexedAccess": true,
        "noErrorTruncation": true,
        "noImplicitAny": true,
        "declaration": true
      },
      "include": ["hold/*.ts"]
    }