Skip to content

Commit

Permalink
fix(eslint-plugin): [no-use-before-define] fix false positive type re…
Browse files Browse the repository at this point in the history
…ference in as, satisfies (typescript-eslint#8474)

* fix(eslint-plugin): [no-use-before-define] fix false positive type reference in as, satisfies

* chore: add testcase

* refactor: condition
  • Loading branch information
yeonjuan authored Feb 23, 2024
1 parent 1458920 commit 60c1cd3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-use-before-define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export default createRule<Options, MessageIds>({
): boolean {
return (
variable.identifiers[0].range[1] <= reference.identifier.range[1] &&
!isInInitializer(variable, reference)
!(reference.isValueReference && isInInitializer(variable, reference))
);
}

Expand Down
65 changes: 65 additions & 0 deletions packages/eslint-plugin/tests/rules/no-use-before-define.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit 60c1cd3

Please sign in to comment.