Sanity-check decaffeinate app.Router

pull/1441/head
Simon Legner 1 year ago
parent 92d6d5e1d4
commit 5a3e872cd7

@ -1,34 +1,21 @@
// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS103: Rewrite code to no longer use __guard__, or convert again using --optional-chaining
* DS206: Consider reworking classes to avoid initClass
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
app.Router = class Router extends Events { app.Router = class Router extends Events {
static initClass() { static routes = [
this.routes = [ ["*", "before"],
["*", "before"], ["/", "root"],
["/", "root"], ["/settings", "settings"],
["/settings", "settings"], ["/offline", "offline"],
["/offline", "offline"], ["/about", "about"],
["/about", "about"], ["/news", "news"],
["/news", "news"], ["/help", "help"],
["/help", "help"], ["/:doc-:type/", "type"],
["/:doc-:type/", "type"], ["/:doc/", "doc"],
["/:doc/", "doc"], ["/:doc/:path(*)", "entry"],
["/:doc/:path(*)", "entry"], ["*", "notFound"],
["*", "notFound"], ];
];
}
constructor() { constructor() {
super(); super();
for (var [path, method] of Array.from(this.constructor.routes)) { for (var [path, method] of this.constructor.routes) {
page(path, this[method].bind(this)); page(path, this[method].bind(this));
} }
this.setInitialPath(); this.setInitialPath();
@ -78,13 +65,11 @@ app.Router = class Router extends Events {
} }
type(context, next) { type(context, next) {
let type;
const doc = app.docs.findBySlug(context.params.doc); const doc = app.docs.findBySlug(context.params.doc);
const type =
doc != null ? doc.types.findBy("slug", context.params.type) : undefined;
if ( if (type) {
(type =
doc != null ? doc.types.findBy("slug", context.params.type) : undefined)
) {
context.doc = doc; context.doc = doc;
context.type = type; context.type = type;
this.triggerRoute("type"); this.triggerRoute("type");
@ -174,18 +159,13 @@ app.Router = class Router extends Events {
isIndex() { isIndex() {
return ( return (
(this.context != null ? this.context.path : undefined) === "/" || this.context?.path === "/" ||
(app.isSingleDoc() && (app.isSingleDoc() && this.context?.entry?.isIndex())
__guard__(this.context != null ? this.context.entry : undefined, (x) =>
x.isIndex(),
))
); );
} }
isSettings() { isSettings() {
return ( return this.context?.path === "/settings";
(this.context != null ? this.context.path : undefined) === "/settings"
);
} }
setInitialPath() { setInitialPath() {
@ -208,10 +188,7 @@ app.Router = class Router extends Events {
getInitialPathFromHash() { getInitialPathFromHash() {
try { try {
return __guard__( return new RegExp("#/(.+)").exec(decodeURIComponent(location.hash))?.[1];
new RegExp("#/(.+)").exec(decodeURIComponent(location.hash)),
(x) => x[1],
);
} catch (error) {} } catch (error) {}
} }
@ -231,10 +208,3 @@ app.Router = class Router extends Events {
); );
} }
}; };
app.Router.initClass();
function __guard__(value, transform) {
return typeof value !== "undefined" && value !== null
? transform(value)
: undefined;
}

Loading…
Cancel
Save