Add linting rules

This commit is contained in:
Zaid Abdulla committed 2018-12-27 01:21:16 -08:00
1 parent 75a5a42296
commit c702003c32
43 files changed
+705 -576

No files matched your search

+16 -13
View File
@@ -1,7 +1,5 @@
"use strict";
const expression = require("./expression");
const validators = {
pass: require("./validators/pass"),
test: require("./validators/test")
@@ -16,17 +14,18 @@ class Authorizer {
hasAccess(resourceType, access) {
const cacheKey = `${resourceType}-${access}`;
if (cacheKey in this.hasAccessCache)
return this.hasAccessCache[cacheKey];
if (cacheKey in this.hasAccessCache) return this.hasAccessCache[cacheKey];
let has = false;
for (let permissionSet of this.permissionSets) {
for (let perm of permissionSet)
if (perm[resourceType])
for (const permissionSet of this.permissionSets) {
for (const perm of permissionSet) {
if (perm[resourceType]) {
if (perm[resourceType].access >= access) {
has = true;
break;
}
}
}
}
this.hasAccessCache[cacheKey] = has;
@@ -39,23 +38,26 @@ class Authorizer {
let funcs = {};
for (let permissionSet of this.permissionSets)
for (let perm of permissionSet)
for (const permissionSet of this.permissionSets) {
for (const perm of permissionSet) {
if (perm[resourceType]) {
if (perm[resourceType].access >= 3) {
if (perm[resourceType].validate)
for (let [k, v] of Object.entries(perm[resourceType].validate))
if (perm[resourceType].validate) {
for (const [k, v] of Object.entries(perm[resourceType].validate))
funcs[k] = v;
}
} else {
funcs = {};
}
}
}
}
const validator = (mutationType, mutation, any) => {
let valid = false;
for (let [k, v] of Object.entries(funcs))
for (const [k, v] of Object.entries(funcs)) {
if (v) {
let res = validators[k](
const res = validators[k](
resourceType,
resource,
mutationType,
@@ -66,6 +68,7 @@ class Authorizer {
if (res > 0) valid = true;
else if (res < 0) return false;
}
}
return valid;
};
+9 -7
View File
@@ -7,7 +7,7 @@ function or(cnf1, cnf2) {
else if (cnf2.length === 0) return cnf1;
if (cnf1.length > cnf2.length) {
let c = cnf1;
const c = cnf1;
cnf1 = cnf2;
cnf2 = c;
}
@@ -53,10 +53,11 @@ function booleanCnf(exp) {
return e;
});
for (let [k, v] of ranges)
for (const [k, v] of ranges) {
if (k.endsWith(":number"))
ranges.set(k, Array.from(v).sort((a, b) => a - b));
else ranges.set(k, Array.from(v).sort());
}
const expressions = new Map();
const mutuallyExclusive = new Map();
@@ -113,11 +114,12 @@ function booleanCnf(exp) {
function getLikeVariables(lhs, pat, esc) {
const vars = [];
let pats = likePatterns.get(lhs);
const pats = likePatterns.get(lhs);
pat = pats.get(`${esc || ""}:${pat}`);
for (let p of pats.values())
for (const p of pats.values()) {
if (pat === p || likePatternIncludes(pat, p))
vars.push(`${lhs}:like:${JSON.stringify(p)}`);
}
return vars.map(key => {
let f = expressions.get(key);
@@ -185,7 +187,7 @@ function booleanCnf(exp) {
let rhs = JSON.stringify(clause[2]);
// For consistency
if (rhs > lhs) {
let t = rhs;
const t = rhs;
rhs = lhs;
lhs = t;
op = { ">": "<", ">=": "<=", "<": ">", "<=": ">=" }[op] || op;
@@ -218,8 +220,8 @@ function booleanCnf(exp) {
const cnf = recursive(exp, false);
for (let m of mutuallyExclusive.values()) {
let ar = Array.from(m);
for (const m of mutuallyExclusive.values()) {
const ar = Array.from(m);
for (let i = 0; i < ar.length; ++i)
for (let j = i + 1; j < ar.length; ++j) cnf.push([0 - ar[i], 0 - ar[j]]);
}
+16 -12
View File
@@ -4,7 +4,7 @@ const parsimmon = require("parsimmon");
// Turn escaped characters into real ones (e.g. "\\n" becomes "\n").
function interpretEscapes(str) {
let escapes = {
const escapes = {
b: "\b",
f: "\f",
n: "\n",
@@ -12,8 +12,8 @@ function interpretEscapes(str) {
t: "\t"
};
return str.replace(/\\(u[0-9a-fA-F]{4}|[^u])/, (_, escape) => {
let type = escape.charAt(0);
let hex = escape.slice(1);
const type = escape.charAt(0);
const hex = escape.slice(1);
if (type === "u") return String.fromCharCode(parseInt(hex, 16));
if (escapes.hasOwnProperty(type)) return escapes[type];
@@ -27,7 +27,7 @@ function map(exp, callback) {
let clone;
for (let i = 1; i < exp.length; ++i) {
let sub = map(exp[i], callback);
const sub = map(exp[i], callback);
if (sub !== exp[i]) {
clone = clone || exp.slice();
clone[i] = sub;
@@ -43,7 +43,7 @@ function binaryLeft(operatorsParser, nextParser) {
parsimmon.seq(operatorsParser, nextParser).many(),
(first, rest) =>
rest.reduce((acc, ch) => {
let [op, another] = ch;
const [op, another] = ch;
if (Array.isArray(acc) && op === acc[0]) return acc.concat([another]);
if (Array.isArray(another) && op === another[0])
return [op, acc].concat(another.slice(1));
@@ -315,8 +315,9 @@ function stringify(exp, level = 0) {
.join(", ")})`
);
} else if (op === "PARAM") {
if (typeof exp[1] === "string") return wrap(exp[1]);
else if (Array.isArray(exp[1]) && exp[1][0] === "||")
if (typeof exp[1] === "string") {
return wrap(exp[1]);
} else if (Array.isArray(exp[1]) && exp[1][0] === "||") {
return wrap(
exp[1]
.slice(1)
@@ -326,24 +327,27 @@ function stringify(exp, level = 0) {
})
.join("")
);
else return wrap(`{${stringify(exp[1])}}`);
} else {
return wrap(`{${stringify(exp[1])}}`);
}
} else if (op === "IS NULL" || op === "IS NOT NULL") {
return wrap(`${stringify(exp[1], opLevels[op])} ${op}`);
} else if (op === "LIKE" || op === "NOT LIKE") {
if (exp[3])
if (exp[3]) {
return wrap(
`${stringify(exp[1], opLevels[op])} ${op} ${stringify(
exp[2],
opLevels[op]
)} ESCAPE ${stringify(exp[3], opLevels[op])}`
);
else
} else {
return wrap(
`${stringify(exp[1], opLevels[op])} ${op} ${stringify(
exp[2],
opLevels[op]
)}`
);
}
} else if (op in opLevels) {
const parts = exp.slice(1).map((e, i) => {
return stringify(e, opLevels[exp[0]] + Math.min(i - 1, 0));
@@ -357,10 +361,10 @@ function stringify(exp, level = 0) {
}
function parseLikePattern(pat, esc) {
let chars = pat.split("");
const chars = pat.split("");
for (let i = 0; i < chars.length; ++i) {
let c = chars[i];
const c = chars[i];
if (c === esc) {
chars[i] = chars[i + 1] || "";
chars[i + 1] = "";
+11 -14
View File
@@ -8,24 +8,14 @@ const isArray = Array.isArray;
const regExpCache = new WeakMap();
function* permute(arr) {
if (arr.length <= 1) {
for (let i = 0; i < arr[0]; ++i) yield [i];
return;
}
let slc = arr.slice(1);
for (let i = 0; i < arr[0]; ++i)
for (let innerArr of permute(slc)) yield [i].concat(innerArr);
}
const REDUCE_SKIP = {};
function reduce(exp, callback) {
let loop = true;
while (loop) {
loop = false;
for (let i = 2; i < exp.length; ++i) {
let res = callback(exp[i - 1], exp[i]);
if (res !== undefined) {
const res = callback(exp[i - 1], exp[i]);
if (res !== REDUCE_SKIP) {
loop = true;
exp = exp.slice();
exp.splice(i - 1, 2, res);
@@ -71,11 +61,13 @@ function evaluate(exp, obj, now, cb) {
return reduce(e, (a, b) => {
if (!isArray(a)) return a ? b : a;
if (!isArray(b)) return b ? a : b;
return REDUCE_SKIP;
});
} else if (e[0] === "OR") {
return reduce(e, (a, b) => {
if (!isArray(a)) return a ? a : b;
if (!isArray(b)) return b ? b : a;
return REDUCE_SKIP;
});
} else if (e[0] === "NOT") {
if (!isArray(e[1])) return !e[1];
@@ -131,6 +123,7 @@ function evaluate(exp, obj, now, cb) {
if (a == null || b == null) return null;
return a * b;
}
return REDUCE_SKIP;
});
} else if (e[0] === "/") {
return reduce(e, (a, b) => {
@@ -138,6 +131,7 @@ function evaluate(exp, obj, now, cb) {
if (a == null || b == null) return null;
return a / b;
}
return REDUCE_SKIP;
});
} else if (e[0] === "+") {
return reduce(e, (a, b) => {
@@ -145,6 +139,7 @@ function evaluate(exp, obj, now, cb) {
if (a == null || b == null) return null;
return a + b;
}
return REDUCE_SKIP;
});
} else if (e[0] === "-") {
return reduce(e, (a, b) => {
@@ -152,6 +147,7 @@ function evaluate(exp, obj, now, cb) {
if (a == null || b == null) return null;
return a - b;
}
return REDUCE_SKIP;
});
} else if (e[0] === "||") {
return reduce(e, (a, b) => {
@@ -159,6 +155,7 @@ function evaluate(exp, obj, now, cb) {
if (a == null || b == null) return null;
return `${a}${b}`;
}
return REDUCE_SKIP;
});
}
return e;
@@ -238,7 +235,7 @@ function likePatternToRegExp(pat, esc = "", flags = "") {
}
function extractParams(exp) {
let params = [];
const params = [];
expressionParser.map(exp, e => {
if (isArray(e) && e[0] === "PARAM") params.push(e[1]);
return e;
+4 -3
View File
@@ -33,11 +33,12 @@ function satisfiableAt(clauses, varVals, varnr, val) {
// find next unassigned var to split
let nextVar = 0;
for (let i = 1; i < varVals.length; i++)
for (let i = 1; i < varVals.length; i++) {
if (varVals[i] === 0) {
nextVar = i;
break;
}
}
if (nextVar === 0) throw new Error("Error in satisfiableAt");
@@ -105,7 +106,7 @@ function unitPropagate(clauses, varVals) {
let unassignedLit = 0; // 0 means none found for this clause
for (let j = 0; j < clause.length; j++) {
let lit = clause[j];
const lit = clause[j];
let nr, polarity;
if (lit < 0) {
nr = 0 - lit;
@@ -149,7 +150,7 @@ function unitPropagate(clauses, varVals) {
// some units were derived: iterate the main loop
for (let j = 0; j < derivedQueue.length; j++) {
let lit = derivedQueue[j];
const lit = derivedQueue[j];
let nr, polarity;
if (lit < 0) {
nr = 0 - lit;