JS: Update OK-style comments to $-style

This commit is contained in:
Asger F
2025-02-06 13:34:01 +01:00
parent 7e5c24a8ec
commit 9be041e27d
536 changed files with 4408 additions and 4762 deletions

View File

@@ -1,36 +1,33 @@
// NOT OK
typeof a === 'array';
typeof a === 'array'; // $ Alert
// OK
typeof b == 'string';
// OK
typeof c != "string";
// OK
"number" !== typeof 23;
// OK
'object' == typeof null;
// OK
typeof es6 === 'symbol';
switch (typeof a) {
// OK
case 'undefined':
// NOT OK
case 'null':
case 'null': // $ Alert
}
// OK
switch (msg) {
case 'null':
case typeof a:
}
// NOT OK
(typeof a) === 'array';
(typeof a) === 'array'; // $ Alert
// JScript extensions
typeof a === 'unknown' || typeof a === 'date';

View File

@@ -1,7 +1,7 @@
var a = [], // OK
b = [1], // OK
c = [1, 2], // OK
d = [1, , 2], // NOT OK
e = [1,], // OK
f = [1, 2, ,], // NOT OK
g = [,1]; // NOT OK
var a = [],
b = [1],
c = [1, 2],
d = [1, , 2], // $ Alert
e = [1,],
f = [1, 2, ,], // $ Alert
g = [,1]; // $ Alert

View File

@@ -1,8 +1,7 @@
// NOT OK
[1, 2, 3].map(function(x) x * x);
[1, 2, 3].map(function(x) x * x); // $ Alert
// OK
[1, 2, 3].map(function(x) { return x * x; });
// OK
[1, 2, 3].map((x) => x * x);

View File

@@ -4,24 +4,24 @@ class C {
class D extends C {
constructor() {
super(); // OK
super();
}
}
let c = new C(); // OK
C(); // NOT OK
new (x=>x); // NOT OK
c.m(); // OK
new c.m(); // NOT OK - but not flagged
let c = new C();
C(); // $ Alert
new (x=>x); // $ Alert
c.m();
new c.m(); // $ MISSED: Alert
var o = {
f: function() {},
g() {}
};
o.f(); // OK
new o.f(); // OK
o.g(); // OK
new o.g(); // NOT OK - but not flagged
o.f();
new o.f();
o.g();
new o.g(); // $ MISSED: Alert
function f(b) {
var g;
@@ -31,31 +31,31 @@ function f(b) {
g = (() => {});
console.log();
if (!b)
g(); // OK
g();
else
new g(); // OK
new g();
}
function* g() {}
async function h() {}
new g() // NOT OK
new h() // NOT OK
new g() // $ Alert
new h() // $ Alert
C.call(); // NOT OK
C.apply(); // NOT OK
C.call(); // $ Alert
C.apply(); // $ Alert
class E {
static call() {}
static apply() {}
}
E.call(); // OK
E.apply(); // OK
E.call();
E.apply();
function invoke(fn) {
if (typeof fn === "function" && fn.hasOwnProperty("foo")) {
fn(); // OK
fn();
}
}
invoke(C);

View File

@@ -1,3 +1,3 @@
function A() {}
new A(); // OK
String(""); // OK
new A();
String("");

View File

@@ -1,6 +1,6 @@
function A() {}
A(); // OK
A();
function MyString() {}
String = MyString;
new String(); // OK
new String();

View File

@@ -1,2 +1,2 @@
Array(45); // OK
new Array(45); // OK
Array(45);
new Array(45);

View File

@@ -48,13 +48,13 @@ function RobustPoint4(x, y) {
new RobustPoint4(23, 42);
RobustPoint4(56, 72);
// OK: Error is an external function
// OK - Error is an external function
new Error();
Error();
class C {}
new C();
C(); // NOT OK, but flagged by IllegalInvocation
C(); // OK - flagged by IllegalInvocation
(function() {
function A(x) {
@@ -64,5 +64,5 @@ C(); // NOT OK, but flagged by IllegalInvocation
A.call({}, 23);
})();
new Point(42, 23); // NOT OK, but not flagged since line 6 above was already flagged
Point(56, 72); // NOT OK, but not flagged since line 7 above was already flagged
new Point(42, 23); // OK - not flagged since line 6 above was already flagged
Point(56, 72); // OK - not flagged since line 7 above was already flagged

View File

@@ -1,11 +1,11 @@
var o1 = { __proto__: null }; // OK
Object.setPrototypeOf(o1, Function.prototype); // OK
Object.create(class{}); // OK
Function.prototype.isPrototypeOf(o1); // OK
o1.__proto__ = new Date(); // OK
var o1 = { __proto__: null };
Object.setPrototypeOf(o1, Function.prototype);
Object.create(class{});
Function.prototype.isPrototypeOf(o1);
o1.__proto__ = new Date();
var o2 = { __proto__: undefined }; // NOT OK
Object.setPrototypeOf(o2, 42); // NOT OK
Object.create(true); // NOT OK
"function".isPrototypeOf(o2); // NOT OK
var o2 = { __proto__: undefined }; // $ Alert
Object.setPrototypeOf(o2, 42); // $ Alert
Object.create(true); // $ Alert
"function".isPrototypeOf(o2); // $ Alert

View File

@@ -1,43 +1,38 @@
// BAD: Loop upper bound is off-by-one
for (var i = 0; i <= args.length; i++) {
for (var i = 0; i <= args.length; i++) { // $ Alert - Loop upper bound is off-by-one
console.log(args[i]);
}
// BAD: Loop upper bound is off-by-one
for (var i = 0; args.length >= i; i++) {
for (var i = 0; args.length >= i; i++) { // $ Alert - Loop upper bound is off-by-one
console.log(args[i]);
}
// GOOD: Loop upper bound is correct
// OK - Loop upper bound is correct
for (var i = 0; i < args.length; i++) {
console.log(args[i]);
}
var j = 0;
// BAD: Off-by-one on index validity check
if (j <= args.length) {
if (j <= args.length) { // $ Alert - Off-by-one on index validity check
console.log(args[j]);
}
// BAD: Off-by-one on index validity check
if (args.length >= j) {
if (args.length >= j) { // $ Alert - Off-by-one on index validity check
console.log(args[j]);
}
// GOOD: Correct terminating value
// OK - Correct terminating value
if (args.length > j) {
console.log(args[j]);
}
// BAD: incorrect upper bound
function badContains(a, elt) {
function badContains(a, elt) { // $ Alert - incorrect upper bound
for (let i = 0; i <= a.length; ++i)
if (a[i] === elt)
return true;
return false;
}
// GOOD: correct upper bound
// OK - correct upper bound
function goodContains(a, elt) {
for (let i = 0; i < a.length; ++i)
if (a[i] === elt)
@@ -53,7 +48,7 @@ function same(a, b) {
return true;
}
// GOOD: incorrect upper bound, but extra check
// OK - incorrect upper bound, but extra check
function badContains(a, elt) {
for (let i = 0; i <= a.length; ++i)
if (i !== a.length && a[i] === elt)

View File

@@ -1,41 +1,38 @@
function distanceFromOrigin(point) {
// NOT OK
var [x, x] = point;
var [x, x] = point; // $ Alert
return Math.sqrt(x*x + y*y);
}
// NOT OK
var { x: x, y: x } = o;
var { x: x, y: x } = o; // $ Alert
var { x, x } = o; // $ Alert
// NOT OK
var { x, x } = o;
// OK
var { x: x, x: y } = o;
// OK
var { p = x, q = x } = o;
function f({
x: string,
y: string // NOT OK
y: string // $ Alert
}) {
}
function g({x, y}: {x: string, y: string}) { // OK
function g({x, y}: {x: string, y: string}) {
}
function blah(arg) {
var {
x: x,
y: {
x: x, // NOT OK
x: x, // $ Alert
y: {
x: x // NOT OK
x: x // $ Alert
}
}
} = arg;
}
function h({x: string, y: string}: any) { // NOT OK
function h({x: string, y: string}: any) { // $ Alert
}

View File

@@ -1,17 +1,14 @@
function distanceFromOrigin(point) {
// NOT OK
var [x, x] = point;
var [x, x] = point; // $ Alert
return Math.sqrt(x*x + y*y);
}
// NOT OK
var { x: x, y: x } = o;
var { x: x, y: x } = o; // $ Alert
var { x, x } = o; // $ Alert
// NOT OK
var { x, x } = o;
// OK
var { x: x, x: y } = o;
// OK
var { p = x, q = x } = o;

View File

@@ -1,24 +1,20 @@
// NOT OK
(0).foo = 42;
(0).foo = 42; // $ Alert
// NOT OK, but already flagged by SuspiciousPropAccess.ql
null.bar = 23; undefined.baz = 42;
null.bar = 23; undefined.baz = 42; // OK - already flagged by SuspiciousPropAccess.ql
function f() {
var s = "";
for (var i=0;i<10;++i)
// NOT OK
s[i] = " ";
s[i] = " "; // $ Alert
}
function g(b) {
var x = b ? "" : 42, z;
// NOT OK
x.y = true;
// OK: we don't know the type of `b`
x.y = true; // $ Alert
// OK - we don't know the type of `b`
b.y = true;
return;
// OK: no types inferred for `z`, since this is dead code
// OK - no types inferred for `z`, since this is dead code
z.y = true;
}
@@ -26,4 +22,4 @@ function h() {
let tmp;
let obj = (tmp ||= {});
obj.p = 42;
}
}

View File

@@ -1,22 +1,22 @@
function tst() {
var a = { // NOT OK
var a = { // $ Alert
'i': 1,
'j': 2
}
return 1 // NOT OK
return 1 // $ Alert
if (condition) { // OK
if (condition) {
}
for (i = 0; i < 10; i++) { // OK
for (i = 0; i < 10; i++) {
}
label: while (condition) { // OK
break label; // OK
label: while (condition) {
break label;
}
return 1; // OK
return 1;
//pad with enough explicit semicolons to satisfy 90% threshold
foo();

View File

@@ -5,7 +5,7 @@ function A() {
return _a;
},
set a(v) {
// OK
_a = v|0
},
@@ -13,19 +13,18 @@ function A() {
return _x;
},
set x(v) {
// NOT OK
},
}, // $ Alert
get y() {
return 56;
},
set y(v) {
// OK
throw new Error("Cannot mutate y.");
},
set z(v) {
// OK
_z = arguments[0] | 0;
}
};
@@ -36,7 +35,6 @@ function Point(x, y) {
get x() { return x; },
set x(_x) { x = _x|0; },
get y() { return y; },
// NOT OK
set y(_y) { x = _x|0; }
set y(_y) { x = _x|0; } // $ Alert
};
}

View File

@@ -1,17 +1,16 @@
var o = {
_secret_x: 42,
get x() {
// OK
return 42;
},
set x(v) {
if (v !== 42)
// OK
return;
_secret_x = v;
},
set y(w) {
// NOT OK
return "nope";
return "nope"; // $ Alert
}
}

View File

@@ -1,7 +1,7 @@
class Class1 {
constructor(x) { this.x = x; }
}
new Class1(42, 23); // NOT OK: `23` is ignored
new Class1(42, 23); // $ Alert - `23` is ignored
class Sup {
constructor(x) { this.x = x; }
@@ -10,12 +10,12 @@ class Sup {
class Sub extends Sup {
}
new Sub(42); // OK: synthetic constructor delegates to super constructor
new Sub(42); // OK - synthetic constructor delegates to super constructor
class Other {}
new Other(42); // NOT OK: `42` is ignored
new Other(42); // $ Alert - `42` is ignored
var args = [];
f(...args); // OK
f(42, ...args); // NOT OK
f(...args);
f(42, ...args); // $ Alert

View File

@@ -4,7 +4,7 @@ function global() {return;}
window.global = function (x) {return;};
})(this);
global(x); // OK: might refer to function on line 4
global(x); // OK - might refer to function on line 4
function otherglobal() {return;}
@@ -12,6 +12,6 @@ var o = {
otherglobal: function (x) {return;}
};
otherglobal(x); // NOT OK: can never refer to function on line 12
otherglobal.call(null, x); // NOT OK
otherglobal.call(null, x, y); // NOT OK
otherglobal(x); // $ Alert - can never refer to function on line 12
otherglobal.call(null, x); // $ Alert
otherglobal.call(null, x, y); // $ Alert

View File

@@ -1,4 +1,4 @@
function foo(this: void, x: number) {return;}
foo(45); // OK
foo(null, 45); // NOT OK
foo(45);
foo(null, 45); // $ Alert

View File

@@ -7,8 +7,7 @@ function g() {
return 23;
}
// NOT OK
f(g());
f(g()); // $ Alert
function sum() {
var result = 0;
@@ -17,28 +16,26 @@ function sum() {
return result;
}
// OK
sum(1, 2, 3);
function h(k) {
k = k || function() {};
// OK
k(42);
}
// OK
new Array(1, 2, 3);
// NOT OK
new String(1, 2, 3);
new String(1, 2, 3); // $ Alert
(function(f) {
// NOT OK
f(42);
f(42); // $ Alert
})(function() {return;});
(function h(f) {
// OK
f(42);
h(function(x) { return x; });
})(function() {});
@@ -67,15 +64,15 @@ parseFloat("123", 10);
constructor(){
}
}
nonEmpty(42); // NOT OK
empty(42); // OK
emptyWithParam(42, 87); // OK
commentedEmpty(42); // OK
commentedEmptyWithSpreadParam(42, 87); // OK
emptyArrow(42); // NOT OK
new ImplicitEmptyConstructor(42); // NOT OK
new ExplicitEmptyConstructor(42); // NOT OK
parseFloat("123", 10); // NOT OK
nonEmpty(42); // $ Alert
empty(42);
emptyWithParam(42, 87);
commentedEmpty(42);
commentedEmptyWithSpreadParam(42, 87);
emptyArrow(42); // $ Alert
new ImplicitEmptyConstructor(42); // $ Alert
new ExplicitEmptyConstructor(42); // $ Alert
parseFloat("123", 10); // $ Alert
});
(function testWhitelistThrowingFunctions() {
@@ -111,14 +108,14 @@ parseFloat("123", 10);
}
})();
}
notAPlainThrower1(42); // NOT OK
notAPlainThrower2(42); // NOT OK
notAPlainThrower3(42); // NOT OK
thrower(42); // OK
throwerArrow(42); // OK
throwerCustom(42); // OK
throwerWithParam(42, 87); // NOT OK
throwerIndirect(42); // OK, but still flagged due to complexity
notAPlainThrower1(42); // $ Alert
notAPlainThrower2(42); // $ Alert
notAPlainThrower3(42); // $ Alert
thrower(42);
throwerArrow(42);
throwerCustom(42);
throwerWithParam(42, 87); // $ Alert
throwerIndirect(42); // OK - but still flagged due to complexity
});
function sum2() {
@@ -128,14 +125,14 @@ function sum2() {
return result;
}
// OK
sum2(1, 2, 3);
const $ = function (x, arr) {
console.log(x, arr);
};
// OK
async function tagThing(repoUrl, directory) {
await $`git clone ${repoUrl} ${directory}`;
}

View File

@@ -1,25 +1,20 @@
var o = {
A: function f(x) {
'use strict';
// BAD
if (!(this instanceof arguments.callee))
// BAD
return new arguments.callee(x);
// BAD
console.log(f.caller);
// BAD
this.y = f.arguments;
if (!(this instanceof arguments.callee)) // $ Alert
return new arguments.callee(x); // $ Alert
console.log(f.caller); // $ Alert
this.y = f.arguments; // $ Alert
this.x = x;
}
};
var D = class extends function() {
// BAD
return arguments.callee;
return arguments.callee; // $ Alert
} {};
function g() {
// OK
return arguments.caller.length;
}
@@ -27,8 +22,7 @@ function g() {
'use strict';
function h() {
var foo = Math.random() > 0.5 ? h : arguments;
// BAD
return foo.caller;
return foo.caller; // $ Alert
}
})();

View File

@@ -37,7 +37,7 @@ function foo1() {
const foobar = 4;
const data = {name: name, date: date};
writer.emit("Name: ${name}, Date: ${date}.", data); // OK
writer.emit("Name: ${name}, Date: ${date}.", data);
writer.emit("Name: ${name}, Date: ${date}, ${foobar}", data); // NOT OK - `foobar` is not in `data`.
writer.emit("Name: ${name}, Date: ${date}, ${foobar}", data); // $ Alert - `foobar` is not in `data`.
}

View File

@@ -1,6 +1,5 @@
function idMaker(){
var index = 0;
while(true)
// NOT OK
yield index++;
yield index++; // $ Alert
}