Is there a formatter similar to Prettier that can format Node.js code with aligned variable declarations like this?
const PRICE_BUTTER = 1.00;
const PRICE_MILK=3.00;
const PRICE_EGGS=6.95;
To:
const PRICE_BUTTER = 1.00;
const PRICE_MILK = 3.00;
const PRICE_EGGS = 6.95;
Prettier doesn't have this options
2 Answers 2
Here is the tool and approaches that can help:
ESLint + plugin or manual rules While ESLint is mainly for linting, some plugins can help with alignment, like:
eslint-plugin-align-assignments This plugin aligns = and : in variable declarations and object literals.
Install:
npm install --save-dev eslint eslint-plugin-align-assignments
Add to your .eslintrc.js:
module.exports = {
plugins: ['align-assignments'],
rules: {
'align-assignments/align-assignments': ['error', { requiresOnly: false }],
},
};
Then run:
npx eslint --fix yourfile.js
Comments
You are asking for a formatter which generates unuseful 'whitespaces' in your code!!!
But the expert recommendation is to avoid useless 'whitespacing' as much as possible, and that is what the formatters out there follows while formatting your code.
According to my knowledge there is not such a extension/tool that can meet your desires.
But if there's a formatter like that, which u want, and that is a BIG IFFFF, that's gotta be a hell of a development mistake...