Skip to content

Commit

Permalink
commitlint: resolve some 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 c16d11b commit 08b7af1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
58 changes: 30 additions & 28 deletions commitlint/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,36 @@ 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,34 +46,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 08b7af1

Please sign in to comment.