mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 13:33:24 +00:00
Babel polyfills this for us for Internet Explorer.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import K from "ast-types/gen/kinds";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
recast.visit(ast, {
visitBinaryExpression(path) {
const { operator, left, right } = path.node;
if (
n.CallExpression.check(left) &&
n.MemberExpression.check(left.callee) &&
!left.callee.computed &&
n.Identifier.check(left.callee.property) &&
left.callee.property.name === "indexOf" &&
left.arguments.length === 1 &&
checkExpression(left.arguments[0]) &&
((["===", "!==", "==", "!=", ">", "<="].includes(operator) &&
n.UnaryExpression.check(right) &&
right.operator == "-" &&
n.Literal.check(right.argument) &&
right.argument.value === 1) ||
([">=", "<"].includes(operator) &&
n.Literal.check(right) &&
right.value === 0))
) {
const test = b.callExpression(
b.memberExpression(left.callee.object, b.identifier("includes")),
[left.arguments[0]]
);
path.replace(
["!==", "!=", ">", ">="].includes(operator)
? test
: b.unaryExpression("!", test)
);
changed = true;
}
this.traverse(path);
},
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
50 lines
1.6 KiB
JavaScript
Executable File
50 lines
1.6 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
const SwaggerParser = require('swagger-parser');
|
|
|
|
function check_duplicate_operationids(file, api) {
|
|
const operation_ids = [];
|
|
|
|
for (const endpoint of api.paths) {
|
|
for (const value of endpoint) {
|
|
const operation_id = value.operationId;
|
|
if (operation_id !== undefined) {
|
|
if (operation_ids.includes(operation_id)) {
|
|
console.error('In', file + ':');
|
|
console.error('Duplicate operationId:', operation_id);
|
|
process.exitCode = 1;
|
|
} else {
|
|
operation_ids.push(operation_id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function validate_swagger(file) {
|
|
SwaggerParser.validate(file)
|
|
.then(function (api) {
|
|
// Let's make sure that there aren't any duplicate operationids,
|
|
// until this issue is fixed:
|
|
// https://github.com/BigstickCarpet/swagger-parser/issues/68
|
|
check_duplicate_operationids(file, api);
|
|
return;
|
|
})
|
|
.catch(function (err) {
|
|
// There is something wrong. Display the validation errors
|
|
console.error('In', file + ':');
|
|
console.error(err.message);
|
|
process.exitCode = 1;
|
|
});
|
|
}
|
|
|
|
// Iterate through the changed files, passed in the arguments.
|
|
// The two first arguments are the call to the Node interpreter and this
|
|
// script, hence the starting point at 2.
|
|
for (const file of process.argv.slice(2)) {
|
|
// Run the validator only for YAML files inside static/swagger
|
|
if (file.startsWith('static/swagger')) {
|
|
validate_swagger(file);
|
|
}
|
|
}
|