Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visitor throws Cannot read property 'kind' of undefined using [email protected] #38558

Closed
lukaskl opened this issue May 13, 2020 · 6 comments Β· Fixed by #38596
Closed

Visitor throws Cannot read property 'kind' of undefined using [email protected] #38558

lukaskl opened this issue May 13, 2020 · 6 comments Β· Fixed by #38596
Assignees
Labels
Bug A bug in TypeScript

Comments

@lukaskl
Copy link

lukaskl commented May 13, 2020

TypeScript Version:

  • πŸ”΄3.9.2
  • πŸ”΄3.9.1-rc
  • πŸ”΄3.9.0-beta
  • βœ…3.8.3 (it work with previous versions)

Search Terms:
Visitor TypeError kind undefined 85032

Code

Unknown (Error is thrown at node_modules/typescript/lib/typescript.js:85032:35)

But this part triggers it

export interface ColorProps {
  style?: CSSProperties
}

const Color = styled(({ style }: ColorProps) => (
  <div
    style={{
      ...style,
      background: `anything`,
    }}
  />
))``

Expected behavior:

It compiles

Actual behavior:

    TypeError: Cannot read property 'kind' of undefined

      at visitObjectLiteralExpression (node_modules/typescript/lib/typescript.js:85032:35)
      at visitJavaScript (node_modules/typescript/lib/typescript.js:83441:28)
      at visitor (node_modules/typescript/lib/typescript.js:83384:24)
      at visitNode (node_modules/typescript/lib/typescript.js:74768:23)
      at Object.visitEachChild (node_modules/typescript/lib/typescript.js:75209:108)
      at visitJavaScript (node_modules/typescript/lib/typescript.js:83491:31)
      at visitor (node_modules/typescript/lib/typescript.js:83384:24)
      at visitNodes (node_modules/typescript/lib/typescript.js:74819:48)
      at Object.visitEachChild (node_modules/typescript/lib/typescript.js:75034:53)
      at visitObjectLiteralExpression (node_modules/typescript/lib/typescript.js:85057:23)

Playground Link:

Related Issues:

@lissein
Copy link
Contributor

lissein commented May 14, 2020

I managed to reduce the reproduction code down to:

const f = (props: any) => (...args: any) => ''; 

f({
    background: `${1} anything`,
    ...{ test: 1 },
})``

The issue is in the es2015 transformer so any target above es2015 will be fine. In fact, the issue is in the object literal visitor when the node contains "es2015 features (eg. template literals, arrow functions, ...)" that need to be transformed. So you can reproduce the same crash with this code using arrow functions:

const f = (props: any) => (...args: any) => ''; 

f({
    background: (() => 1)(),
    ...{ test: 1 },
})``

This code, for instance does not crash because the object literal doesn't have any es2015 features in it.

const f = (props: any) => (...args: any) => ''; 

f({
    background: 'anything',
    ...{ test: 1 },
})``

I'll try to fix this.


Until the bug is fixed, @lukaskl you can use a string instead of a template literal for the background variable or use an external variable to store the template literal outside of the object literal like that:

export interface ColorProps {
  style?: CSSProperties
}

const Color = styled(({ style }: ColorProps) => {
  const background = `${1 + 1}`;
  return (
    <div
      style={{
        ...style,
        background,
      }}
    />
  );
})``

@lukaskl
Copy link
Author

lukaskl commented May 14, 2020

Thank you @lissein for investigating this!

To add another workaround, it is also possible to split this into two definitions, e.g.:

const ColorUnstyled = ({ style }: ColorProps) => (
  <div
    style={{
      ...style,
      background: `anything`,
    }}
  />
)

const Color = styled(ColorUnstyled)``

@lissein
Copy link
Contributor

lissein commented May 14, 2020

After more investigations, I confirmed that the code crashing is in the es2015 transformer but the root cause is not in here. The bug is actually in the es2018 transformer.
Here is an example of code with a target es2017 (< es2018):

const f = (props: any) => (...args: any) => ''; 

f({
    background: (() => 1)(),
    ...{ test: 1 },
})``

const a = {
    ...{ test: 1 },
}
"use strict";
const f = (props) => (...args) => '';
f({
    background: (() => 1)(),
    ...{ test: 1 },
}) ``;
const a = Object.assign({ test: 1 });

As you can see, the second spread is transpiled correctly but the first one isn't.

@elibarzilay
Copy link
Contributor

(This is most likely very unrelated to #38383...)

elibarzilay added a commit to elibarzilay/TypeScript that referenced this issue May 15, 2020
This problem was introduced in 70399e1 (from PR microsoft#23801), which added
a `visitTaggedTemplateExpression` case for `TaggedTemplateExpression`,
before that, it would fallback to the default of `visitNode`.  So re-add
that happen in `processTaggedTemplateExpression`.

Since it doesn't hurt, I left a `Debug.checkDefined(property.name)`
instead of `!`-ing it.

Fixes microsoft#38558.
elibarzilay added a commit to elibarzilay/TypeScript that referenced this issue May 15, 2020
This problem was introduced in 70399e1 (from PR microsoft#23801), which added
a `visitTaggedTemplateExpression` case for `TaggedTemplateExpression`,
before that, it would fallback to the default of `visitNode`.  So re-add
that happen in `processTaggedTemplateExpression`.

Since it doesn't hurt, I left a `Debug.checkDefined(property.name)`
instead of `!`-ing it.

Fixes microsoft#38558.
elibarzilay added a commit that referenced this issue May 15, 2020
This problem was introduced in 70399e1 (from PR #23801), which added
a `visitTaggedTemplateExpression` case for `TaggedTemplateExpression`,
before that, it would fallback to the default of `visitNode`.  So re-add
that happen in `processTaggedTemplateExpression`.

Since it doesn't hurt, I left a `Debug.checkDefined(property.name)`
instead of `!`-ing it.

Fixes #38558.
@chaselal
Copy link

chaselal commented Jul 21, 2020

tsc throws the same error for me with TypeScript version 3.9.7:

Chase@DESKTOP-P21MP39 MINGW64 ~/company/companyWeb/projects/company.Server.Web ((b99b96a0de...))
$ node_modules/typescript/bin/tsc
C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:82931
                throw e;
                ^

TypeError: Cannot read property 'kind' of undefined
    at visitObjectLiteralExpression (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:70631:35)
    at visitJavaScript (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:69611:28)
    at visitor (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:69554:24)
    at visitNode (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:63010:23)
    at Object.visitEachChild (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:63260:45)
    at visitParenthesizedExpression (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:70365:23)
    at visitJavaScript (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:69625:28)
    at visitor (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:69554:24)
    at Object.visitNode (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:63010:23)
    at transformFunctionBody (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:70307:37)

My project is quite large. I don't know which code tsc fails on.

Edit: This happens when I try to upgrade directly from typescript version 3.7.3 to 3.9.7. Version 3.7.3 works for me.

@peterj35-dd
Copy link

peterj35-dd commented Aug 11, 2021

Why has this issue been closed, what is the resolution without having to make manual code changes? I'm facing the same issue on 3.9.10, targetting ES5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
6 participants