Skip to content

Commit e04fefb

Browse files
authored
Merge pull request #597 from neo4j/code-cleanup
Apply sonarcloud recommendations
2 parents 3a0a0f0 + a72f230 commit e04fefb

File tree

6 files changed

+28
-28
lines changed

6 files changed

+28
-28
lines changed

src/Environment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ export class CypherEnvironment {
8181
}
8282

8383
public addExtraParams(params: Record<string, Param>): void {
84-
Object.entries(params).forEach(([key, param]) => {
84+
for (const [key, param] of Object.entries(params)) {
8585
this.addNamedParamReference(key, param);
86-
});
86+
}
8787
}
8888

8989
public addNamedParamReference(name: string, param: Param): void {

src/clauses/mixins/sub-clauses/WithSetRemove.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ export abstract class WithSetRemove extends Mixin {
7878
return undefined;
7979
}
8080

81-
return this.subClauses[this.subClauses.length - 1];
81+
return this.subClauses.at(-1);
8282
}
8383
}

src/clauses/mixins/sub-clauses/WithWhere.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ import { Mixin } from "../Mixin";
3131
/** @inline */
3232
type VariableLike = Variable | Literal | PropertyRef;
3333

34+
/** @inline */
35+
type WhereInputOrTarget = Predicate | Variable | PropertyRef | undefined;
36+
3437
export abstract class WithWhere extends Mixin {
3538
protected whereSubClause: Where | undefined;
3639

@@ -39,25 +42,22 @@ export abstract class WithWhere extends Mixin {
3942
*/
4043
public where(input: Predicate | undefined): this;
4144
public where(target: Variable | PropertyRef, params: Record<string, VariableLike>): this;
42-
public where(input: Predicate | Variable | PropertyRef | undefined, params?: Record<string, VariableLike>): this {
43-
this.updateOrCreateWhereClause(input, params);
45+
public where(inputOrTarget: WhereInputOrTarget, params?: Record<string, VariableLike>): this {
46+
this.updateOrCreateWhereClause(inputOrTarget, params);
4447
return this;
4548
}
4649

4750
/** Shorthand for `AND` operation after a `WHERE` subclause
4851
*/
4952
public and(input: Predicate | undefined): this;
5053
public and(target: Variable | PropertyRef, params: Record<string, VariableLike>): this;
51-
public and(input: Predicate | Variable | PropertyRef | undefined, params?: Record<string, VariableLike>): this {
52-
this.updateOrCreateWhereClause(input, params);
54+
public and(inputOrTarget: WhereInputOrTarget, params?: Record<string, VariableLike>): this {
55+
this.updateOrCreateWhereClause(inputOrTarget, params);
5356
return this;
5457
}
5558

56-
private updateOrCreateWhereClause(
57-
input: Predicate | Variable | PropertyRef | undefined,
58-
params?: Record<string, VariableLike>
59-
): void {
60-
const whereInput = this.createWhereInput(input, params);
59+
private updateOrCreateWhereClause(inputOrTarget: WhereInputOrTarget, params?: Record<string, VariableLike>): void {
60+
const whereInput = this.createWhereInput(inputOrTarget, params);
6161
if (!whereInput) {
6262
return;
6363
}
@@ -71,17 +71,17 @@ export abstract class WithWhere extends Mixin {
7171
}
7272

7373
private createWhereInput(
74-
input: Predicate | Variable | PropertyRef | undefined,
74+
inputOrTarget: WhereInputOrTarget,
7575
params: Record<string, VariableLike> | undefined
7676
): Predicate | undefined {
77-
if (!input) {
77+
if (!inputOrTarget) {
7878
return undefined;
7979
}
80-
if (input instanceof Variable || input instanceof PropertyRef) {
81-
const generatedOp = this.variableAndObjectToOperation(input, params ?? {});
80+
if (inputOrTarget instanceof Variable || inputOrTarget instanceof PropertyRef) {
81+
const generatedOp = this.variableAndObjectToOperation(inputOrTarget, params ?? {});
8282
return generatedOp;
8383
}
84-
return input;
84+
return inputOrTarget;
8585
}
8686

8787
/** Transforms a simple input into an operation sub tree */

src/clauses/utils/mixin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export function mixin(...mixins: AbstractConstructorType<any>[]) {
4646
* Based on https://www.typescriptlang.org/docs/handbook/mixins.html
4747
*/
4848
function applyMixins<T>(baseClass: ConstructorType<T>, mixins: AbstractConstructorType<unknown>[]): ConstructorType<T> {
49-
mixins.forEach((baseCtor) => {
50-
Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
49+
for (const baseCtor of mixins) {
50+
for (const name of Object.getOwnPropertyNames(baseCtor.prototype)) {
5151
if (name !== "constructor") {
5252
// Base class constructor takes precedence over mixins
5353
Object.defineProperty(
@@ -57,8 +57,8 @@ function applyMixins<T>(baseClass: ConstructorType<T>, mixins: AbstractConstruct
5757
Object.getOwnPropertyDescriptor(baseCtor.prototype, name) ?? Object.create(null)
5858
);
5959
}
60-
});
61-
});
60+
}
61+
}
6262

6363
return baseClass;
6464
}

src/expressions/operations/comparison.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe("comparison operations", () => {
5656

5757
describe("IS NORMALIZED", () => {
5858
test("isNormalized (IS NORMALIZED) operator", () => {
59-
const stringLiteral = new Cypher.Literal("the \\u212B char");
59+
const stringLiteral = new Cypher.Literal(String.raw`the \u212B char`);
6060
const query = new Cypher.Return([Cypher.isNormalized(stringLiteral), "normalized"]);
6161
const { cypher } = query.build();
6262
expect(cypher).toMatchInlineSnapshot(`"RETURN \\"the \\\\u212B char\\" IS NORMALIZED AS normalized"`);
@@ -65,15 +65,15 @@ describe("comparison operations", () => {
6565
test.each(["NFC", "NFD", "NFKC", "NFKD"] as const)(
6666
"isNormalized (IS NORMALIZED) operator with normalization type %s",
6767
(type) => {
68-
const stringLiteral = new Cypher.Literal("the \\u212B char");
68+
const stringLiteral = new Cypher.Literal(String.raw`the \u212B char`);
6969
const query = new Cypher.Return([Cypher.isNormalized(stringLiteral, type), "normalized"]);
7070
const { cypher } = query.build();
7171
expect(cypher).toBe(`RETURN "the \\u212B char" IS ${type} NORMALIZED AS normalized`);
7272
}
7373
);
7474

7575
test("isNotNormalized (IS NOT NORMALIZED) operator with one predicate", () => {
76-
const stringLiteral = new Cypher.Literal("the \\u212B char");
76+
const stringLiteral = new Cypher.Literal(String.raw`the \u212B char`);
7777
const query = new Cypher.Return([Cypher.isNotNormalized(stringLiteral), "notNormalized"]);
7878
const { cypher } = query.build();
7979
expect(cypher).toMatchInlineSnapshot(
@@ -84,7 +84,7 @@ describe("comparison operations", () => {
8484
test.each(["NFC", "NFD", "NFKC", "NFKD"] as const)(
8585
"isNotNormalized (IS NOT NORMALIZED) operator with normalization type %s",
8686
(type) => {
87-
const stringLiteral = new Cypher.Literal("the \\u212B char");
87+
const stringLiteral = new Cypher.Literal(String.raw`the \u212B char`);
8888
const query = new Cypher.Return([Cypher.isNotNormalized(stringLiteral, type), "notNormalized"]);
8989
const { cypher } = query.build();
9090
expect(cypher).toBe(`RETURN "the \\u212B char" IS NOT ${type} NORMALIZED AS notNormalized`);

src/utils/escape.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
const ESCAPE_SYMBOL_REGEX = /`/g;
2121

2222
/** These names must be escaped for variables */
23-
const RESERVED_VAR_NAMES = ["contains", "in", "where", "is"];
23+
const RESERVED_VAR_NAMES = new Set(["contains", "in", "where", "is"]);
2424

2525
/** Escapes a Node label string */
2626
export function escapeLabel(label: string): string {
@@ -40,7 +40,7 @@ export function escapeProperty(propName: string): string {
4040

4141
/** Escapes a variable name if needed */
4242
export function escapeVariable(varName: string): string {
43-
if (RESERVED_VAR_NAMES.includes(varName.toLowerCase())) {
43+
if (RESERVED_VAR_NAMES.has(varName.toLowerCase())) {
4444
return escapeString(varName);
4545
}
4646
return escapeIfNeeded(varName);
@@ -61,7 +61,7 @@ function escapeIfNeeded(str: string): string {
6161

6262
function escapeString(str: string): string {
6363
const normalizedStr = normalizeString(str);
64-
const escapedStr = normalizedStr.replace(ESCAPE_SYMBOL_REGEX, "``");
64+
const escapedStr = normalizedStr.replaceAll(ESCAPE_SYMBOL_REGEX, "``");
6565
return `\`${escapedStr}\``;
6666
}
6767

0 commit comments

Comments
 (0)