From 60c1cd35dd057f0e6d836f1c38d255f275002c89 Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Sat, 24 Feb 2024 00:20:55 +0900 Subject: [PATCH] fix(eslint-plugin): [no-use-before-define] fix false positive type reference in as, satisfies (#8474) * fix(eslint-plugin): [no-use-before-define] fix false positive type reference in as, satisfies * chore: add testcase * refactor: condition --- .../src/rules/no-use-before-define.ts | 2 +- .../tests/rules/no-use-before-define.test.ts | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/no-use-before-define.ts b/packages/eslint-plugin/src/rules/no-use-before-define.ts index c08a21b4bac5..d577773de9ef 100644 --- a/packages/eslint-plugin/src/rules/no-use-before-define.ts +++ b/packages/eslint-plugin/src/rules/no-use-before-define.ts @@ -317,7 +317,7 @@ export default createRule({ ): boolean { return ( variable.identifiers[0].range[1] <= reference.identifier.range[1] && - !isInInitializer(variable, reference) + !(reference.isValueReference && isInInitializer(variable, reference)) ); } diff --git a/packages/eslint-plugin/tests/rules/no-use-before-define.test.ts b/packages/eslint-plugin/tests/rules/no-use-before-define.test.ts index 44ce43101e79..38efe428dd1a 100644 --- a/packages/eslint-plugin/tests/rules/no-use-before-define.test.ts +++ b/packages/eslint-plugin/tests/rules/no-use-before-define.test.ts @@ -529,6 +529,52 @@ class A { } } `, + { + code: ` +const obj = { + foo: 'foo-value', + bar: 'bar-value', +} satisfies { + [key in 'foo' | 'bar']: \`\${key}-value\`; +}; + `, + options: [{ ignoreTypeReferences: false }], + }, + { + code: ` +const obj = { + foo: 'foo-value', + bar: 'bar-value', +} as { + [key in 'foo' | 'bar']: \`\${key}-value\`; +}; + `, + options: [{ ignoreTypeReferences: false }], + }, + { + code: ` +const obj = { + foo: { + foo: 'foo', + } as { + [key in 'foo' | 'bar']: key; + }, +}; + `, + options: [{ ignoreTypeReferences: false }], + }, + { + code: ` +const foo = { + bar: 'bar', +} satisfies { + bar: typeof baz; +}; + +const baz = ''; + `, + options: [{ ignoreTypeReferences: true }], + }, ], invalid: [ { @@ -1104,6 +1150,25 @@ const Foo = { }, ], }, + { + code: ` +const foo = { + bar: 'bar', +} satisfies { + bar: typeof baz; +}; + +const baz = ''; + `, + options: [{ ignoreTypeReferences: false }], + errors: [ + { + messageId: 'noUseBeforeDefine', + data: { name: 'baz' }, + type: AST_NODE_TYPES.Identifier, + }, + ], + }, // "variables" option {