dsherret / ts-morph

TypeScript Compiler API wrapper for static analysis and programmatic code changes.
https://ts-morph.com
MIT License
5.02k stars 196 forks source link

ts-morph does not handle js/jsx properly #1567

Closed ofersadgat closed 3 months ago

ofersadgat commented 3 months ago

Describe the bug

ts-morph doesnt properly handle js/jsx files

Version: 23.0.0

To Reproduce

import { Project } from "ts-morph";

const data = `
       import MyClass from 'MyClass';
       export default function MyJsComponent(props){ 
            return (
                <MyClass>{props.name}</MyClass>
            );
       };
       function MyOtherComponent(props){ 
            return (
                <OtherComponent>{props.name}</OtherComponent>
            );
       };
`;
const project = new Project({
    useInMemoryFileSystem: true,
    compilerOptions: {
        jsx: ts.JsxEmit.ReactJSX
    }
});
const sourceFile = project.createSourceFile("/test.jsx", data);
expect(sourceFile.getImportStringLiterals().map(literal => literal.getLiteralValue())).toEqual(['MyClass']); //fails
expect(sourceFile.getEmitOutput().getOutputFiles()?.[0]?.getText?.() || '').to.not.equal(''); //fails

Expected behavior

I expect the first expect to return the valid imports. I expect the second expect to have the jsx compiled. The above works if I change the extension to tsx, but that messes up imports in a more full example (i have some imports by filename)

ofersadgat commented 3 months ago

apologies, i needed the 'allowJS' flag