Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate sourcemaps for code manipulations #1438

Open
milahu opened this issue Aug 10, 2023 · 0 comments
Open

generate sourcemaps for code manipulations #1438

milahu opened this issue Aug 10, 2023 · 0 comments

Comments

@milahu
Copy link

milahu commented Aug 10, 2023

ts-to-jsdoc uses ts-morph for code manipulations in typescript files

for example, ts-to-jsdoc adds jsdoc nodes:

/** 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

/**
 * 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

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

    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: futurGH/ts-to-jsdoc#19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant