gilbsgilbs / babel-plugin-i18next-extract

Babel plugin that statically extracts i18next and react-i18next translation keys.
https://i18next-extract.netlify.com
MIT License
161 stars 37 forks source link

Updating @babel/types breaks typings #169

Closed gilbsgilbs closed 2 years ago

gilbsgilbs commented 4 years ago

Describe the bug

Upgrading @babel/types to 7.10.0+ breaks typings. For example:

function foo(path: NodePath) {
  if (nodePath.isMemberExpression()) {
    // nodePath used to be of type "NodePath<MemberExpression>" here.
    // But now is "NodePath<Node> & NodePath<MemberExpression>".

    // As a result, obj has a loose type: "NodePath<Node> | NodePath<Node>[]"
    // Used to be "NodePath<Expression>".
    const obj = path.get('object');

    // And then this breaks:
    obj.isIdentifier();
  }
}

Hard to figure out a proper fix and understand why this happens. The only workaround I found so far is too cumbersome to be acceptable:

function foo(path: NodePath) {
  if (nodePath.isMemberExpression()) {
    const obj = (path as NodePath<MemberExpression>).get('object');

    // now obj has expected type, we can do:
    obj.isIdentifier();
  }
}

Another problem:

function foo(path: NodePath<Node>) {}
function bar(expr: NodePath<CallExpression>) {
    // fails here
    //  ↓↓↓↓
    foo(expr);
}

Now fails with the following error:

Argument of type 'NodePath<CallExpression>' is not assignable to parameter of type 'NodePath<Node>'.
  Type 'Node' is not assignable to type 'CallExpression'.
    Type 'AnyTypeAnnotation' is missing the following properties from type 'CallExpression': callee, arguments, optional, typeArguments, typeParametersts(2345)

Waiting for a proper fix, #170 pinned @babel/types to the latest version that works: 7.9.6. Starting at 7.10.0 the breaking change occurs :man_shrugging: .

How to reproduce

Expected behavior

Types should be strong and not break.

What actually happens

Lot of typing errors, mostly related to the aforementioned issue. For example:

src/extractors/withTranslationHOC.ts:293:62 - error TS2345: Argument of type 'NodePath<Node> | NodePath<Node>[]' is not assignable to parameter of type 'NodePath<Node>'.
  Type 'NodePath<Node>[]' is not assignable to type 'NodePath<Node>'.

293         result.push(...findTFunctionCallsFromPropsAssignment(id));
                                                                 ~~

Your environment

irrelevant

Additional context

None

xDisfigure commented 4 years ago

taking a look at it

gilbsgilbs commented 4 years ago

Wow, thanks mate. FYI, I've asked the question on Babel's slack channel. I don't have a lot of hopes, but maybe it's a known issue :shrug: .