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

Error with cjs version of prettier-plugin-sql Cannot read properties of undefined (reading 'parse') #332

Closed
Brookke opened this issue Dec 16, 2023 · 7 comments · Fixed by #333

Comments

@Brookke
Copy link
Contributor

Brookke commented Dec 16, 2023

I am getting the following error when trying to use this plugin prettier on save in vscode:

TypeError: Cannot read properties of undefined (reading 'parse')
    at Object.print (/Users/user/Developer/app/node_modules/.pnpm/prettier-plugin-sql@0.17.1_prettier@3.1.1/node_modules/prettier-plugin-sql/lib/index.cjs:213:63)

I have established that the commonjs version of the plugin does not import JSOX correctly. The bundled code is as follows:

'use strict';

var jsox = require('jsox');
...
        let formatted = typeof value === "string" ? sqlFormatter.format(value, __spreadProps(__spreadValues({}, options), {
          params: params == null ? void 0 : jsox.JSOX.parse(
            params
          ),
          paramTypes: paramTypes == null ? void 0 : jsox.JSOX.parse(
            paramTypes
          )
        })) : parser.sqlify(value, { type, database });
     ...

Swapping, jsox.JSOX.parse to jsox.parse fixes the issue.

@JounQin
Copy link
Member

JounQin commented Dec 16, 2023

Would you like to raise a PR which will be appreciated!

@Brookke
Copy link
Contributor Author

Brookke commented Dec 16, 2023

Happy to help, however I don't have a proper fix yet other than directly modifying the bundled code. I think it might be a rollup config issue, I will spend some more time looking into how rollup is setup here

@JounQin
Copy link
Member

JounQin commented Dec 16, 2023

export const JSOX: typeof JSON

It needs to be changed to

const JSOX = typeof JSON
export = JSOX

And the ESM version should be changed accordingly.

You can juat raise the PR and I'll review and guide to finish.

@JounQin
Copy link
Member

JounQin commented Dec 16, 2023

@Brookke
Copy link
Contributor Author

Brookke commented Dec 16, 2023

export const JSOX: typeof JSON

It needs to be changed to

const JSOX = typeof JSON

export = JSOX

And the ESM version should be changed accordingly.

You can juat raise the PR and I'll review and guide to finish.

Yep I tried this locally when I was investigating. It seems like import { JSOX } from 'jsox' is valid and in CommonJS it's var jsox = require('jsox'). I assumed they'd be something in roll up that could fix this for us. But you're right ideally it'd be fixed upstream

@JounQin
Copy link
Member

JounQin commented Dec 16, 2023

We need to support both of them at the same time.

// shim.d.ts
const JSOX: typeof JSON & { JSOX?: typeof JSON}

expot = JSOX

// index.ts
import * as _JSOX from 'jsox'

// workaround for inconsistent cjs/esm exports
const JSOX = _JSOX.JSOX || _JSOX

@Brookke
Copy link
Contributor Author

Brookke commented Dec 16, 2023

Good idea, I'm happy to open a pr tomorrow with those changes (it's late UK time right now)

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

Successfully merging a pull request may close this issue.

2 participants