microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.09k stars 12.5k forks source link

Watch emit is not consistent #29967

Closed kaaninel closed 8 months ago

kaaninel commented 5 years ago

I wrote a script using ts.createWatchCompilerHost and with visitors im modifying emits and for first one it works great but when i save second time my edit from last time affects results. For example when i see @CustomElement decorator i delete that decorator and emit some lines of code. Next time that file compiles with watch since that decorator is deleted neither code i actually try to emit works or decorator comes back to me. I'm curious whats right way for this kind of edits.

Deleting decorator from decorator array

const SpliceDecorator = (
  node: ts.Node,
  Condition: (x: ts.Decorator) => boolean
) => {
  if (!node.decorators) return;
  const Dec = node.decorators.findIndex(Condition);
  if (Dec > -1) {
    const R = node.decorators[Dec];
    const Decs = Array.from(node.decorators);
    Decs.splice(Dec, 1);
    if (Decs && Decs.length) {
      node.decorators =  ts.createNodeArray(Decs);
    } else {
      delete node.decorators;
    }
    return R;
  }
};

Updating emited code / class

    const AssignTag = ts.createProperty(
      undefined,
      [ts.createModifier(ts.SyntaxKind.StaticKeyword)],
      "Tag",
      undefined,
      undefined,
      ts.createStringLiteral(Key)
    );
    let Nodes: any[] = [AssignTag];
    Nodes = Nodes.concat(node.members);
    node = ts.updateClassDeclaration(
      node,
      node.decorators,
      node.modifiers,
      node.name,
      node.typeParameters,
      node.heritageClauses,
      ts.createNodeArray(Nodes)
    );

I cut some unrelated parts if some things look unnecessary its probably because of this.

DanielRosenwasser commented 5 years ago

Can you provide a minimal repro of the code you're using to consume the APIs?

kaaninel commented 5 years ago

https://github.com/EmpatiLab/Linker/tree/master/Source