johnsoncodehk / tsslint

🔋⚡️ The fastest and lightest TypeScript semantic linting solution
MIT License
287 stars 2 forks source link

Why this config is not correct? #10

Closed tjx666 closed 4 months ago

tjx666 commented 4 months ago

example config worked fine.

But I want to use normal eslint style config. So, I create a convert function convertESLintRulesToTSSLint:

import { defineConfig } from '@tsslint/config';
import { convertRule } from '@tsslint/eslint';

type ESLintRules = Record<string, [number, ...any[]]>;

const rules: ESLintRules = {
  'no-unnecessary-type-assertion': [0],
};

async function convertESLintRulesToTSSLint(rules: ESLintRules) {
  return Object.fromEntries(
    await Promise.all(
      Object.entries(rules).map(async ([ruleName, [severity, ...options]]) => {
        const rulePath = `./node_modules/@typescript-eslint/eslint-plugin/dist/rules/${ruleName}.js`;
        return [ruleName, convertRule((await import(rulePath)).default.default, options, severity)];
      }),
    ),
  );
}

export default defineConfig({
  rules: await convertESLintRulesToTSSLint(rules),
});

but result with config error:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/yutengjing/code/xxx/web/node_modules/.tsslint/node_modules/@typescript-eslint/eslint-plugin/dist/rules/no-unnecessary-type-assertion.js' imported from /Users/yutengjing/code/xxx/web/node_modules/.tsslint/4d14da5c55e5af991765a38b24e6a69a622182f59251eda3136512abbf651d6a.mjstsslint
johnsoncodehk commented 4 months ago

This is a bundle issue with ESBuild, please take a try with this.

import { defineConfig } from '@tsslint/config';
import { convertRule } from '@tsslint/eslint';
import { createRequire } from 'module';
import * as path from 'path';

type ESLintRules = Record<string, [number, ...any[]]>;

const rules: ESLintRules = {
    'no-unnecessary-type-assertion': [0],
};

async function convertESLintRulesToTSSLint(rules: ESLintRules) {
    const require = createRequire(import.meta.url);
    const rulesDir = path.dirname(require.resolve('@typescript-eslint/eslint-plugin/package.json'));
    return Object.fromEntries(
        await Promise.all(
            Object.entries(rules).map(async ([ruleName, [severity, ...options]]) => {
                const rulePath = path.join(rulesDir, 'dist', 'rules', ruleName + '.js');
                return [ruleName, convertRule((await import(rulePath)).default.default, options, severity)];
            }),
        ),
    );
}

export default defineConfig({
    rules: await convertESLintRulesToTSSLint(rules),
});
tjx666 commented 4 months ago

为啥子我 fork 后本地跑起来,用一个新的 vscode 窗口打开 fixtures/convert-eslint-rules 就看不到报错提示了:

image

直接打开 mono 是可以看到的:

image
tjx666 commented 4 months ago

your convert works.

johnsoncodehk commented 4 months ago

为啥子我 fork 后本地跑起来,用一个新的 vscode 窗口打开 fixtures/convert-eslint-rules 就看不到报错提示了:

Make sure to restart the TS server after changing tsslint.config.ts. Due to a bug in the TSSLint TS plugin, it is currently not possible to automatically respond to tsslint.config.ts changes.

tjx666 commented 4 months ago

had integrated to my company project, following is my current config:

import { createRequire } from 'module';
import path from 'path';

import { defineConfig } from '@tsslint/config';
import { convertRule } from '@tsslint/eslint';

type ESLintRules = Record<string, [number, ...any[]]>;
const suggestion = 0;
const error = 1;

/**
 * @see https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/recommended-type-checked.ts
 */
const rules: ESLintRules = {
  // 'ban-ts-comment': [error],
  // 'no-explicit-any': [error],
  // 'no-misused-promises': [error],
  // 'no-unsafe-argument': [error],
  // 'no-unsafe-assignment': [error],
  // 'no-unsafe-return': [error],
  // 'no-unused-vars': [error],

  'await-thenable': [error],
  'ban-types': [error],
  'consistent-type-exports': [error],
  'no-array-constructor': [error],
  'no-base-to-string': [error],
  'no-duplicate-enum-values': [error],
  'no-duplicate-type-constituents': [error],
  'no-extra-non-null-assertion': [error],
  'no-floating-promises': [suggestion],
  'no-for-in-array': [error],
  'no-implied-eval': [error],
  'no-loss-of-precision': [error],
  'no-misused-new': [error],
  'no-namespace': [error],
  'no-non-null-asserted-optional-chain': [error],
  'no-redundant-type-constituents': [error],
  'no-this-alias': [error],
  'no-unnecessary-type-assertion': [error],
  'no-unnecessary-type-constraint': [error],
  'no-unsafe-call': [error],
  'no-unsafe-declaration-merging': [error],
  'no-unsafe-enum-comparison': [error],
  'no-unsafe-member-access': [error],
  'no-var-requires': [error],
  'prefer-as-const': [error],
  'prefer-optional-chain': [error],
  'require-await': [suggestion],
  'restrict-plus-operands': [error],
  'restrict-template-expressions': [error],
  'return-await': [error],
  'triple-slash-reference': [error],
  'unbound-method': [error],
};

/**
 * @see https://github.com/johnsoncodehk/tsslint/issues/10#issuecomment-2169565542
 */
async function convertESLintRulesToTSSLint(rules: ESLintRules) {
  const require = createRequire(import.meta.url);
  const rulesDir = path.dirname(require.resolve('@typescript-eslint/eslint-plugin/package.json'));
  return Object.fromEntries(
    await Promise.all(
      Object.entries(rules).map(async ([ruleName, [severity, ...options]]) => {
        const rulePath = path.join(rulesDir, 'dist', 'rules', `${ruleName}.js`);
        return [ruleName, convertRule((await import(rulePath)).default.default, options, severity)];
      }),
    ),
  );
}

export default defineConfig({
  rules: await convertESLintRulesToTSSLint(rules),
});
johnsoncodehk commented 3 months ago

cc @tjx666 https://github.com/johnsoncodehk/tsslint/issues/17#issuecomment-2229264934

kurtextrem commented 1 week ago

Hello! I've been following tsslint for a while, but I still don't fully understand how I can make use of it to get faster linting. I've looked at this repositorys tsslint config, and I see some rules (https://raw.githubusercontent.com/volarjs/volar.js/master/tsslint.config.ts), but those mostly do not seem to be the slowest ones that might be really useful to substitute. The slowest ones of typescript-eslint seem to be, based on https://github.com/typescript-eslint/typescript-eslint/issues/7199:

@typescript-eslint/no-misused-promises       |  1185.705 |    21.3%
@typescript-eslint/no-unsafe-argument        |  1076.531 |    19.3%
@typescript-eslint/no-unsafe-assignment      |   921.291 |    16.6%
@typescript-eslint/no-unsafe-member-access   |   411.248 |     7.4%
@typescript-eslint/no-unsafe-return          |   331.991 |     6.0%
@typescript-eslint/no-unsafe-enum-comparison |   198.984 |     3.6%
@typescript-eslint/no-floating-promises      |   126.510 |     2.3%

Is it somehow possible to 'move' those to tsslint?