Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unsafe-assignment] handle shorthand property …
Browse files Browse the repository at this point in the history
…assignment (#8800)

* fix(eslint-plugin): [no-unsafe-assignment] handle shorthand property assignment

* fix formatting
  • Loading branch information
yeonjuan committed Apr 19, 2024
1 parent 21708bb commit d07eb9e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
20 changes: 20 additions & 0 deletions packages/eslint-plugin/tests/rules/no-unsafe-assignment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ declare function Foo(props: { a: string }): never;
'const x: Set<unknown> = y as Set<any>;',
// https://github.com/typescript-eslint/typescript-eslint/issues/2109
'const x: Map<string, string> = new Map();',
`
type Foo = { bar: unknown };
const bar: any = 1;
const foo: Foo = { bar };
`,
],
invalid: [
{
Expand Down Expand Up @@ -389,5 +394,20 @@ const test: T = ['string', []] as any;
},
],
},
{
code: `
type Foo = { bar: number };
const bar: any = 1;
const foo: Foo = { bar };
`,
errors: [
{
messageId: 'anyAssignment',
line: 4,
column: 20,
endColumn: 23,
},
],
},
],
});
6 changes: 5 additions & 1 deletion packages/type-utils/src/getContextualType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export function getContextualType(
return parent.type ? checker.getTypeFromTypeNode(parent.type) : undefined;
} else if (ts.isJsxExpression(parent)) {
return checker.getContextualType(parent);
} else if (ts.isPropertyAssignment(parent) && ts.isIdentifier(node)) {
} else if (
ts.isIdentifier(node) &&
(ts.isPropertyAssignment(parent) ||
ts.isShorthandPropertyAssignment(parent))
) {
return checker.getContextualType(node);
} else if (
ts.isBinaryExpression(parent) &&
Expand Down

0 comments on commit d07eb9e

Please sign in to comment.