Skip to content

Commit

Permalink
Drop id type annotations when destructuring content
Browse files Browse the repository at this point in the history
They're unnecessary and cause a syntax error
  • Loading branch information
gitKrystan committed Apr 24, 2024
1 parent 2826d5f commit a57b45f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
Binary file modified packages/codemods/codemods
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,22 @@ export function transformLegacyStoreMethod(
path.value.arguments = [builderExpression];

if (isRecord(path.parent.parent)) {
if (j.VariableDeclarator.check(path.parent.parent.value)) {
if (
j.VariableDeclarator.check(path.parent.parent.value) &&
(j.Identifier.check(path.parent.parent.value.id) || j.ObjectPattern.check(path.parent.parent.value.id))
) {
// Replace `const post` with `const { content: post }`
// Replace `const { id }` with `const { content: { id } }`
const id = path.parent.parent.value.id;
// Intentionally drop unnecessary type annotation from id, as it causes syntax errors otherwise
const value = j.Identifier.check(id)
? j.identifier.from({ ...id, typeAnnotation: null })
: j.objectPattern.from({ ...id, typeAnnotation: null });
path.parent.parent.value.id = j.objectPattern.from({
properties: [
j.objectProperty.from({
key: j.identifier.from({ name: 'content' }),
value: path.parent.parent.value.id,
value,
}),
],
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const post: Post = await this.store.findAll<Post>('post');
const { id }: Post = await this.store.findAll<Post>('post');
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { findAll } from '@ember-data/legacy-compat/builders';
const { content: post } = await this.store.request<Post[]>(findAll<Post>('post'));
const {
content: { id },
} = await this.store.request<Post[]>(findAll<Post>('post'));

0 comments on commit a57b45f

Please sign in to comment.