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

Running setIsAsync(true) on ArrowFunction incorrectly inserts async keyword #1583

Open
tmaarse opened this issue Nov 13, 2024 · 0 comments
Open

Comments

@tmaarse
Copy link

tmaarse commented Nov 13, 2024

Describe the bug

Version: 24.0.0 (latest)

Using setIsAsync(true) on an ArrowFunction (which indirectly calls addModifier) can result in an error due to incorrect insertion of the async keyword.

To Reproduce

Self-contained reproduction file:

import { Node, Project } from "ts-morph";

const project = new Project();
const fileText = `const f = a => a * 2;`;
const sourceFile = project.createSourceFile("main.js", fileText);

sourceFile.forEachDescendant((node) => {
  if (Node.isArrowFunction(node)) {
    node.setIsAsync(true);
  }
});

This results in:

ManipulationError: Manipulation error: A syntax error was inserted.

main.js:1:13 - error TS1005: ',' expected.

1 const f = a async => a * 2;
              ~~~~~
main.js:1:19 - error TS1005: ';' expected.

1 const f = a async => a * 2;
                    ~~

Error replacing tree: The children of the old and new trees were expected to have the same count (2:3).

-- Details --
Path: repro/main.js
Text: "const f = a async => a * 2;"
Stack: Error: Error replacing tree: The children of the old and new trees were expected to have the same count (2:3).
    at #handleChildren (repro/node_modules/ts-morph/dist/ts-morph.js:1469:19)
    at ParentFinderReplacementNodeHandler.handleNode (repro/node_modules/ts-morph/dist/ts-morph.js:1463:33)
    at ParentFinderReplacementNodeHandler.handleNode (repro/node_modules/ts-morph/dist/ts-morph.js:1620:19)
    at doManipulation (repro/node_modules/ts-morph/dist/ts-morph.js:2364:21)
    at insertIntoParentTextRange (repro/node_modules/ts-morph/dist/ts-morph.js:2398:5)
    at ArrowFunction.addModifier (repro/node_modules/ts-morph/dist/ts-morph.js:9175:13)
    at ArrowFunction.toggleModifier (repro/node_modules/ts-morph/dist/ts-morph.js:9152:22)
    at ArrowFunction.setIsAsync (repro/node_modules/ts-morph/dist/ts-morph.js:2884:18)
    at file://repro/arrow.mjs:9:10
    at nodeCallback (repro/node_modules/ts-morph/dist/ts-morph.js:3345:33)
    at throwError (repro/node_modules/ts-morph/dist/ts-morph.js:2380:19)
    at doManipulation (repro/node_modules/ts-morph/dist/ts-morph.js:2374:13)
    at insertIntoParentTextRange (repro/node_modules/ts-morph/dist/ts-morph.js:2398:5)
    at ArrowFunction.addModifier (repro/node_modules/ts-morph/dist/ts-morph.js:9175:13)
    at ArrowFunction.toggleModifier (repro/node_modules/ts-morph/dist/ts-morph.js:9152:22)
    at ArrowFunction.setIsAsync (repro/node_modules/ts-morph/dist/ts-morph.js:2884:18)
    at file://repro/arrow.mjs:9:10
    at nodeCallback (repro/node_modules/ts-morph/dist/ts-morph.js:3345:33)
    at repro/node_modules/ts-morph/dist/ts-morph.js:3386:37
    at VariableDeclaration.forEachChild (repro/node_modules/ts-morph/dist/ts-morph.js:3325:37) {
  filePath: 'repro/main.js',
  oldText: 'const f = a => a * 2;',
  newText: 'const f = a async => a * 2;'
}

Node.js v20.18.0

Expected behavior

I would expect the resulting file to be const f = async a => a * 2;.

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