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

getParentIfKindOrThrow throws on CallExpression syntax #1468

Open
ranj2175 opened this issue Nov 6, 2023 · 0 comments
Open

getParentIfKindOrThrow throws on CallExpression syntax #1468

ranj2175 opened this issue Nov 6, 2023 · 0 comments

Comments

@ranj2175
Copy link

ranj2175 commented Nov 6, 2023

Describe the bug

Version: 20.0.0

Error received: "Error: The parent was not a syntax kind of CallExpression."

When using getParentIfKindOrThrow, this sometimes throws on the callExpression syntax. This seems to occur depending on the length of the script.

To Reproduce

import { Project } from "ts-morph";

const generateSourceFile = (script: string): SourceFile => {
    const project = new Project({
        useInMemoryFileSystem: true
    });
    project.createSourceFile("script.ts", script)

    const sourceFile = project.getSourceFileOrThrow("script.ts");
    return sourceFile;
}

// This script throws...
const script = `declare function getSomeField(id: string): boolean;getSomeField(“abcdefg wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww”);`

const sourceFile = generateSourceFile(script);
const func = sourceFile.getFunction("getSomeField");

 try {
        const getFieldByIdNodes = func?.findReferencesAsNodes();
        for(const node of getFieldByIdNodes) {
             const callExpression = node.getParentIfKindOrThrow(SyntaxKind.CallExpression);
             ....
        }
} catch (err) {
        console.log(error);
}

But updating script to be:
const script = \n declare function getSomeField(id: string): boolean;getSomeField(“abcdefg wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww”); \n

Then it no longer throws.

Also adding in a few extra characters into the script such as:

const script = declare function getSomeField(id: string): boolean;getSomeField("abcdefg wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww zz"); results in it no longer throwing too.

Why would wrapping this in newline stop it from throwing?

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