dsherret / dsherret/ts-morph

Is there a way to create inline object assignments?

Open
#756 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

Hi there and big up for this library!

I am currently working on a tool that reads TS types from a class properties and fills this type into decorators.

So I read the source file of class like this:

```typescript
export class FooBar {

@Property()
name?: string;

}
```

And want to update it to this:

```typescript
export class FooBar {

@Property({ type: 'string', optional: true })
name?: string;

}
```

I god the code working, but it does append new lines around that object assignment (in this example there is no decorator argument, but in reality there can be an object with already defined props).

This is what it outputs now:

```typescript
export class FooBar {

@Property({
type: 'string',
optional: true
})
name?: string;

}
```

What I am doing is basically:
- find the `SourceFile`
- go thru all properties (`getInstanceProperties()`)
- get the property decorators (`getDecorators()`)
- if no argument, add empty object (`decorator.addArgument('{}')`)
- add new `type` and `optional` assignments via `insertPropertyAssignment(0, { name: '...', initializer: '' })`

I also tried to use writer method instead of string initializer, but no luck there.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.