Beitrag vom: 02.12.2024 (updated: 03.12.2024 06:32) Let's start with the simpler issue: Stylechecks. Basically it all boils down to maintaining the same style over the full application. It doesn't really matter WHICH style, but it must be the same! or tabs vs spaces * Btw I'm a spaces guy, but convention over sympathy! There is also the analyzer which sorts imports in a predictable manner. This is optional, but gives each header of a file the same order. For details about the order you can look here. This is actually giving us a headstart on customers finding bugs. Each linter obviously has a small false positive rate, but the ruleset itself makes sure to exclude some classes of errors before they even happen! The full ruleset of biome is here. It has some very trivial checks like no distracting elements which prohibits marquee and blink, but also far more advanced checks. My favorite category is nursery which contains legacy JS features which are surpassed by advanced Syntax. ... Aaaaand a lot more! We should use linters at every stage of our development process. The earlier the better and we WILL be using themsoon in our processes. For the huge amount of reasons we should use biome whereever possible. For the most common editors here the extension/plugin so that you can benefit directly. Just make sure the red squiggly lines disappear and autoformatting is active Afterwards be sure to:Stylecheck
Why?
Tools
Examples
'
vs "
where applicable or the same amount of spacesTrivial: Spacing
// FROM
@Injectable()
export class JwtService {
decodeToken (token: string): any {
return jwt.decode(token);
}
}
// TO
@Injectable()
export class JwtService {
decodeToken(token: string): any {
return jwt.decode(token);
}
}
// FROM 2 spaces
describe('ReportsController', () => {
let controller: ReportsController;
beforeEach(async () => {
// TO tabs
describe('ReportsController', () => {
let controller: ReportsController;
beforeEach(async () => {
Trivial: Newlines
// FROM
@Module({
controllers: [IntegrationController],
providers: [IntegrationService, PrismaService, ScrapingService, CustomerIoService, JwtService]
})
// TO
@Module({
controllers: [IntegrationController],
providers: [
IntegrationService,
PrismaService,
ScrapingService,
CustomerIoService,
JwtService,
],
})
Linting
Linter warnings examples
// 1. no banned types
code-block.ts:1:10 lint/complexity/noBannedTypes ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Don’t use ’{}’ as a type.
> 1 │ const n: {} = 0
│ ^^
// 2. complexity
code-block.js:1:10 lint/complexity/noExcessiveCognitiveComplexity ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Excessive complexity of 21 detected (max: 15).
> 1 │ function tooComplex() {
│ ^^^^^^^^^^
2 │ for (let x = 0; x < 10; x++) {
3 │ for (let y = 0; y < 10; y++) {
// 3. for .. of vs Array.forEach
code-block.js:1:1 lint/complexity/noForEach ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Prefer for…of instead of forEach.
> 1 │ els.forEach((el) => {
│ ^^^^^^^^^^^^^^^^^^^^^
> 2 │ f(el);
> 3 │ })
│ ^^
// 4. no useless catch
code-block.js:4:5 lint/complexity/noUselessCatch ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The catch clause that only rethrows the original error is useless.
2 │ doSomething();
3 │ } catch(e) {
> 4 │ throw e;
│ ^^^^^^^^
// 5. no uselss ternary
const a = foo === 1 ? false : true; // just use foo !== 1
code-block.js:1:9 lint/complexity/noUselessTernary FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unnecessary use of boolean literals in conditional expression.
> 1 │ const a = foo === 1 ? false : true;
│ ^^^^^^^^^^^^^^^^^^^^^^^^
for .. of ..
is easier to read than Array.forEach(...)
. Also a performance issue!FIXABLE
lints: This one can be auto corrected!Biome in your editor
npm install --save-dev --save-exact @biomejs/biome
biome format FOLDER/FILE
or biome format --write FOLDER/FILE
biome lint FOLDER/FILE
or biome lint --write FOLDER/FILE
biome check FOLDER/FILE