I would like the end result of my imports to be like this, via a tool that can automatically format my code onSave
:
import { Stack, StackProps, Duration, Resource } from "aws-cdk-lib";
import { LambdaStack } from "./lambda-Stack";
import { Construct } from "constructs";
How can I align all of the "from" statements vertically in VS Code? I’ve looked at both prettier and eslint.
2
Answers
Usually, with prettier or eslint you might want to limit printWidth to defined number of caracter per line. So imagine if you have many imports or long module name :
So my answer is not responding to "how can you align ‘from’ statements?" but it
may open another question ‘should you ?’
Here is a common way to indent imports :
Here is the eslint rule to auto indent your code : https://eslint.org/docs/latest/rules/object-curly-newline
example for
object-curly-newline
rule in eslint:PS:
Here some example of how I use it
Prettier doesn’t care what you want. 🙂 It’s an opinionated tool useful for applying consistency to code within teams (and for avoiding arguments about different formatting styles [although you just get arguments about whether to use Prettier instead]).
If you don’t want Prettier’s formatting, don’t use Prettier. There are other code formatters, some of which offer more control than Prettier does.
You could tell Prettier to ignore each of those import statements:
or
For now there’s no "block ignore" in Prettier for JavaScript (only for certain select other languages), although there’s an open request for one.