From b681161691b45577c64ce1b067e64ee0339ce711 Mon Sep 17 00:00:00 2001 From: ttionya Date: Thu, 12 Sep 2024 17:31:04 +0800 Subject: [PATCH] feat(eslint-config): add no-useless-template-literals rule --- packages/eslint-config/dist/manifest.json | 2 +- packages/eslint-config/dist/typescript.js | 9 ++++++++- .../eslint-config/tests/typescript/index.js | 1 + .../no-useless-template-literals/.eslintrc.js | 11 +++++++++++ .../no-useless-template-literals/bad.ts | 19 +++++++++++++++++++ .../no-useless-template-literals/good.ts | 17 +++++++++++++++++ 6 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 packages/eslint-config/tests/typescript/no-useless-template-literals/.eslintrc.js create mode 100644 packages/eslint-config/tests/typescript/no-useless-template-literals/bad.ts create mode 100644 packages/eslint-config/tests/typescript/no-useless-template-literals/good.ts diff --git a/packages/eslint-config/dist/manifest.json b/packages/eslint-config/dist/manifest.json index 475dae8..b8b8472 100644 --- a/packages/eslint-config/dist/manifest.json +++ b/packages/eslint-config/dist/manifest.json @@ -386,6 +386,7 @@ "@typescript-eslint/no-use-before-define", "@typescript-eslint/no-useless-constructor", "@typescript-eslint/no-useless-empty-export", + "@typescript-eslint/no-useless-template-literals", "@typescript-eslint/non-nullable-type-assertion-style", "@typescript-eslint/parameter-properties", "@typescript-eslint/prefer-enum-initializers", @@ -519,7 +520,6 @@ "descriptionCN": "项目中应该存在但是缺少的规则。", "expected": false, "list": [ - "@typescript-eslint/no-useless-template-literals", "@typescript-eslint/prefer-destructuring", "@typescript-eslint/prefer-find", "@typescript-eslint/prefer-namespace-keyword", diff --git a/packages/eslint-config/dist/typescript.js b/packages/eslint-config/dist/typescript.js index 02e5959..a8c927a 100644 --- a/packages/eslint-config/dist/typescript.js +++ b/packages/eslint-config/dist/typescript.js @@ -661,7 +661,6 @@ module.exports = { * 禁止不安全的一元减号 * @see {@link https://typescript-eslint.io/rules/no-unsafe-unary-minus/} * @category TypeScript - * @requires-type-information */ '@typescript-eslint/no-unsafe-unary-minus': 'error', @@ -712,6 +711,14 @@ module.exports = { */ '@typescript-eslint/no-useless-empty-export': 'error', + /** + * 禁止不必要的模板字符串 + * @see {@link https://typescript-eslint.io/rules/no-useless-template-literals/} + * @category TypeScript + * @requires-type-information + */ + '@typescript-eslint/no-useless-template-literals': 'error', + /** * 必须使用 ! 而不是 as * @see {@link https://typescript-eslint.io/rules/non-nullable-type-assertion-style/} diff --git a/packages/eslint-config/tests/typescript/index.js b/packages/eslint-config/tests/typescript/index.js index f7678a4..d96aa82 100644 --- a/packages/eslint-config/tests/typescript/index.js +++ b/packages/eslint-config/tests/typescript/index.js @@ -69,6 +69,7 @@ module.exports = { 'no-use-before-define': require('./no-use-before-define/.eslintrc.js'), 'no-useless-constructor': require('./no-useless-constructor/.eslintrc.js'), 'no-useless-empty-export': require('./no-useless-empty-export/.eslintrc.js'), + 'no-useless-template-literals': require('./no-useless-template-literals/.eslintrc.js'), 'non-nullable-type-assertion-style': require('./non-nullable-type-assertion-style/.eslintrc.js'), 'parameter-properties': require('./parameter-properties/.eslintrc.js'), 'prefer-enum-initializers': require('./prefer-enum-initializers/.eslintrc.js'), diff --git a/packages/eslint-config/tests/typescript/no-useless-template-literals/.eslintrc.js b/packages/eslint-config/tests/typescript/no-useless-template-literals/.eslintrc.js new file mode 100644 index 0000000..5ea69ae --- /dev/null +++ b/packages/eslint-config/tests/typescript/no-useless-template-literals/.eslintrc.js @@ -0,0 +1,11 @@ +module.exports = { + rules: { + /** + * 禁止不必要的模板字符串 + * @see {@link https://typescript-eslint.io/rules/no-useless-template-literals/} + * @category TypeScript + * @requires-type-information + */ + '@typescript-eslint/no-useless-template-literals': 'error', + }, +} diff --git a/packages/eslint-config/tests/typescript/no-useless-template-literals/bad.ts b/packages/eslint-config/tests/typescript/no-useless-template-literals/bad.ts new file mode 100644 index 0000000..539b514 --- /dev/null +++ b/packages/eslint-config/tests/typescript/no-useless-template-literals/bad.ts @@ -0,0 +1,19 @@ +/* eslint-disable */ +/* eslint-enable @typescript-eslint/no-useless-template-literals */ + +// errorCount 9 + +export default 1 + +const ab1 = `${'a'}${'b'}` +const ab2 = `a${'b'}` + +const stringWithNumber = `${'1 + 1 = '}${2}` + +const stringWithBoolean = `${'true is '}${true}` + +const text = 'a' +const wrappedText = `${text}` + +declare const intersectionWithString: string & { _brand: 'test-brand' } +const wrappedIntersection = `${intersectionWithString}` diff --git a/packages/eslint-config/tests/typescript/no-useless-template-literals/good.ts b/packages/eslint-config/tests/typescript/no-useless-template-literals/good.ts new file mode 100644 index 0000000..2209ae3 --- /dev/null +++ b/packages/eslint-config/tests/typescript/no-useless-template-literals/good.ts @@ -0,0 +1,17 @@ +/* eslint-disable */ +/* eslint-enable @typescript-eslint/no-useless-template-literals */ + +export default 1 + +const ab1 = 'ab' +const ab2 = 'ab' + +const stringWithNumber = `1 + 1 = 2` + +const stringWithBoolean = `true is true` + +const text = 'a' +const wrappedText = text + +declare const intersectionWithString: string & { _brand: 'test-brand' } +const wrappedIntersection = intersectionWithString