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

Unexpected token in code example #1321

Open
Sly36 opened this issue Mar 27, 2019 · 43 comments
Open

Unexpected token in code example #1321

Sly36 opened this issue Mar 27, 2019 · 43 comments

Comments

@Sly36
Copy link

Sly36 commented Mar 27, 2019

I had to install react-styleguidist with yarn (vs npm) to have PROPS & METHODSi working.

Now it works but i have Syntax errors for all my components (with defaultExample set to true and components with their own examples too):

Current behavior

in the .md file

``` jsx
<Card>test</Card>

results in

SyntaxError: Unexpected token (1:6)
1 : <React.Fragment><Card>test</Card></React.Fragment>;
          ^

and

``` jsx
// comment
<Card>test</Card>

results in

SyntaxError: Unexpected token (2:11)
1 : // comment
2 : <Card>test</Card>
               ^

here is an example of a simple component code :

import React from 'react';
import PropTypes from "prop-types";
import "../../../styles/elements/card.less"

const Card = ({children, id}) =>
    <div id={id} className={"Card"}>
        {children}
    </div>;

Card.propTypes = {
    id: PropTypes.string
};

export default Card;

Expected behavior
The code example should display my component

@vollmerr
Copy link

I am getting this same error. Everything works fine on version 9.0.4, but get the above error on 9.0.5 for all my components.

@Sly36
Copy link
Author

Sly36 commented Mar 28, 2019

Thank you, downgrading to 9.0.4 fix the problem

stonebk pushed a commit to zillow/create-react-styleguide that referenced this issue Apr 5, 2019
…leguidist

BREAKING CHANGE: react-styleguidist@9.x has breaking changes to how examples are loaded, read more here:
https://github.com/styleguidist/react-styleguidist/releases/tag/v9.0.0. Also note, react-styleguidist is
hard pinned to `9.0.4` due to the following issue: styleguidist/react-styleguidist#1321.
stonebk pushed a commit to zillow/create-react-styleguide that referenced this issue Apr 5, 2019
…leguidist

BREAKING CHANGE: react-styleguidist@9.x has breaking changes to how examples are loaded, read more here:
https://github.com/styleguidist/react-styleguidist/releases/tag/v9.0.0. Also note, react-styleguidist is
hard pinned to `9.0.4` due to the following issue: styleguidist/react-styleguidist#1321.
Betree added a commit to opencollective/opencollective-frontend that referenced this issue Apr 23, 2019
@JonathanLehner
Copy link

downgrading works, but doesn't seem like a nice solution. what is the underlying issue?

@karsai5
Copy link

karsai5 commented May 15, 2019

Having the same issue. Also 9.0.4 gets picked up as having vulnerabilities by npm audit which doesn't make it a super viable alternative.

@Betree
Copy link

Betree commented May 17, 2019

It seems that this issue doesn't affect everyone so maybe we can get more info by sharing our configs. Below is ours, we use it with the following libraries (see the full list here):

  • @babel/cli: 7.1.2
  • @babel/core: 7.1.2
  • @babel/node: 7.0.0
  • next: 8.1.0
  • react: 16.8.6

https://github.com/opencollective/opencollective-frontend/blob/master/styleguide.config.js

const fs = require('fs');
const path = require('path');
const fileExistsCaseInsensitive = require('react-styleguidist/lib/scripts/utils/findFileCaseInsensitive');

module.exports = {
  assetsDir: 'styleguide',
  getExampleFilename(componentPath) {
    const parsedPath = path.parse(componentPath);
    const parentDirName = parsedPath.dir.split('src/components/')[1] || '';
    const parentDirPath = path.join(__dirname, 'styleguide', 'examples', parentDirName);

    if (!fs.existsSync(parentDirPath)) {
      return false;
    }

    const examplePath = path.join(parentDirPath, `${parsedPath.name}.md`);
    return fileExistsCaseInsensitive(examplePath) || false;
  },
  pagePerSection: true,
  moduleAliases: {
    components: path.resolve(__dirname, 'src/components'),
  },
  sections: [
    {
      name: 'Atoms',
      components: 'src/components/Styled*.js',
      description: 'Base design atoms.',
    },
    {
      name: 'UI',
      content: 'styleguide/pages/UI.md',
      components: 'src/components/*.js',
      ignore: ['src/components/Contribute*.js', 'src/components/Styled*.js', 'src/components/collective-page/*.js'],
    },
    {
      name: 'Collective Page',
      components: 'src/components/collective-page/*.js',
      description: 'These components are used on the donate/contribute flow.',
    },
    {
      name: 'Contribution Flow',
      components: 'src/components/Contribute*.js',
      description: 'These components are used on the donate/contribute flow.',
    },
    {
      name: 'Grid',
      content: 'styleguide/pages/Grid.md',
      sections: [
        {
          name: 'Box',
          content: 'styleguide/examples/Box.md',
        },
        {
          name: 'Flex',
          content: 'styleguide/examples/Flex.md',
        },
      ],
    },
  ],
  skipComponentsWithoutExample: true,
  styleguideComponents: {
    Wrapper: path.join(__dirname, 'styleguide/Wrapper'),
  },
  styles: {
    Blockquote: {
      blockquote: {
        borderLeft: '3px solid grey',
        margin: '16px 0',
        padding: '0 32px',
      },
    },
  },
  title: 'Open Collective Frontend Style Guide',
  usageMode: 'expand',
  webpackConfig: {
    resolve: { extensions: ['.js', '.json'] },
    stats: { children: false, chunks: false, modules: false, reasons: false },
    module: {
      rules: [
        {
          test: /\.(js|jsx)$/,
          exclude: /node_modules/,
          use: [{ loader: 'babel-loader', options: { cacheDirectory: true } }],
        },
        {
          test: /components\/.*\.(svg)$/,
          use: {
            loader: 'url-loader',
            options: {
              limit: 1000000,
            },
          },
        },
      ],
    },
  },
};

@WaldoJeffers
Copy link
Contributor

I can confirm this, but it does not seem to be related to v9.0.4 or v9.0.5. I had the same issue without updating any dependencies in my project. I simply did rm -rf node_modules followed by yarn install.

After some digging, it seems to be related to:

I got rid of it by upgrading all my eslint-* dependencies, but it's still a weird issue

@matsgm
Copy link

matsgm commented May 22, 2019

I can confirm this, but it does not seem to be related to v9.0.4 or v9.0.5. I had the same issue without updating any dependencies in my project. I simply did rm -rf node_modules followed by yarn install.

After some digging, it seems to be related to:

I got rid of it by upgrading all my eslint-* dependencies, but it's still a weird issue

It looks like upgrading ESLint fixed it 😃

@karsai5
Copy link

karsai5 commented May 24, 2019

Unfortunately upgrading eslint didn't fix it for me. But it seemed to be related to the version of acorn I had. Running the following worked for me:

  • npm update acorn --depth 20
  • npm dedupe

I suspect I had some dependency that was using the old version of acorn that was causing the problem.

@stoff
Copy link

stoff commented May 26, 2019

Seeing this in a nascent project, currently very basic deps/config. I've tried reinstalling and updating acorn to no avail; interestingly I have not yet installed eslint so have not tried updating that lib yet (will look into a deep update when time permits). Sharing my configs for record:
package.json:

{
  "name": "fepdx-react-component-library",
  "version": "0.1.0",
  "description": "React components to encapsulate the design for FEPDX projects.",
  "main": "index.js",
  "scripts": {
    "start": "styleguidist server",
    "build": "rimraf dist && babel src --out-dir dist",
    "test": "jest"
  },
  "author": "Christoph Saxe",
  "license": "ISC",
  "dependencies": {
    "@emotion/core": "^10.0.10",
    "emotion-theming": "^10.0.10",
    "prop-types": "^15.7.2",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-styleguidist": "^9.1.2",
    "webpack": "^4.32.2"
  },
  "devDependencies": {
    "@babel/cli": "^7.4.4",
    "@babel/core": "^7.4.5",
    "@babel/preset-env": "^7.4.5",
    "@babel/preset-react": "^7.0.0",
    "@emotion/babel-preset-css-prop": "^10.0.9",
    "babel-jest": "^24.8.0",
    "babel-loader": "^8.0.6",
    "jest": "^24.8.0",
    "react-test-renderer": "^16.8.6",
    "rimraf": "^2.6.3"
  }
}

styleguide.config.js:

module.exports = {
  webpackConfig: {
    module: {
      rules: [
        {
          test: /\.js?$/,
          exclude: /node_modules/,
          loader: 'babel-loader',
        }
      ]
    }
  },
  components: 'src/components/**/[A-Z]*.js',
};

@ManishBobde

This comment has been minimized.

@michaelkutuzov

This comment has been minimized.

@antonin87

This comment has been minimized.

@tonyjeffrey
Copy link

I have the same problem. 9.0.4 works but 9.0.5 have the error.

@cisen
Copy link

cisen commented Jul 30, 2019

yarn add acorn acorn-jsx

It works for me!

@swashcap
Copy link

swashcap commented Aug 4, 2019

Changing from npm to yarn solved this problem in my project. Seems like something's wrong in the dependency tree? #1321 (comment) and other comments on this issue indicate this.

@roblevintennis
Copy link
Contributor

roblevintennis commented Aug 12, 2019

Yeah, my colleague is anti shorthand so we don't have this properly configured (maybe I'll convince him to switch with this!). So, I fixed it by doing the downgrade with:
yarn add --dev --exact react-styleguidist@9.0.4

@cicely-f

This comment has been minimized.

@dustin-ruetz
Copy link

dustin-ruetz commented Aug 21, 2019

I was experiencing the same "unexpected token" issue in all of my .md React component examples in one of my libraries when using the latest version of react-styleguidist (currently 9.1.16).

To expand on @cisen's fix posted above, I determined that the issue was with the latest version of acorn (currently 7.0.0). Explicitly installing the latest 6.* version of acorn (currently 6.3.0) as a dev dependency fixed the issue for me.

Specific steps:

# remove dependencies and delete lock file
rm -rf node_modules && rm -rf package-lock.json

# install + add acorn to devDependencies
npm install --save-dev acorn@^6.3.0

# re-install everything from package.json
npm install

Additional context:

  • I didn't want to switch my entire local dev/CI workflow from NPM to Yarn; and
  • I couldn't rollback to a previous version of react-styleguidist because I'm using React hooks in my code examples.

@rafegoldberg
Copy link

rafegoldberg commented Aug 21, 2019

@dustin-ruetz's solution worked for me!

rm -rf node_modules && rm -rf package-lock.json  # nix dependencies + lockfile
npm install --save-dev acorn@^6.3.0              # save acorn as dev dependency
npm install                                      # reinstall e'erything else

@mancioshell

This comment has been minimized.

@MacZel

This comment has been minimized.

@malyzeli
Copy link

Seems that explicitly installing acorn helps, but it didn't work for me until I cleared npm cache.

npm cache clean --force
rm -rf node_modules package-lock.js
npm install --save-dev acorn@6
npm install

@sapegin
Copy link
Member

sapegin commented Oct 29, 2019

I guess some of your project dependencies depends on Acorn 7, because Styleguidist depends only on 6.x. And all the solutions above is how to trick npm/yarn to use Acorn 6 with Stylegudist.

And help with actually fixing the issue is very much appreciated.

@sapegin
Copy link
Member

sapegin commented Oct 29, 2019

Buble doesn't support Acorn 7:

https://github.com/bublejs/buble/blob/2da2ddb00f751a266fa07759a370a99059e21ce6/package.json#L73

And looks like something weird is happening when Acorn 7 is in the dependency tree. Buble tries to use Acorn 6 from its own node_modules but it doesn't work for some reason. Here might be some difference in module resolution between npm and Yarn, and why Yarn works when npm doesn't.

I've made a simple failing test:

package.json:

{
  "name": "acorn-test",
  "private": true,
  "dependencies": {
    "acorn": "7.1.0",
    "acorn-jsx": "5.1.0",
    "buble": "0.19.8"
  }
}

test.js:

var buble = require('buble');
var result = buble.transform(`<Button />`);

And when I run node test.js I see an error:

SyntaxError: Unexpected token (1:8)
    at Object.pp$4.raise (/Users/asapegin/klsdafafs/node_modules/buble/node_modules/acorn/dist/acorn.js:2825:15)
    at Object.pp.unexpected (/Users/asapegin/klsdafafs/node_modules/buble/node_modules/acorn/dist/acorn.js:689:10)
    at Object.jsx_parseIdentifier (/Users/asapegin/klsdafafs/node_modules/acorn-jsx/index.js:249:14)
    at Object.jsx_parseNamespacedName (/Users/asapegin/klsdafafs/node_modules/acorn-jsx/index.js:258:23)
    at Object.jsx_parseAttribute (/Users/asapegin/klsdafafs/node_modules/acorn-jsx/index.js:335:24)
    at Object.jsx_parseOpeningElementAt (/Users/asapegin/klsdafafs/node_modules/acorn-jsx/index.js:348:35)
    at Object.jsx_parseElementAt (/Users/asapegin/klsdafafs/node_modules/acorn-jsx/index.js:370:33)
    at Object.jsx_parseElement (/Users/asapegin/klsdafafs/node_modules/acorn-jsx/index.js:428:19)
    at Object.parseExprAtom (/Users/asapegin/klsdafafs/node_modules/acorn-jsx/index.js:435:21)
    at Object.pp$3.parseExprSubscripts (/Users/asapegin/klsdafafs/node_modules/buble/node_modules/acorn/dist/acorn.js:2089:21)

Looks at the stack trace I see that Buble uses Acorn 6.3.0 from it's own dependencies but it fails anyway. But looks like acorn-jsx will use whatever the latest version of Acorn is available in the tree.

@sapegin
Copy link
Member

sapegin commented Oct 29, 2019

Also Buble seems to be in maintenance mode now, so we should consider migrating to something else:

bublejs/buble#220

@iansan5653
Copy link

I've been trying to narrow the problem down a bit more:

The test you posted still fails when package.json is:

{
  "name": "acorn-test",
  "private": true,
  "dependencies": {
    "acorn": "^7.1.0",
    "buble": "0.19.8"
  }
}

acorn-jsx has the following dependency:

  "peerDependencies": {
    "acorn": "^6.0.0 || ^7.0.0"
  },

So when buble uses the acorn-jsx plugin to extend acorn, acorn-jsx is perfectly OK with pulling in the ^7.1.0 version instead of 6, and this causes the problem. So buble (and any other module that is not compatible with acorn^7.0.0, should be relying not on acorn-jsx^5.0.0, but on ^4.0.0.

In short this is definitely something that should be fixed upstream.

@iansan5653
Copy link

iansan5653 commented Oct 30, 2019

EDIT: Never mind. This causes all examples to go to Error: Unknown transform 'asyncAwait' because we need the recent version of buble at the moment.

A possible fix here would be to lock buble to =0.19.4 which is the most recent version that depended on acorn-jsx^4.0.0. Not the prettiest solution but it would fix the problem until you're able to move away from Buble altogether or at least until the issue I opened over there gets resolved, which might take time as it's not an actively developed project.

Changing package.json in the aforementioned test to:

{
  "name": "acorn-test",
  "private": true,
  "dependencies": {
    "acorn": "^7.1.0",
    "buble": "=0.19.4"
  }
}

works just fine.

@malyzeli
Copy link

malyzeli commented Oct 31, 2019

Also Buble seems to be in maintenance mode now, so we should consider migrating to something else:

bublejs/buble#220

@sapegin Sorry for offtopic considering scope of this issue but what is the reason for using something like buble instead of battle-tested babel? If there are any articles/discussions answering my question, please point me there. Thank you!

@sapegin
Copy link
Member

sapegin commented Oct 31, 2019

@malyzeli Primarily its size: babel-standalone is gigantic and we'll need to find a way to lazy load it somehow which is tricky because we need it to render all the examples.

@JustFly1984
Copy link

We also have same issue. https://react-google-maps-api-docs.netlify.com/ Nothing helps.

@sapegin
Copy link
Member

sapegin commented Nov 3, 2019

@JustFly1984 This kind of comments don't add anything to the discussion and significantly contribute to maintainers burnout. Now I have to either lock this thread to prevent future spam comment, which will also prevent folks who are actually trying to help from posting useful comments. Or unsubscribe myself to stop receiving spam comments — my own mental health is definitely more important to me than someone's problem with their project.

@JustFly1984
Copy link

@sapegin sorry mate, I didn't mean to be rude. I use your open source project in my open source project.

I'm building my project with tsdx, and it has acorn@7 in dependencies, so I can't add acorn@6.3.0 to resolution in package.json, cos it breaks build.

@iansan5653
Copy link

iansan5653 commented Nov 27, 2019

Looks like acornjs/acorn#887 fixed this! Since the release of acorn@6.4.0 I'm able to run the test that @sapegin posted, and after running npm update acorn --depth 20 in my project, I'm able to use Styleguidist again. ❤️

@sergdenisov
Copy link
Contributor

@iansan5653 I've made that fix in #1495.

@Betree
Copy link

Betree commented Dec 16, 2019

Fix worked for us. Thanks!

@iamchathu
Copy link

Tried it. Still have the issue "react-styleguidist": "^10.6.1"

@JustFly1984
Copy link

@iamchathu check if you have semicolons everywhere. I had issues with files until I’ve set semicolons in place. This is not obvious, and it could be helpful if there was no semicolons required.

@iamchathu
Copy link

@JustFly1984 I'm using to prettier rules to set semi colons and I double checked but seems there is an issue yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests