BANG88/typescript-react-intl

Not correctly parsing Typescript output?

Closed this issue · 2 comments

jwulf commented

Hi there, first of all - thanks for putting this together!

I'm having an issue getting it to work in my TypeScript React project.

I'm using the example script, and I get empty objects back for all my files.

I added some console.log statements to your code to see what's going on.

The first is in findFirstJsxOpeningLikeElementWithName:

[ NodeObject {
    pos: 4020,
    end: 4147,
    flags: 0,
    transformFlags: undefined,
    parent: undefined,
    kind: 247,
    tagName:
     IdentifierObject {
       pos: 4021,
       end: 4037,
       flags: 0,
       parent: undefined,
       text: 'FormattedMessage' },
    attributes: [ [Object], [Object], pos: 4037, end: 4140 ] } ]
[]

The next is in main in the elements.attributes.forEach function:

attr:
NodeObject {
  pos: 4037,
  end: 4075,
  flags: 0,
  transformFlags: undefined,
  parent: undefined,
  kind: 250,
  name: IdentifierObject { pos: 4037, end: 4046, flags: 0, parent: undefined, text: 'id' },
  initializer:
   NodeObject {
     pos: 4047,
     end: 4075,
     flags: 0,
     transformFlags: undefined,
     parent: undefined,
     kind: 252,
     expression:
      TokenObject {
        pos: 4048,
        end: 4073,
        flags: 0,
        parent: undefined,
        kind: 9,
        text: 'Magikcraft.NotLoggedIn' } } }
attr:
NodeObject {
  pos: 4075,
  end: 4140,
  flags: 0,
  transformFlags: undefined,
  parent: undefined,
  kind: 250,
  name:
   IdentifierObject {
     pos: 4075,
     end: 4095,
     flags: 0,
     parent: undefined,
     text: 'defaultMessage' },
  initializer:
   NodeObject {
     pos: 4097,
     end: 4140,
     flags: 0,
     transformFlags: undefined,
     parent: undefined,
     kind: 252,
     expression:
      TokenObject {
        pos: 4098,
        end: 4138,
        flags: 0,
        parent: undefined,
        kind: 9,
        text: 'Go to Minecraft and type \'/spellbook\'' } } }

And finally in main just before return res.concat(dm);. This is res:

[ { id: undefined, defaultMessage: undefined } ]

It looks like elements.attributes.forEach is parsing the wrong properties of the returned nodes. Could this be a TypeScript compiler version issue?

You are requiring typescript, but the package.json doesn't specify a version. The version in my project is 2.1.4.

jwulf commented

Changing line 71 of lib/index.js to:

element.attributes.forEach(function (attr) { return attr.name && (msg[attr.name.text] = attr.initializer.expression.text); });

fixes it for me.

Ping @jwulf I just merged your PR and publish a new version to npm. Thanks