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

Prettier throws SyntaxError: Unexpected token in valid MDX function #13077

Closed
dimaMachina opened this issue Jul 3, 2022 · 4 comments
Closed
Labels
type:duplicate Issues that are a duplicate of a previous issue

Comments

@dimaMachina
Copy link

Prettier 2.7.1
Playground link

--parser mdx

Input:

export const getStaticProps = async () => {
  const pluginsData = await getPackagesData({ packageList: PACKAGES });

  const data = await Promise.all(
    pluginsData.map(async plugin => {
      const [description, content] = await buildMultipleMDX();

      return {
        ...plugin,
        description,
        content,
      };
    })
  );

  return {
    props: {
      ssg: { data },
    },
    revalidate: 60 * 60,
  };
};

Output:

SyntaxError: Unexpected token (2:72)
  1 | export const getStaticProps = async () => {
> 2 |   const pluginsData = await getPackagesData({ packageList: PACKAGES });
    |                                                                        ^

Expected behavior:

@dimaMachina dimaMachina changed the title Prettier has SyntaxError: Unexpected token in valid MDX function Prettier throws SyntaxError: Unexpected token in valid MDX function Jul 3, 2022
@kachkaev
Copy link
Member

kachkaev commented Jul 5, 2022

👋 @B2o5T! I see --parser mdx used for JS code in your playground example. Was this your intent or do you have *.mdx code that is not working? When your snippet is used with --parser babel, there is not parsing error:

Prettier 2.7.1
Playground link

--parser babel

Input:

export const getStaticProps = async () => {
  const pluginsData = await getPackagesData({ packageList: PACKAGES });

  const data = await Promise.all(
    pluginsData.map(async plugin => {
      const [description, content] = await buildMultipleMDX();

      return {
        ...plugin,
        description,
        content,
      };
    })
  );

  return {
    props: {
      ssg: { data },
    },
    revalidate: 60 * 60,
  };
};

Output:

export const getStaticProps = async () => {
  const pluginsData = await getPackagesData({ packageList: PACKAGES });

  const data = await Promise.all(
    pluginsData.map(async (plugin) => {
      const [description, content] = await buildMultipleMDX();

      return {
        ...plugin,
        description,
        content,
      };
    })
  );

  return {
    props: {
      ssg: { data },
    },
    revalidate: 60 * 60,
  };
};

@kachkaev kachkaev added the status:awaiting response Issues that require answers to questions from maintainers before action can be taken label Jul 5, 2022
@dimaMachina
Copy link
Author

@kachkaev yes I use this code in .mdx file, it's a totally valid code, but prettier breaks my code.

prettier-ignore-start/prettier-ignore-end also doesn't work in mdx2, so I can't ignore this block of code, but there is already PR that fixes the issue #12208 on 1 FEBRUARY! Please give attention to MDX community 😵‍💫

@kachkaev
Copy link
Member

kachkaev commented Jul 5, 2022

My brain’s pattern matching failed me because the example only had export const ... 🤦 😅 You are right though – this should be a valid MDX!

Prettier does not support MDX2 yet – see umbrella issue: #12209. In the meantime, for the code to parse, it is necessary to remove newlines from your JS blocks. I know it’s annoying because the workaround is not intuitive, but at least it works:

Prettier 2.7.1
Playground link

--parser mdx

Input:

export const getStaticProps = async () => {
  const pluginsData = await getPackagesData({ packageList: PACKAGES });
  const data = await Promise.all(
    pluginsData.map(async plugin => {
      const [description, content] = await buildMultipleMDX();
      return {
        ...plugin,
        description,
        content,
      };
    })
  );
  return {
    props: {
      ssg: { data },
    },
    revalidate: 60 * 60,
  };
};	

Output:

export const getStaticProps = async () => {
  const pluginsData = await getPackagesData({ packageList: PACKAGES });
  const data = await Promise.all(
    pluginsData.map(async (plugin) => {
      const [description, content] = await buildMultipleMDX();
      return {
        ...plugin,
        description,
        content,
      };
    })
  );
  return {
    props: {
      ssg: { data },
    },
    revalidate: 60 * 60,
  };
};

@kachkaev
Copy link
Member

kachkaev commented Jul 5, 2022

Closing as duplicate of #5795

@kachkaev kachkaev closed this as not planned Won't fix, can't repro, duplicate, stale Jul 5, 2022
@kachkaev kachkaev added type:duplicate Issues that are a duplicate of a previous issue and removed status:awaiting response Issues that require answers to questions from maintainers before action can be taken labels Jul 5, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type:duplicate Issues that are a duplicate of a previous issue
Projects
None yet
Development

No branches or pull requests

2 participants