Skip to content

Commit

Permalink
fix: codegen crash when parsing TS interfaces (#35492)
Browse files Browse the repository at this point in the history
Summary:
when I'm defining a turbomodule spec, I tried to extract a common parameter into an interface.

The error I get is this:

```
[Codegen] >>>>> Processing RNSimpleToastSpec

[Codegen] Done.
/Users/vojta/_dev/_own/simple-toast/example/node_modules/react-native-codegen/lib/parsers/typescript/utils.js:111
        throw error;
        ^

TypeError: Cannot read properties of undefined (reading 'length')
    at isModuleInterface (/Users/vojta/_dev/_own/simple-toast/example/node_modules/react-native-codegen/lib/parsers/typescript/modules/index.js:695:18)
    at Array.filter (<anonymous>)
    at buildModuleSchema (/Users/vojta/_dev/_own/simple-toast/example/node_modules/react-native-codegen/lib/parsers/typescript/modules/index.js:709:44)
    at /Users/vojta/_dev/_own/simple-toast/example/node_modules/react-native-codegen/lib/parsers/typescript/index.js:158:9
    at guard (/Users/vojta/_dev/_own/simple-toast/example/node_modules/react-native-codegen/lib/parsers/typescript/utils.js:108:14)
    at buildSchema (/Users/vojta/_dev/_own/simple-toast/example/node_modules/react-native-codegen/lib/parsers/typescript/index.js:157:22)
    at Object.parseFile (/Users/vojta/_dev/_own/simple-toast/example/node_modules/react-native-codegen/lib/parsers/typescript/index.js:185:10)
```

After the fix I get this:

```
[Codegen] >>>>> Processing RNSimpleToastSpec

[Codegen] Done.
/Users/vojta/_dev/_own/simple-toast/example/node_modules/react-native-codegen/lib/parsers/typescript/utils.js:111
        throw error;
        ^

Invariant Violation: GenericTypeAnnotation 'Styles' must resolve to a TSTypeAliasDeclaration. Instead, it resolved to a 'TSInterfaceDeclaration'
```

Then I know that I should not be using an `interface` but a `type`

## Changelog

[Internal] [Fixed] - TS codegen crash when parsing interfaces

Pull Request resolved: #35492

Test Plan:
tested locally

let me know if you want an automated test

Reviewed By: cortinico

Differential Revision: D41548015

Pulled By: cipolleschi

fbshipit-source-id: 9acf02dffbb084831690f665357fb80225cbce0d
  • Loading branch information
vonovak authored and facebook-github-bot committed Nov 28, 2022
1 parent cf4269a commit 9087186
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ function translateTypeAnnotation(
function isModuleInterface(node: $FlowFixMe) {
return (
node.type === 'TSInterfaceDeclaration' &&
node.extends.length === 1 &&
node.extends?.length === 1 &&
node.extends[0].type === 'TSExpressionWithTypeArguments' &&
node.extends[0].expression.name === 'TurboModule'
);
Expand Down

0 comments on commit 9087186

Please sign in to comment.