sql-formatter-org / sql-formatter

A whitespace formatter for different query languages
https://sql-formatter-org.github.io/sql-formatter/
MIT License
2.37k stars 405 forks source link

Cannot read properties of undefined (reading 'nestedBlockComments') #715

Closed MrSwitch closed 9 months ago

MrSwitch commented 9 months ago

Describe the bug

Given any SQL in JS, i.e. ...

const query = SQL`
    SELECT 1
`;

And using a .prettierrc.js like...

module.exports = {
    embeddedSqlTags: ['SQL'],
    embeddedSqlParser: 'mysql',
    dialect: 'mysql',
    indentStyle: 'tabularLeft',
    plugins: [
        'prettier-plugin-embed',
        'prettier-plugin-sql',
    ],
};

Expected behavior Expect no bug

Actual behavior But whilst it does format it the item, we also get the error...

TypeError: Cannot read properties of undefined (reading 'nestedBlockComments') at Tokenizer.buildRulesBeforeParams (file:node_modules/sql-formatter/dist/index.js:14969:20) at new Tokenizer (file:node_modules/sql-formatter/dist/index.js:14946:35) at dialectFromOptions (file:node_modules/sql-formatter/dist/index.js:15192:14) at createDialect (file:node_modules/sql-formatter/dist/index.js:15186:15) at formatDialect (file:node_modules/sql-formatter/dist/index.js:16614:24) at Object.print (file:node_modules/prettier-plugin-sql/lib/index.js:47:31) at callPluginPrintFunction (file:node_modules/prettier/index.mjs:22296:20) at printAstToDoc (file:node_modules/prettier/index.mjs:22251:22) at async textToDoc (file:node_modules/prettier/index.mjs:22218:16) at async Ur (file:node_modules/prettier-plugin-embed/dist/index.js:1437:15)

Usage

ps: Many thanks for this project it's awesome!

Sec-ant commented 9 months ago

I think you're using the option in a wrong way.

The dialect in the prettier-plugin-sql plugin is expecting a stringified JSON-like (will be parserd by JSOX) object, but you're setting it to a string 'mysql'. That's why the tokenizer throws this error.

You can discard the dialect option and simply use the language option to tell the plugin you're formatting mysql language.

Also, you don't need the embeddedSqlParser option if you're using the prettier-plugin-sql plugin to format your embedded sql code. embeddedSqlParser is only for the prettier-plugin-sql-cst plugin.

nene commented 9 months ago

Yep. As @Sec-ant said. Just use the language option. The main purpose of the dialect option is defining a custom SQL dialect.

Also be aware that indentStyle: tabularLeft is deprecated and will be removed in future versions of the formatter.

MrSwitch commented 9 months ago

Thankyou both @Sec-ant and @nene for pointing helping me out there.