skolmer / i18n-tag-schema

Generates a json schema for all i18n tagged template literals in your project
http://i18n-tag.kolmer.net
MIT License
15 stars 3 forks source link

Issue with processing typescript files with certain generic expressions. #49

Closed mmorton closed 6 years ago

mmorton commented 6 years ago

When calling transpileModule on a plain .ts file, with certain generic expressions and when jsx is set to ts.JsxEmit.Preserve, incorrect JS will be emitted. For example:

const ts = require('typescript');

const contents = `
const something = <T>(a: T) => {
}
`;

const processed = ts.transpileModule(contents, {
    compilerOptions: {
        target: ts.ScriptTarget.Latest,
        jsx: ts.JsxEmit.Preserve
    }
});

console.log(processed.outputText);

will emit:

const something = <T>(a: T) =>
</>;

Which will cause errors when Babylon tries to parse it.

skolmer commented 6 years ago

thanks for the pull request! to understand this right, is the preserve flag treating simple arrow functions that do not return jsx as react stateless components?

mmorton commented 6 years ago

It's treating the generic type expression <T> as the start of a JSX element.

mmorton commented 6 years ago

To use those in a .tsx file, you have to either have multiple generic parameters, or use qualifiers. But since this is in a normal .ts file, it should not matter (and does not matter when using tsc to build everything). The only issue is here, that transpileModule doesn't have that same information about file extensions that is there when building normally.

skolmer commented 6 years ago

ok, I think this fix will work fine for 99% of typescript user. If for some reason someone is writing jsx in .ts files this will be an issue but there is still the possibility to write your own preprocessor if you want a custom typescript processing behavior. so I merged this an it is now online in 2.4.3, thanks!