Skip to content

Commit 0e460ea

Browse files
committed
chore: implement replaceNodeText utility and update consistent nullish comparison rule to use it
1 parent 7fad2d5 commit 0e460ea

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

.pkgs/tsl-local/dist/index.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ import { match } from "ts-pattern";
22
import { defineRule } from "tsl";
33
import { SyntaxKind } from "typescript";
44

5-
//#region src/utils/ast.ts
6-
function getStartAndEnd(node) {
5+
//#region src/utils/fix.ts
6+
/**
7+
* Replaces the text of a node with new text.
8+
* @param node The node to replace.
9+
* @param newText The new text to insert.
10+
* @returns An object containing the start and end positions of the node and the new text.
11+
*/
12+
function replaceNodeText(node, newText) {
713
return {
814
start: node.getStart(),
9-
end: node.getEnd()
15+
end: node.getEnd(),
16+
newText
1017
};
1118
}
1219

@@ -35,13 +42,7 @@ const consistentNullishComparison = defineRule(() => ({
3542
node,
3643
suggestions: [{
3744
message: offendingChild === node.left ? `Replace with 'null ${newOperatorText} ${node.right.getText()}'.` : `Replace with '${node.left.getText()} ${newOperatorText} null'.`,
38-
changes: [{
39-
...getStartAndEnd(node.operatorToken),
40-
newText: newOperatorText
41-
}, {
42-
...getStartAndEnd(offendingChild),
43-
newText: "null"
44-
}]
45+
changes: [replaceNodeText(node.operatorToken, newOperatorText), replaceNodeText(offendingChild, "null")]
4546
}]
4647
});
4748
} }

.pkgs/tsl-local/src/rules/consistent-nullish-comparison.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { match } from "ts-pattern";
22
import { defineRule } from "tsl";
33
import { SyntaxKind } from "typescript";
44

5-
import { getStartAndEnd } from "../utils";
5+
import { replaceNodeText } from "../utils";
66

77
/**
88
* Rule to enforce the use of `== null` or `!= null` for nullish comparisons.
@@ -38,14 +38,8 @@ export const consistentNullishComparison = defineRule(() => ({
3838
? `Replace with 'null ${newOperatorText} ${node.right.getText()}'.`
3939
: `Replace with '${node.left.getText()} ${newOperatorText} null'.`,
4040
changes: [
41-
{
42-
...getStartAndEnd(node.operatorToken),
43-
newText: newOperatorText,
44-
},
45-
{
46-
...getStartAndEnd(offendingChild),
47-
newText: "null",
48-
},
41+
replaceNodeText(node.operatorToken, newOperatorText),
42+
replaceNodeText(offendingChild, "null"),
4943
],
5044
},
5145
],

.pkgs/tsl-local/src/utils/fix.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Replaces the text of a node with new text.
3+
* @param node The node to replace.
4+
* @param newText The new text to insert.
5+
* @returns An object containing the start and end positions of the node and the new text.
6+
*/
7+
export function replaceNodeText(node: { getStart: () => number; getEnd: () => number }, newText: string) {
8+
return {
9+
start: node.getStart(),
10+
end: node.getEnd(),
11+
newText,
12+
} as const;
13+
}

.pkgs/tsl-local/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./ast";
2+
export * from "./fix";

0 commit comments

Comments
 (0)