Skip to content

Commit

Permalink
commitlint: resolve strict build errors
Browse files Browse the repository at this point in the history
I had to use downlevelIteration flag because typeScript is
unable to iterate over certain types of objects without the
--downlevelIteration flag or a --target of es2015 or higher.

```
commitlint/plugins.ts:94:29 - error TS2802: Type 'IterableIterator<[number, string]>' can only be iterated through when using the '--downlevelIteration' flag or with a '--target' of 'es2015' or higher.
commitlint/plugins.ts:216:29 - error TS2802: Type 'Set<unknown>' can only be iterated through when using the '--downlevelIteration' flag or with a '--target' of 'es2015' or higher.
commitlint/plugins.ts:222:29 - error TS2802: Type 'Set<unknown>' can only be iterated through when using the '--downlevelIteration' flag or with a '--target' of 'es2015' or higher.
```

This error occurs when a function declaration is inside a
block in strict mode when targeting ‘ES3’ or ‘ES5’. One
way to fix this is to assign a function expression to a
variable name instead.

```
commitlint/plugins.ts:19:26 - error TS1251: Function declarations are not allowed inside blocks in strict mode when targeting 'ES3' or 'ES5'. Class definitions are automatically in strict mode.
```

To fix this error, we can add "ES2021" or "ES2021.String" to
the lib array in your tsconfig.json file.

```
commitlint/helpers.ts:191:14 - error TS2550: Property 'replaceAll' does not exist on type 'string'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2021' or later
```
  • Loading branch information
Mersho committed Sep 18, 2023
1 parent 3a7d592 commit 30aeaf9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
57 changes: 28 additions & 29 deletions commitlint/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@ import { Helpers } from "./helpers";

export abstract class Plugins {
public static bodyProse(rawStr: string) {
function paragraphHasValidEnding(paragraph: string): boolean {
let paragraphWords = paragraph.split(" ");
let lastWordInParagraph = paragraphWords[paragraphWords.length - 1];
let isParagraphEndingWithUrl =
Helpers.isValidUrl(lastWordInParagraph);
if (isParagraphEndingWithUrl) {
return true;
}

let endingChar = paragraph[paragraph.length - 1];
if (
endingChar === "." ||
endingChar === ":" ||
endingChar === "!" ||
endingChar === "?"
) {
return true;
}
if (
endingChar === ")" &&
paragraph.length > 1 &&
paragraphHasValidEnding(paragraph[paragraph.length - 2])
) {
return true;
}
return false;
}

let offence = false;

rawStr = rawStr.trim();
Expand All @@ -16,35 +44,6 @@ export abstract class Plugins {
bodyStr = Helpers.removeAllCodeBlocks(bodyStr).trim();

if (bodyStr !== "") {
function paragraphHasValidEnding(paragraph: string): boolean {
let paragraphWords = paragraph.split(" ");
let lastWordInParagraph =
paragraphWords[paragraphWords.length - 1];
let isParagraphEndingWithUrl =
Helpers.isValidUrl(lastWordInParagraph);
if (isParagraphEndingWithUrl) {
return true;
}

let endingChar = paragraph[paragraph.length - 1];
if (
endingChar === "." ||
endingChar === ":" ||
endingChar === "!" ||
endingChar === "?"
) {
return true;
}
if (
endingChar === ")" &&
paragraph.length > 1 &&
paragraphHasValidEnding(paragraph[paragraph.length - 2])
) {
return true;
}
return false;
}

for (let paragraph of Helpers.splitByEOLs(bodyStr, 2)) {
paragraph = paragraph.trim();

Expand Down
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"compilerOptions": {
"strict": true,
"esModuleInterop": true,
"downlevelIteration": true,
"lib": ["ES2021"],
},
}

0 comments on commit 30aeaf9

Please sign in to comment.