Skip to content

Commit

Permalink
fix #16 and some regexes, messages, and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hchiam committed Jul 28, 2019
1 parent 455da16 commit 49018aa
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "custom-vscode-linter",
"displayName": "custom-vscode-linter",
"description": "Custom VSCode Linter!",
"version": "0.6.3",
"version": "0.6.4",
"publisher": "hchiam",
"repository": {
"type": "git",
Expand Down
14 changes: 7 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ export function activate(context: vscode.ExtensionContext) {
}

function check_ifIdWithoutNotNull(rangesToDecorate: vscode.DecorationOptions[]) {
let regex = /if ?\(([^=!)]*[iI][dD](?!\.)\b) ?[^=<>\r\n]*?\)/g;
let hoverMessage = 'An ID of 0 would evaluate to false. Consider: ${match[1]} != null';
let popupMessage = 'ID of 0 would evaluate to false. Consider adding "!= null" for if-statements containing IDs: ${errors.join(", ")}';
let regex = /if ?\(([^=)]*?I[dD](?!\.)\b) ?[^=<>\r\n]*?\)/g;
let hoverMessage = 'An ID of 0 would evaluate to falsy. !(0) is truthy. Consider: ${match[1]} != null';
let popupMessage = 'ID of 0 would evaluate to falsy. Consider adding "!= null" for if-statements containing IDs: ${errors.join(", ")}';
genericCheck(regex, hoverMessage, popupMessage, rangesToDecorate);
}

Expand Down Expand Up @@ -110,8 +110,8 @@ export function activate(context: vscode.ExtensionContext) {
}

function check_numberOfId(rangesToDecorate: vscode.DecorationOptions[]) {
let regex = /\WNumber\(([^!]?[^|)]*?I(d|D))\)/g;
let hoverMessage = 'If ${match[1]} is an ID but is empty, then Number(${match[1]}) evaluates to 0 (e.g. Number("") -> 0).';
let regex = /\WNumber\(([^|)]*?I(d|D))\)/g;
let hoverMessage = 'If ${match[1]} is an ID but is empty, then Number(${match[1]}) would evaluate to 0 (e.g. Number("") -> 0).';
let popupMessage = 'Number(someId) evaluates to 0 if someId is empty (e.g. "").';
genericCheck(regex, hoverMessage, popupMessage, rangesToDecorate);
}
Expand All @@ -134,13 +134,13 @@ export function activate(context: vscode.ExtensionContext) {
const endPos = activeEditor.document.positionAt(match.index + match[0].length);
const decoration = {
'range': new vscode.Range(startPos, endPos),
'hoverMessage': hoverMessage.replace(/\$\{match\[1\]\}/g, match[1]).replace(/\$\{match\[2\]\}/g, match[2]) // e.g. `An ID of 0 would evaluate to false. Consider: ${match[1]} != null`
'hoverMessage': hoverMessage.replace(/\$\{match\[1\]\}/g, match[1]).replace(/\$\{match\[2\]\}/g, match[2]) // e.g. `An ID of 0 would evaluate to falsy. Consider: ${match[1]} != null`
};
rangesToDecorate.push(decoration);
match = regexIdVariable.exec(text);
}
if (errors.length > 0) {
// e.g. let popupMessage = `ID of 0 would evaluate to false. Consider adding "!= null" for if-statements containing IDs: `;
// e.g. let popupMessage = `ID of 0 would evaluate to falsy. Consider adding "!= null" for if-statements containing IDs: `;
vscode.window.showInformationMessage('Line ' + firstLineNumber + ': ' + popupMessage.replace(/\$\{errors.join\(", "\)}/g, errors.join(', '))); // e.g. popupMessage + errors.join(', '));
}
}
Expand Down
8 changes: 6 additions & 2 deletions visually-check-this.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ Number(something.someID); // YES

getNumber(something.someId || somethingElse.someOtherId); // NO

if (!equipmentId) {} // NO
if (!equipmentId) {} // YES

Number(someId || -1) // NO
Number(-1 || someId) // NO
Number(-1 || someId) // NO

if (!someID) { // YES

}

0 comments on commit 49018aa

Please sign in to comment.