我是Node.js的新手,我有一些问题。我在sonarqube中收到了一个错误,因为它定义了一个常量,而不是复制5次"-deleteFlag“。我怎样才能解决这个问题。
export class CCGuid {
"-deleteFlag": string;
"#text": string;
constructor(obj: any) {
if (typeof obj === "string") {
this["#text"] = obj;
this["-deleteFlag"] = "N";
} else {
try {
this["-deleteFlag"] = obj["-deleteFlag"];
} catch {
this["-deleteFlag"] = undefined;
}
try {
this["#text"] = obj["#text"];
} catch {
this["#text"] = undefined;
}
}
}
}发布于 2022-07-08 12:58:23
要解决这个问题,请停止多次声明类似的消息。我建议的是声明一次并在所有需要的地方使用,或者如果您必须在它们之间有一些细微的差别,创建一个接收消息的函数,并用消息键替换接收到的内容。
会是这样的:
const MessageCodeMapping = {
REQUEST_COMPLETED_SUCCESSFULLY: {
code: "1",
description: "REQUEST_COMPLETED_SUCCESSFULLY",
detailedDescription: "REQUEST_COMPLETED_SUCCESSFULLY",
},
INVALID_PASSWORD: (desc) => ({
code: "2",
description: "Invalid Password",
detailedDescription: desc || `Minimum of 8 and max of 16 characters - Requires 3 out of the following 4 options:
1. Lowercase characters
2. Uppercase characters
3. Numbers
4. Special characters.
Password is case sensitive.`,
}),
}
// Usage
// Returns what is hardcoded
MessageCodeMapping.INVALID_PASSWORD()
// Returns the object with the detailedDescription changed
MessageCodeMapping.INVALID_PASSWORD("Custom Message")发布于 2022-07-08 12:31:30
就像它在tin上说的那样:声明,比如说,const invalidPassword = "Invalid Password",并使用它而不是字符串。更多的抵抗打字,对开始。更容易改变,如果你想改变短期的未来。
而且,即使SonarQube不反对,我也会为代码"2"和"3"的重复长detailedDescription声明一个常量。
https://stackoverflow.com/questions/72911316
复制相似问题