guybedford / es-module-lexer

Low-overhead lexer dedicated to ES module parsing for fast analysis
MIT License
917 stars 48 forks source link

parse error #105

Closed yehuozhili closed 2 years ago

yehuozhili commented 2 years ago

hello, i try to parse my code , but it cause a parse error

const { init, parse } = require('es-module-lexer');

(async () => {
  // either await init, or call parse asynchronously
  // this is necessary for the Web Assembly boot
  await init;
  const [imports, exports] = parse(
    `
  import React, { PureComponent } from 'react';
  import PropTypes from 'prop-types';
  import classNames from 'classnames';
  import styles from './index.module.scss';

  export default class LoadingMod extends PureComponent {
    static propTypes = {
      horizontal: PropTypes.bool,
      imgSrc: PropTypes.string,
      showImg: PropTypes.bool,
      height: PropTypes.string,
    };

    static defaultProps = {
      horizontal: false,
      imgSrc: '',
      showImg: true,
      height: '200px',
    };

    render() {
      const { className, children, showImg, imgSrc, height, noborder, horizontal } = this.props;
      const clazz = classNames(styles.container, styles.loading, className, {
        [styles.horizontal]: horizontal,
        [styles.bordered]: !noborder,
      });

      return (
        <div className={ clazz } style={{ height }}>
          {showImg ? (
            <div className={styles.imgbox}>
              <img src={imgSrc} alt="" />
            </div>
          ) : null}
          {children ? <div className={styles.desc}>{children}</div> : null}
        </div>
      );
    }
  }
  `,
  );
  console.log(imports, exports);
})();

result:

(node:204776) UnhandledPromiseRejectionWarning: Error: Parse error @:34:19
    at parse (D:\project\prr\wd-operation-wrap\node_modules\_es-module-lexer@0.10.0@es-module-lexer\dist\lexer.cjs:1:404)
    at D:\project\prr\wd-operation-wrap\test.js:7:30

Is there something wrong with my source file ?

guybedford commented 2 years ago

Hi @yehuozhili the reason for this is that this project only supports parsing JS syntax not the JSX syntax in use here. This project isn't going to be extended to support JSX but you can run a JSX preparser first before using this, or use another similar analysis tool that supports JSX instead. This was also discussed in https://github.com/guybedford/es-module-lexer/issues/47.