ds300 / react-native-typescript-transformer

Seamlessly use TypeScript with React Native
MIT License
657 stars 50 forks source link

TypeError: Cannot read property 'tokens' of undefined #62

Closed dluksza closed 6 years ago

dluksza commented 6 years ago

I'm using jest-ts-preprocessor.js from https://github.com/ds300/react-native-typescript-transformer/issues/39#issuecomment-356764981 and after upgrading to version 1.2.6 I'm getting this error:

TypeError: Cannot read property 'tokens' of undefined

      18 |       src
      19 |     }).ast;
    > 20 |     return generate.default(
      21 |       ast,
      22 |       {
      23 |         filename: file,

      at new Generator (node_modules/babel-generator/lib/index.js:51:22)
      at Object.exports.default (node_modules/babel-generator/lib/index.js:19:13)
      at Object.process (tools/jest-ts-preprocessor.js:20:28)
ds300 commented 6 years ago

Hi Dariusz! 👋The transformer is now async, and returns a promise. So you need to update your preprocessor to wait for the promise to resolve. Let me know how you get on!

On Thu, 24 May 2018, 08:56 Dariusz Łuksza, notifications@github.com wrote:

I'm using jest-ts-preprocessor.js from #39 (comment) https://github.com/ds300/react-native-typescript-transformer/issues/39#issuecomment-356764981 and after upgrading to version 1.2.6 I'm getting this error:

TypeError: Cannot read property 'tokens' of undefined

  18 |       src
  19 |     }).ast;
> 20 |     return generate.default(
  21 |       ast,
  22 |       {
  23 |         filename: file,

  at new Generator (node_modules/babel-generator/lib/index.js:51:22)
  at Object.exports.default (node_modules/babel-generator/lib/index.js:19:13)
  at Object.process (tools/jest-ts-preprocessor.js:20:28)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ds300/react-native-typescript-transformer/issues/62, or mute the thread https://github.com/notifications/unsubscribe-auth/ABL1qRfPH4To_CE4jI2ith1OU1A9u2yQks5t1meggaJpZM4ULvUR .

dluksza commented 6 years ago

Now I do wait for the transformer to resolve, but the ast property of result doesn't have code field nor is a string. Therefore I'm getting this error:

TypeError: Jest: a transform's `process` function must return a string, or an object with `code` key containing this string.

      at ScriptTransformer.transformSource (node_modules/jest-runtime/build/script_transformer.js:316:15)
ds300 commented 6 years ago

Yes. As mentioned here you need to use babel-generator to render the ast as a string.

Adapting the code in that comment to be async:

var tsTransformer = require('react-native-typescript-transformer')
var rnTransformer = require('react-native/jest/preprocessor')
var generate = require('babel-generator')

var preprocessor = Object.assign({}, rnTransformer, {
  async process (src, file) {
    const {ast} = await tsTransformer.transform({
      filename: file,
      localPath: file,
      options: {
        dev: true,
        platform: '',
        projectRoot: '',
      },
      src,
    })

    return generate.default(ast, {
      filename: file,
      retainLines: true
    }, src)
  },
})

module.exports = preprocessor

(I haven't tested this)

ds300 commented 6 years ago

Argh, it might not be possible: https://github.com/facebook/jest/issues/2711

In which case you'll need to revert to react-native-typescript-transformer@1.2.5

ds300 commented 6 years ago

Actually I'm going to revert to that myself. Updating source-map has raised quite a few issues and I don't believe the alleged perf wins are worth the extra work it has caused for me and other people whose builds it broke.

dluksza commented 6 years ago

This looks same as my solution, and it produces the same error.

dluksza commented 6 years ago

OK, will revert to 1.2.5. Thanks for the help and quick responses!

ds300 commented 6 years ago

Alright 1.2.9 is the same as 1.2.5.