惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
Jina AI
Jina AI
MyScale Blog
MyScale Blog
N
Netflix TechBlog - Medium
月光博客
月光博客
云风的 BLOG
云风的 BLOG
T
The Blog of Author Tim Ferriss
博客园_首页
GbyAI
GbyAI
The Cloudflare Blog
博客园 - 叶小钗
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
Y
Y Combinator Blog
博客园 - 三生石上(FineUI控件)
量子位
博客园 - Franky
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
人人都是产品经理
人人都是产品经理
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
InfoQ
Google DeepMind News
Google DeepMind News
S
SegmentFault 最新的问题
大猫的无限游戏
大猫的无限游戏
Apple Machine Learning Research
Apple Machine Learning Research
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
J
Java Code Geeks
腾讯CDC
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
S
Schneier on Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
Lohrmann on Cybersecurity
S
Securelist
F
Full Disclosure
Cisco Talos Blog
Cisco Talos Blog
小众软件
小众软件
The GitHub Blog
The GitHub Blog

WMI

How to Activate MySQL for Multiple Devices - WMI ESLint Flat Config for JS, TS, React, and Prettier - WMI Yarn NPM Registry Configuration - WMI Yarn Guides - WMI Set Up ADB Wireless Debugging on Android: A Full Guide - WMI How to Fill Tax Information on Google Adsense - WMI PySide6 button click open new window - WMI PySide6 autocomplete input text - WMI Mobile Legends To The Stars Event Clue - WMI [PHP] generate random proxy IP:PORT from CIDR - WMI [PHP] generate big text file for testing purpose - WMI List of Chrome Driver command line arguments - WMI Happy eid mubarak - WMI Install markdown engine on vite ESM typescript - WMI Android Activity lifecycle - WMI OkHttp cookie handling on android (webview supported) - WMI Turn git log history into markdown - WMI enable automatic memory heap resizing of android studio - WMI is defining screen density can reduce build time ? - WMI
Migrate ESLint v9 for prettier typescript javascript - WMI
Dimas Lanjaka · 2024-10-13 · via WMI

Install dependencies

install using npm:

npm i -D prettier eslint-config-prettier eslint-plugin-prettier eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin globals @eslint/js @eslint/eslintrc

install using yarn:

yarn add -D prettier eslint-config-prettier eslint-plugin-prettier eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin globals @eslint/js @eslint/eslintrc

ESLint v9 ESM configuration migrated

Save this rules to eslint.config.mjs

import globals from 'globals';
import tsParser from '@typescript-eslint/parser';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import js from '@eslint/js';
import { FlatCompat } from '@eslint/eslintrc';

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 [
  {
    // Ignore certain files and directories from linting
    ignores: [
      '**/*.md',          // Markdown files
      '**/tmp/**/*',      // Temporary files
      '**/*.html',        // HTML files
      '**/*.py',          // Python files
      '**/*.txt',         // Text files
      '**/app/**/*',      // Application-specific files
      '**/dist/**/*',     // Distribution/build files
      '**/node_modules/**/*', // Node.js dependencies
    ]
  },

  // Extend recommended ESLint rules, TypeScript plugin rules, and Prettier plugin rules
  ...compat.extends(
    'eslint:recommended',                     // Base ESLint recommended rules
    'plugin:@typescript-eslint/eslint-recommended', // TypeScript-specific recommended rules
    'plugin:@typescript-eslint/recommended',  // Additional TypeScript rules
    'plugin:prettier/recommended'             // Integrate Prettier for code formatting
  ),

  {
    linterOptions: {
      reportUnusedDisableDirectives: true      // Report unused ESLint disable comments
    },

    languageOptions: {
      globals: {
        ...globals.browser,                    // Browser global variables
        ...globals.amd,                        // AMD module globals
        ...globals.node,                       // Node.js global variables
        $: 'readonly',                         // jQuery object
        jQuery: 'readonly',                    // jQuery object
        adsbygoogle: 'writable',               // Google Ads
        hexo: 'readonly'                       // Hexo static site generator object
      },

      parser: tsParser,                        // Use TypeScript parser
      ecmaVersion: 2020,                       // Specify ECMAScript version 2020
      sourceType: 'module'                     // Enable ES6 modules
    },

    rules: {
      // Prettier formatting rules
      'prettier/prettier': [
        'error',
        {
          $schema: 'https://json.schemastore.org/prettierrc', // Prettier schema
          printWidth: 120,          // Max line length
          tabWidth: 2,              // Use 2 spaces for indentation
          useTabs: false,           // Use spaces instead of tabs
          bracketSameLine: true,    // Keep opening/closing tags on the same line for JSX
          bracketSpacing: true,     // Add space inside object literal brackets
          semi: true,               // Add semicolons at the end of statements
          singleQuote: true,        // Use single quotes for strings
          trailingComma: 'none',    // No trailing commas
          endOfLine: 'lf',          // Use linefeed (\n) as the end-of-line character
          quoteProps: 'as-needed',  // Only quote object properties when necessary

          // Override settings for specific file types
          overrides: [
            {
              excludeFiles: ['*.min.js', '*.min.cjs', '*.min.css', '*.min.html', '*.min.scss'], // Skip minified files
              files: ['*.js', '*.css', '*.sass', '*.html', '*.md', '*.ts'],                    // Target specific file types
              options: { semi: true }                                                         // Always use semicolons
            },
            {
              files: ['*.ejs', '*.njk', '*.html'],     // Specific parser for templating and HTML files
              options: { parser: 'html' }
            }
          ]
        }
      ],

      // TypeScript-specific rules
      '@typescript-eslint/explicit-function-return-type': 'off',  // Disable enforcing return type on functions
      'no-unused-vars': 'off',                                    // Disable base rule (TypeScript has its own)

      // Allow unused variables starting with _ (common convention for ignored variables)
      '@typescript-eslint/no-unused-vars': [
        'error',
        {
          argsIgnorePattern: '^_',            // Ignore unused arguments starting with _
          varsIgnorePattern: '^_',            // Ignore unused variables starting with _
          caughtErrorsIgnorePattern: '^_'     // Ignore unused caught errors starting with _
        }
      ],

      '@typescript-eslint/no-explicit-any': 'off',                // Allow usage of 'any' type
      '@typescript-eslint/no-this-alias': [
        'error',
        {
          allowDestructuring: false,          // Disallow destructuring with aliasing
          allowedNames: ['self', 'hexo']      // Allow specific aliases like 'self' and 'hexo'
        }
      ],

      // JavaScript arrow function rules
      'arrow-body-style': 'off',              // Disable forcing arrow function bodies
      'prefer-arrow-callback': 'off'          // Disable enforcing arrow functions for callbacks
    }
  },

  {
    // Specific rules for JavaScript and CommonJS files
    files: ['**/*.js', '**/*.cjs'],

    rules: {
      '@typescript-eslint/no-var-requires': 'off',  // Allow require() in CommonJS files
      '@typescript-eslint/no-require-imports': 'off' // Allow require imports
    }
  }
];

ESLint v9 CommonJS (CJS) configuraion migrated

Save this rules to eslint.config.cjs

const globals = require('globals');
const tsParser = require('@typescript-eslint/parser');
const js = require('@eslint/js');
const { FlatCompat } = require('@eslint/eslintrc');

const compat = new FlatCompat({
  baseDirectory: __dirname,                   // The current directory for ESLint configs
  recommendedConfig: js.configs.recommended,  // ESLint's recommended rules
  allConfig: js.configs.all                   // All ESLint rules
});

module.exports = [
  {
    // Ignore certain files and directories from linting
    ignores: [
      '**/*.md',          // Markdown files
      '**/tmp/**/*',      // Temporary files
      '**/*.html',        // HTML files
      '**/*.py',          // Python files
      '**/*.txt',         // Text files
      '**/app/**/*',      // Application-specific files
      '**/dist/**/*',     // Distribution/build files
      '**/node_modules/**/*', // Node.js dependencies
    ]
  },

  // Extend recommended ESLint rules, TypeScript plugin rules, and Prettier plugin rules
  ...compat.extends(
    'eslint:recommended',                     // Base ESLint recommended rules
    'plugin:@typescript-eslint/eslint-recommended', // TypeScript-specific recommended rules
    'plugin:@typescript-eslint/recommended',  // Additional TypeScript rules
    'plugin:prettier/recommended'             // Integrate Prettier for code formatting
  ),

  {
    linterOptions: {
      reportUnusedDisableDirectives: true      // Report unused ESLint disable comments
    },

    languageOptions: {
      globals: {
        ...globals.browser,                    // Browser global variables
        ...globals.amd,                        // AMD module globals
        ...globals.node,                       // Node.js global variables
        $: 'readonly',                         // jQuery object
        jQuery: 'readonly',                    // jQuery object
        adsbygoogle: 'writable',               // Google Ads
        hexo: 'readonly'                       // Hexo static site generator object
      },

      parser: tsParser,                        // Use TypeScript parser
      ecmaVersion: 2020,                       // Specify ECMAScript version 2020
      sourceType: 'module'                     // Enable ES6 modules
    },

    rules: {
      // Prettier formatting rules
      'prettier/prettier': [
        'error',
        {
          $schema: 'https://json.schemastore.org/prettierrc', // Prettier schema
          printWidth: 120,          // Max line length
          tabWidth: 2,              // Use 2 spaces for indentation
          useTabs: false,           // Use spaces instead of tabs
          bracketSameLine: true,    // Keep opening/closing tags on the same line for JSX
          bracketSpacing: true,     // Add space inside object literal brackets
          semi: true,               // Add semicolons at the end of statements
          singleQuote: true,        // Use single quotes for strings
          trailingComma: 'none',    // No trailing commas
          endOfLine: 'lf',          // Use linefeed (\n) as the end-of-line character
          quoteProps: 'as-needed',  // Only quote object properties when necessary

          // Override settings for specific file types
          overrides: [
            {
              excludeFiles: ['*.min.js', '*.min.cjs', '*.min.css', '*.min.html', '*.min.scss'], // Skip minified files
              files: ['*.js', '*.css', '*.sass', '*.html', '*.md', '*.ts'],                    // Target specific file types
              options: { semi: true }                                                         // Always use semicolons
            },
            {
              files: ['*.ejs', '*.njk', '*.html'],     // Specific parser for templating and HTML files
              options: { parser: 'html' }
            }
          ]
        }
      ],

      // TypeScript-specific rules
      '@typescript-eslint/explicit-function-return-type': 'off',  // Disable enforcing return type on functions
      'no-unused-vars': 'off',                                    // Disable base rule (TypeScript has its own)

      // Allow unused variables starting with _ (common convention for ignored variables)
      '@typescript-eslint/no-unused-vars': [
        'error',
        {
          argsIgnorePattern: '^_',            // Ignore unused arguments starting with _
          varsIgnorePattern: '^_',            // Ignore unused variables starting with _
          caughtErrorsIgnorePattern: '^_'     // Ignore unused caught errors starting with _
        }
      ],

      '@typescript-eslint/no-explicit-any': 'off',                // Allow usage of 'any' type
      '@typescript-eslint/no-this-alias': [
        'error',
        {
          allowDestructuring: false,          // Disallow destructuring with aliasing
          allowedNames: ['self', 'hexo']      // Allow specific aliases like 'self' and 'hexo'
        }
      ],

      // JavaScript arrow function rules
      'arrow-body-style': 'off',              // Disable forcing arrow function bodies
      'prefer-arrow-callback': 'off'          // Disable enforcing arrow functions for callbacks
    }
  },

  {
    // Specific rules for JavaScript and CommonJS files
    files: ['**/*.js', '**/*.cjs'],

    rules: {
      '@typescript-eslint/no-var-requires': 'off',  // Allow require() in CommonJS files
      '@typescript-eslint/no-require-imports': 'off' // Allow require imports
    }
  }
];

Prettier rule config

You can separate prettier config into .prettierrc

{
  "$schema": "https://json.schemastore.org/prettierrc",
  "printWidth": 120,
  "tabWidth": 2,
  "useTabs": false,
  "bracketSameLine": true,
  "bracketSpacing": true,
  "semi": true,
  "singleQuote": true,
  "trailingComma": "none",
  "endOfLine": "lf",
  "quoteProps": "as-needed"
}

VSCode config

Create .vscode/settings.json

{
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true // let ESLint take formating and linting
  },
  // ESLint v9 need to using flat config
  "eslint.useFlatConfig": true,
  "eslint.experimental.useFlatConfig": true
}

Optional typescript rules config

create tsconfig.json (if not created yet), for example tsconfig.json for node 16.

npm

npm install --save-dev @tsconfig/node16

yarn

yarn add --dev @tsconfig/node16
{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "Node 16",
  "extends": "@tsconfig/node16/tsconfig.json",
  "compilerOptions": {
    "preserveConstEnums": true,
    "allowJs": true,
    "outDir": "./dist"
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "**/node_modules/**",
    "**/*.spec.ts",
    "**/*.test.ts",
    "**/__tests__/**"
  ]
}

Finish

now your vscode format and lint your codes automatically.

See Also:
Comprehensive ESLint Flat Config for JS, TS, React, and Prettier