0

i am creating new Vscode extention using prettier custom plugin for formatting code as requirement, my whole code work properly even extension is running properly but my plugin file that i describe here can't print consol.log values like node and text and for that reason i can not format my file using my extension

prettier-plugin/newplugin.js

const babelParser = require('@babel/parser');

module.exports = {
    parsers: {
        customJS: {
            parse: (text) => {
                **console.log(" ~ file: newplug.js:7 ~ text:", text)**
                // Directly use @babel/parser
                return babelParser.parse(text, {
                    sourceType: 'module',
                    plugins: [
                        'jsx',
                        'typescript',
                        'classProperties',
                        'javascript' /* any other plugins you want to support */,
                    ],
                });
            },
            astFormat: 'customJS',
            locStart: (node) => node.start,
            locEnd: (node) => node.end,
        },
    },
    printers: {
        customJS: {
            print: (path, options, print) => {
                const node = path.getValue();
                
                **console.log(" ~ file: newplug.js:27 ~ node:", node);**
                
                if (node.type === 'StringLiteral') {
                    node.value = node.value.replace(/"/g, "'");
                    return `'${node.value}'`;
                }
        
                if (node.type === 'ObjectExpression') {
                    console.log(" ~ file: newplug.js:36 ~ node:", node)
                    const properties = path.map.print(print, "properties");
                    return `{\n${properties.join(",\n")}\n}`;
                  }

                return path.call(print);
            },
        },
    },

};

formateCustomPreitter.js

import * as prettier from 'prettier';

//formatting custome code which tack options from .prettierrc file and text 
const formatCustomCode = async (text: string) => {
    const configPath = await prettier.resolveConfigFile(__dirname);
    const options = await prettier.resolveConfig(configPath as string);
    console.log(
        ' ~ file: formatWithCustomPrettier.ts:21 ~ formatCustomCode ~ options:',
        options
    );

    return prettier.format(text, {
        ...options,
        parser: 'babel',
    });
};

export default formatCustomCode;

.pritterrc

{   
    "plugins": ["./src/prettier-plugin/newplug.js"]
}


please provide solution if you have any idea about this 
thank you

0 Answers0