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

Cannot read property 'name' of null #66

Closed
MoOx opened this issue Jan 24, 2017 · 4 comments
Closed

Cannot read property 'name' of null #66

MoOx opened this issue Jan 24, 2017 · 4 comments

Comments

@MoOx
Copy link

MoOx commented Jan 24, 2017

I have the following error with 0.20.0 (which was not present with 0.10.1

TypeError: src/static/url-as-html.js: Cannot read property 'name' of null
    at annotate (/.../node_modules/babel-plugin-flow-react-proptypes/lib/index.js:84:26)
    at functionVisitor (/.../node_modules/babel-plugin-flow-react-proptypes/lib/index.js:111:7)
    at PluginPass.FunctionDeclaration (/.../node_modules/babel-plugin-flow-react-proptypes/lib/index.js:178:16)
    at newFn (/.../node_modules/babel-traverse/lib/visitors.js:276:21)
    at NodePath._call (/.../node_modules/babel-traverse/lib/path/context.js:76:18)
    at NodePath.call (/.../node_modules/babel-traverse/lib/path/context.js:48:17)
    at NodePath.visit (/.../node_modules/babel-traverse/lib/path/context.js:105:12)
    at TraversalContext.visitQueue (/.../node_modules/babel-traverse/lib/context.js:150:16)
    at TraversalContext.visitSingle (/.../node_modules/babel-traverse/lib/context.js:108:19)
    at TraversalContext.visit (/.../node_modules/babel-traverse/lib/context.js:192:19)
    at Function.traverse.node (/.../node_modules/babel-traverse/lib/index.js:114:17)

Here is the file (it's not a react components, but I babelify an entire folder)

// @flow

import React from "react"
import ReactDOMserver from "react-dom/server"
import { match, RouterContext as RouterContextProvider } from "react-router"
import { Provider as ReduxContextProvider } from "react-redux"

import DefaultHtml from "../components/Html"
import pathToUri from "../_utils/path-to-uri"
import PhenomicContextProvider from "../components/ContextProvider"
import serialize from "../_utils/serialize"
import minifyCollection from "../loader/minify"

export default function(
  url: string,
  options: PhenomicStaticConfig,
  Html: Function = DefaultHtml
): Promise<string> {

  const {
    baseUrl,
    assetsFiles,
    routes,
    collection,
    metadata,
    store,
  } = options

  const render = ReactDOMserver[
    (!options.clientScripts)
    ? "renderToStaticMarkup"
    : "renderToString"
  ]

  return new Promise((resolve, reject) => {
    try {
      match(
        {
          routes,
          location: url,
          basename: baseUrl.pathname,
        },
        (error, redirectLocation, renderProps) => {
          if (error) {
            return reject(error)
          }
          else if (redirectLocation) {
            // TODO add a redirect page à la "jekyll redirect plugin"
            throw new Error (
              "phenomic (static) doesn't handle redirection yet"
            )
          }
          else if (!renderProps) {
            throw new Error (
              "phenomic (static) doesn't handle page not found yet. " +
              "You are not supposed so see this message because this code is " +
              "not supposed to be executed the way thing are, so this can " +
              "be a react-router issue. Check out opened issue to find a " +
              "workaround: https://github.com/MoOx/phenomic/issues"
            )
          }

          const collectionMin = minifyCollection(collection)

          /* eslint-disable react/no-multi-comp */

          const renderBody = () => render(
            <PhenomicContextProvider
              collection={ collectionMin }
              metadata={ metadata }
            >
              <ReduxContextProvider store={ store }>
                <RouterContextProvider { ...renderProps } />
              </ReduxContextProvider>
            </PhenomicContextProvider>
          )

          const renderScript = () => {
            if (options.clientScripts) {
              const initialState = {
                ...store.getState(),
                // only keep current page as others are not necessary
                pages: {
                  [url]: store.getState().pages[url],
                },
              }
              const script = (
                `window.__COLLECTION__ = ${ serialize(collectionMin) };` +
                `window.__INITIAL_STATE__ = ${ serialize(initialState) }`
              )

              return (
                <script dangerouslySetInnerHTML={{ __html: script }} />
              )
            }

            return null
          }

          // write htmlString as html files
          return resolve(
            // render html document as simple html
            "<!doctype html>" +
            ReactDOMserver.renderToStaticMarkup(
              React.createElement(
                Html,
                {
                  ...assetsFiles && {
                    css: assetsFiles.css
                    ? assetsFiles.css.map(
                      (fileName) => pathToUri(baseUrl.pathname, fileName)
                    )
                    : [],
                    js: options.clientScripts && assetsFiles.js
                    ? assetsFiles.js.map(
                      (fileName) => pathToUri(baseUrl.pathname, fileName)
                    )
                    : [],
                  },
                  renderBody,
                  renderScript,
                }
              )
            )
          )
        }
      )
    }
    catch (err) {
      reject(err)
    }
  })
}

@charlesoconor
Copy link

charlesoconor commented Feb 6, 2017

I'm running into this issue to. I believe it has to do with exporting and anonymous functions. I've just flipped the plugin of for any file I do this in. It would be great to have another work around.

Edit
estree/estree#98
seems like babel has broken backwards compatibility other plugins are running into the same issue

@mhaas
Copy link
Contributor

mhaas commented May 27, 2017

Can you provide a more minimal example which shows this behavior? Ideally, it would be a test case similar to what we have in src/tests/

@mhaas
Copy link
Contributor

mhaas commented May 30, 2017

I can reproduce this (or rather, a similar error) and will fix soon.

mhaas pushed a commit to mhaas/babel-plugin-flow-react-proptypes that referenced this issue May 31, 2017
Any function containing a react component is considered a functional
React component by isFunctionalReactComponent().

This leads to false positives for functions which just use or generate
instances of React components. These factory functions do not
follow the general contract of react components. In particular, the
first argument to the function may not be props.

It is hard to detect these cases in isFunctionalReactComponent. Instead,
we relax the sanity check in getPropsForTypeAnnotation: if a typeAnnotation
exists but does not have the type we expect, we do not fail hard.
We just don't generate proptypes.
mhaas pushed a commit to mhaas/babel-plugin-flow-react-proptypes that referenced this issue May 31, 2017
brigand pushed a commit that referenced this issue May 31, 2017
* Fix issue #66

Any function containing a react component is considered a functional
React component by isFunctionalReactComponent().

This leads to false positives for functions which just use or generate
instances of React components. These factory functions do not
follow the general contract of react components. In particular, the
first argument to the function may not be props.

It is hard to detect these cases in isFunctionalReactComponent. Instead,
we relax the sanity check in getPropsForTypeAnnotation: if a typeAnnotation
exists but does not have the type we expect, we do not fail hard.
We just don't generate proptypes.

* Add missing changelog for 3.1.2

* Add changelog entry for #66
@brigand
Copy link
Owner

brigand commented May 31, 2017

Fixed in #99, published as 3.1.3.

@brigand brigand closed this as completed May 31, 2017
Mayank1791989 pushed a commit to Mayank1791989/babel-plugin-flow-react-proptypes that referenced this issue Jul 21, 2017
* Fix issue brigand#66

Any function containing a react component is considered a functional
React component by isFunctionalReactComponent().

This leads to false positives for functions which just use or generate
instances of React components. These factory functions do not
follow the general contract of react components. In particular, the
first argument to the function may not be props.

It is hard to detect these cases in isFunctionalReactComponent. Instead,
we relax the sanity check in getPropsForTypeAnnotation: if a typeAnnotation
exists but does not have the type we expect, we do not fail hard.
We just don't generate proptypes.

* Add missing changelog for 3.1.2

* Add changelog entry for brigand#66
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants