generate sourcemaps for code manipulations
- Dominant language
- TypeScript
- Stars
- 6.2k
- Forks
- 238
- Avg merge
- 2m
- Merged PRs (30d)
- 1
Description
[ts-to-jsdoc](https://github.com/futurGH/ts-to-jsdoc) uses ts-morph for [code manipulations](https://ts-morph.com/manipulation/) in typescript files
for example, ts-to-jsdoc adds jsdoc nodes:
```ts
/** Get JSDoc for a node or create one if there isn't any */
function getJsDocOrCreate(node: JSDocableNode): JSDoc {
return node.getJsDocs()[0] || node.addJsDoc({});
}
```
addJsDoc calls [insertIntoParentTextRange](https://github.com/dsherret/ts-morph/blob/latest/packages/ts-morph/src/manipulation/manipulations/insertion.ts)
```ts
/**
* Inserts a text range into a parent.
*/
export function insertIntoParentTextRange(opts: InsertIntoParentTextRangeOptions) {
const { insertPos, newText, parent } = opts;
doManipulation(
parent._sourceFile,
new InsertionTextManipulator({
insertPos,
newText,
replacingLength: opts.replacing?.textLength,
}),
new NodeHandlerFactory().getForParentRange({
parent,
start: insertPos,
end: insertPos + newText.length,
replacingLength: opts.replacing?.textLength,
replacingNodes: opts.replacing?.nodes,
customMappings: opts.customMappings,
}),
);
}
```
insertIntoParentTextRange calls [InsertionTextManipulator](https://github.com/dsherret/ts-morph/blob/latest/packages/ts-morph/src/manipulation/textManipulators/InsertionTextManipulator.ts)
```ts
export class InsertionTextManipulator implements TextManipulator {
constructor(private readonly opts: InsertionTextManipulatorOptions) {
}
getNewText(inputText: string) {
const { insertPos, newText, replacingLength = 0 } = this.opts;
return inputText.substring(0, insertPos) + newText + inputText.substring(insertPos + replacingLength);
}
getTextForError(newText: string) {
return getTextForError(newText, this.opts.insertPos, this.opts.newText.length);
}
}
```
... but the code manipulation is not recorded in a sourcemap
i am passing the option `{ sourceMap: true }` to the typescript compiler
but this option is not used by ts-morph
```ts
const project = new Project({
compilerOptions: {
target: ScriptTarget.ESNext,
esModuleInterop: true,
sourceMap: true,
...compilerOptions,
},
});
```
expected behavior: similar to https://github.com/Rich-Harris/magic-string
related downstream issue: https://github.com/futurGH/ts-to-jsdoc/pull/19
Contributor guide
Research direction
Start by tracing sourceMap handling from the Project compiler options into the manipulation entry points in insertion.ts and InsertionTextManipulator.ts. Compare the expected mapping behavior with magic-string and the linked ts-to-jsdoc use case. Done means code manipulations produce usable sourcemaps for inserted or replaced text.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- build-system, devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100