Commit 66a91e7a authored by Laurent Wouters's avatar Laurent Wouters
Browse files

[fix] Fixed linting issues with ldbrowser

parent c6d72cbef8d0
......@@ -6,7 +6,7 @@
"build": "webpack",
"clean": "rm -rf build/*",
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "tslint -p . -c tslint.json 'src/**/*.ts' --fix",
"lint": "tslint -p . -c tslint.json 'src/**/*.ts*' --fix",
"webpack": "webpack -w",
"webpack:production": "NODE_ENV=production webpack"
},
......
......@@ -374,10 +374,10 @@ chrome.runtime.onMessage.addListener(
} else if (request.requestType === "FetchObservable") {
fetchObservableAt(request.payload)
.then((content: ObservableContent) => {
sendResponse({ ok: content, error: null });
sendResponse({ ok: content });
})
.catch((reason: any) => {
sendResponse({ ok: null, error: reason });
sendResponse({ error: reason });
});
return true;
} else if (request.requestType === "TurnOffForTab") {
......@@ -389,7 +389,7 @@ chrome.runtime.onMessage.addListener(
}
let observation = allObservations[tabId];
if (observation === undefined) {
sendResponse(null);
sendResponse(undefined);
return;
}
observation.status = DocumentStatus.asleep;
......@@ -404,7 +404,7 @@ chrome.runtime.onMessage.addListener(
}
let observation = allObservations[tabId];
if (observation === undefined) {
sendResponse(null);
sendResponse(undefined);
return;
}
observation.status = DocumentStatus.activeRaw;
......
......@@ -155,7 +155,7 @@ export class ConfigSourcesTab extends React.Component<
return (
<div className="row" key={"source-" + index}>
<div className="col-2">
{source.kind != definition.ViewRegistrySourceKind.inline ? (
{source.kind !== definition.ViewRegistrySourceKind.inline ? (
<a
className="btn btn-outline-light"
title="Remove this source"
......
......@@ -52,7 +52,7 @@ export class CurrentTabConfig extends React.Component<
this.props.data.command.selectedTopic === null;
let topic = selectPrimaryTopic(this.props.data);
let viewName = "";
if (this.props.data.rendering != null) {
if (this.props.data.rendering !== null) {
let descriptor = this.props.registry.descriptors[
this.props.data.rendering.viewId
];
......@@ -244,12 +244,11 @@ export class CurrentTabConfig extends React.Component<
<div className="col-8">
<span>{"\u25BA"}</span>
<span style={{ marginLeft: "5pt" }}>
{this.props.data.rendering !== null &&
this.props.data.rendering.languages ? (
{this.props.data.rendering !== null ? (
this.props.data.rendering.languages.map(
(language: application.Language, index: number) => (
<span key={"used-lang" + index} title={language.iso639_2}>
{index != 0 ? ", " : ""}
{index !== 0 ? ", " : ""}
{language.name}
</span>
)
......
......@@ -162,7 +162,6 @@ export class CurrentTabDataSources extends React.Component<
let observations = this.props.data.observations[url];
if (observations === undefined) return <span />;
let totalLoaded = observations.totalLoaded;
let origin = observations.origin;
return (
<li key={"series-" + index} style={{ marginTop: "10px" }}>
<span
......@@ -173,8 +172,7 @@ export class CurrentTabDataSources extends React.Component<
{totalLoaded}
</span>
<a href={url}>{url}</a>
{origin ? this.renderOrigin(origin) : <span />}
{this.renderOrigin(observations.origin)}
<ul>
{series.map((source: DocumentSource, index: number) => (
<li key={"source-" + index}>
......
......@@ -46,8 +46,9 @@ export class CurrentTabMenu extends React.Component<CurrentTabMenuProps, {}> {
: this.props.data.rendering.evaluations.map(
(evaluation: application.RenderingEvaluation, index: number) => {
let view = this.props.registry.descriptors[evaluation.viewId];
if (this.props.data.rendering === null || view === undefined)
if (this.props.data.rendering === null || view === undefined) {
return null;
}
let isSelected =
this.props.data.rendering.viewId === evaluation.viewId;
return (
......
......@@ -96,8 +96,9 @@ export class CurrentTabViews extends React.Component<CurrentTabViewsProps, {}> {
(value: application.RenderingEvaluation) =>
value.viewId === viewId
)
)
) {
return null;
}
let view = this.props.registry.descriptors[viewId];
if (view === undefined) return null;
return (
......@@ -115,7 +116,7 @@ export class CurrentTabViews extends React.Component<CurrentTabViewsProps, {}> {
</div>
);
})
.filter((value: any) => value != null)}
.filter((value: any) => value !== null)}
</div>
);
}
......
......@@ -20,6 +20,11 @@
import { application } from "@logilab/libview";
export function n<X>(i: X | null): X {
if (i !== null) return i;
throw new Error("Expected DOM node is null!");
}
/**
* Metadata about a MIME type
*/
......@@ -189,45 +194,37 @@ export function findLinksInDocument(
resource = url;
}
if (document.head !== null) {
let elementsLink = document.head.getElementsByTagName("link");
for (let i = 0; i !== elementsLink.length; i++) {
let element = elementsLink.item(i);
if (element !== null) {
let rel = element.getAttribute("rel");
let type = element.type;
let href = element.href;
if (rel === "meta") {
links.push(
new Link(href, [
{ name: "type", value: type },
{ name: "rel", value: "alternate" }
])
);
} else if (rel === "bookmark") {
links.push(new Link(href, [{ name: "rel", value: "bookmark" }]));
}
}
let elementsLink = n(document.head).getElementsByTagName("link");
for (let i = 0; i !== elementsLink.length; i++) {
let element = n(elementsLink.item(i));
let rel = element.getAttribute("rel");
let type = element.type;
let href = element.href;
if (rel === "meta") {
links.push(
new Link(href, [
{ name: "type", value: type },
{ name: "rel", value: "alternate" }
])
);
} else if (rel === "bookmark") {
links.push(new Link(href, [{ name: "rel", value: "bookmark" }]));
}
}
let elementsA = document.body.getElementsByTagName("a");
for (let i = 0; i !== elementsA.length; i++) {
let element = elementsA.item(i);
if (
element === null ||
element.href === null ||
element.href === undefined
) {
continue;
}
let element = n(elementsA.item(i));
if (!element.href.startsWith(resource)) continue;
let mimes = Object.keys(MIME).map((key: string) => MIME[key]);
for (let j = 0; j !== mimes.length; j++) {
if (element.href.endsWith(mimes[j].fileExtension)) {
let info = mimes[j];
if (info === undefined) continue;
if (element.href.endsWith(info.fileExtension)) {
links.push(
new Link(element.href, [
{ name: "type", value: mimes[j].mime },
{ name: "type", value: info.mime },
{ name: "rel", value: "alternate" }
])
);
......@@ -293,7 +290,7 @@ export function fetchObservableAt(target: string): Promise<ObservableContent> {
contentType: contentType,
content: xmlHttp.responseText,
url: target,
linkHeader: linkHeader ? linkHeader : ""
linkHeader: linkHeader !== null ? linkHeader : ""
});
}
};
......@@ -307,7 +304,7 @@ export function fetchObservableAt(target: string): Promise<ObservableContent> {
/**
* Map of known MIME types to badge names
*/
export const MIME: { [mime: string]: MimeInfo } = {
export const MIME: { [mime: string]: MimeInfo | undefined } = {
"text/n3": new MimeInfo("text/n3", "N3", 5, ".n3"),
"application/n-triples": new MimeInfo(
"application/n-triples",
......@@ -478,8 +475,7 @@ export class DocumentSourcePage implements DocumentSource {
this.origin = origin;
this.sourceType = "DocumentSourcePage";
this.name =
"Page's content" +
(mime !== undefined && mime !== null ? " (" + mime.name + ")" : "");
"Page's content" + (mime !== undefined ? " (" + mime.name + ")" : "");
this.url = url;
this.contentType = contentType;
this.priority = 1;
......@@ -525,18 +521,13 @@ function doFetchDocumentPage(page: DocumentSourcePage): Promise<RawContent> {
export class DocumentSourceLinked implements DocumentSource {
constructor(link: Link, origin: Origin) {
let mime = MIME[link.tags.type];
if (mime === null || mime === undefined) {
if (mime === undefined) {
throw new Error("Unrecognized link type");
}
this.tags = link.tags;
this.origin = origin;
this.sourceType = "DocumentSourceLinked";
this.name =
"Linked content" +
(mime !== undefined && mime !== null ? " (" + mime.name + ")" : "") +
" (" +
link.url +
")";
this.name = "Linked content" + mime.name + " (" + link.url + ")";
this.url = link.url;
this.contentType = mime.mime;
this.priority = mime.priority;
......@@ -586,9 +577,7 @@ function doFetchDocumentLink(link: DocumentSourceLinked): Promise<RawContent> {
* Fetches this data
*/
export function fetchDocument(source: DocumentSource): Promise<RawContent> {
if (source === null || source === undefined) {
return new Promise((resolve, reject) => reject("No data"));
} else if (source.sourceType === "DocumentSourceNone") {
if (source.sourceType === "DocumentSourceNone") {
return new Promise((resolve, reject) => reject("No data"));
} else if (source.sourceType === "DocumentSourceInline") {
let inline = source as DocumentSourceInline;
......@@ -700,11 +689,12 @@ export function observeContent(
// Linked-data content at this URI
let links = parseLinks(observable.linkHeader);
let primary = detectTopicOnlinks(links);
let mimeInfo = MIME[observable.contentType];
let source =
observable.content !== null
observable.content.length > 0 && mimeInfo !== undefined
? new DocumentSourceInline(
observable.url,
MIME[observable.contentType],
mimeInfo,
observable.content,
origin
)
......@@ -732,7 +722,6 @@ export function observeContent(
let primary = detectTopicOnlinks(links);
if (
observable.contentType === "text/html" &&
observable.content !== null &&
observable.content.length > 0
) {
// parse the html to retrieve its header
......@@ -756,7 +745,7 @@ export function observeContent(
);
let observations: DocumentObservations = {
origin: origin,
primaryTopic: primary === null || primary === undefined ? "" : primary,
primaryTopic: primary === null ? "" : primary,
url: observable.url,
sources: sources,
preemptable: MIME.hasOwnProperty(observable.contentType),
......@@ -776,7 +765,7 @@ export function hasDetectedData(observation: DocumentObservations): boolean {
let main = observation.sources.find(
(source: DocumentSource) => source !== NO_DATA
);
return main !== undefined && main !== null && main !== NO_DATA;
return main !== undefined && main !== NO_DATA;
}
/**
......@@ -992,10 +981,7 @@ export function selectPrimaryTopic(data: ResourceData): application.Resource {
let url = Object.keys(data.observations).find((url: string) => {
let observations = data.observations[url];
if (observations) {
return (
observations.primaryTopic !== null &&
observations.primaryTopic.length > 0
);
return observations.primaryTopic.length > 0;
} else {
return false;
}
......@@ -1034,7 +1020,7 @@ export function checkCompliance(data: ResourceData): ComplianceWarning[] {
source.origin.kind === OriginKind.Negotiated ||
source.origin.kind === OriginKind.Redirected) &&
data.resource.uris.indexOf(source.origin.url) >= 0) ||
data.resource.uris.indexOf(source.url)
data.resource.uris.indexOf(source.url) >= 0
) {
loadedFromAlternatives.push(source);
}
......
......@@ -217,13 +217,17 @@ export function fetchObservableAt(target: string): Promise<ObservableContent> {
requestType: "FetchObservable",
payload: target
},
(result: { ok: ObservableContent; error: string }) => {
if (result === undefined || result === null) {
(result: { ok?: ObservableContent; error?: string } | undefined) => {
if (result === undefined) {
return reject("Failed to fetch " + target);
}
if (result.error !== null && result.error.length > 0) {
if (result.error !== undefined) {
reject(result.error);
} else resolve(result.ok);
} else if (result.ok !== undefined) {
resolve(result.ok);
} else {
return reject("Failed to fetch " + target);
}
}
);
}
......@@ -247,8 +251,8 @@ export function backgroundTurnOffForTab(
requestType: "TurnOffForTab",
payload: tabId
},
(result: DocumentObservations) => {
if (result === null) {
(result: DocumentObservations | undefined) => {
if (result === undefined) {
reject("Invalid tab");
} else {
resolve(result);
......@@ -276,8 +280,8 @@ export function backgroundToggleRawForTab(
requestType: "ToggleRawForTab",
payload: tabId
},
(result: DocumentObservations) => {
if (result === null) {
(result: DocumentObservations | undefined) => {
if (result === undefined) {
reject("Invalid tab");
} else {
resolve(result);
......
......@@ -87,9 +87,9 @@ export function reloadRegistryFromStorage(
let r = chrome.storage.local.get(
STORAGE_SOURCES,
(items: { [key: string]: any }) => onLoadedItems(items)
);
) as any;
if (r !== null && r !== undefined) {
let promise = (r as any) as Promise<{ [key: string]: any }>;
let promise = r as Promise<{ [key: string]: any }>;
promise
.then((items: { [key: string]: any }) => onLoadedItems(items))
.catch((reason: any) => {
......
......@@ -144,11 +144,7 @@ function renderRdfNode(
node: $rdf.Node
): HTMLElement {
let result = document.createElement("td");
if (node === undefined || node === null) {
let sub = document.createElement("span");
sub.appendChild(document.createTextNode("?"));
result.appendChild(sub);
} else if (node.termType === "NamedNode") {
if (node.termType === "NamedNode") {
let nodeIRI = node as $rdf.NamedNode;
let sub = document.createElement("a");
sub.href = nodeIRI.uri;
......@@ -170,11 +166,9 @@ function renderRdfNode(
let nodeLiteral = node as $rdf.Literal;
let sub = document.createElement("span");
let language =
nodeLiteral.lang !== null &&
nodeLiteral.lang !== undefined &&
nodeLiteral.lang.length > 0
? nodeLiteral.lang
: nodeLiteral.language !== null && nodeLiteral.language !== undefined
: nodeLiteral.language.length > 0
? nodeLiteral.language
: "";
let text = '"' + nodeLiteral.value + '"';
......
......@@ -20,6 +20,7 @@
import { application, definition } from "@logilab/libview";
import { getResourceContent } from "../common/messages";
import { n } from "../common/data";
/**
* Interface for the viewer
......@@ -141,16 +142,16 @@ class MainViewer implements Viewer {
while (root.hasChildNodes() && root.lastChild !== null) {
root.removeChild(root.lastChild);
}
if (document.head) {
let links = getAllNodes(document.head, "link");
for (let i = 0; i !== links.length; i++) {
document.head.removeChild(links[i]);
}
let styles = getAllNodes(document.head, "style");
for (let i = 0; i !== styles.length; i++) {
document.head.removeChild(styles[i]);
}
let links = getAllNodes(n(document.head), "link");
for (let i = 0; i !== links.length; i++) {
n(document.head).removeChild(links[i]);
}
let styles = getAllNodes(n(document.head), "style");
for (let i = 0; i !== styles.length; i++) {
n(document.head).removeChild(styles[i]);
}
let scripts = getAllNodes(document.body, "script");
for (let i = 0; i !== scripts.length; i++) {
if (scripts[i].id !== "main-script") {
......@@ -180,7 +181,7 @@ class MainViewer implements Viewer {
let style = document.createElement("link");
style.href = "bootstrap.min.css";
style.rel = "stylesheet";
if (document.head) document.head.appendChild(style);
n(document.head).appendChild(style);
let div = document.createElement("div");
div.classList.add("alert");
div.classList.add("alert-info");
......@@ -191,7 +192,7 @@ class MainViewer implements Viewer {
let style = document.createElement("link");
style.href = "bootstrap.min.css";
style.rel = "stylesheet";
if (document.head) document.head.appendChild(style);
n(document.head).appendChild(style);
let div = document.createElement("div");
div.classList.add("alert");
div.classList.add("alert-danger");
......@@ -209,7 +210,7 @@ class MainViewer implements Viewer {
.then((content: string) => {
let style = document.createElement("style");
style.appendChild(document.createTextNode(content));
if (document.head) document.head.appendChild(style);
n(document.head).appendChild(style);
})
.catch((reason: any) => {
console.log(reason);
......@@ -234,7 +235,7 @@ class MainViewer implements Viewer {
let style = document.createElement("link");
style.href = "bootstrap.min.css";
style.rel = "stylesheet";
if (document.head) document.head.appendChild(style);
n(document.head).appendChild(style);
let div = document.createElement("div");
div.classList.add("alert");
div.classList.add("alert-danger");
......@@ -250,8 +251,8 @@ function getAllNodes(parent: Element, tag: string): Element[] {
let result = [];
let nodes = parent.getElementsByTagName(tag);
for (let i = 0; i !== nodes.length; i++) {
let node = nodes.item(i);
if (node !== null) result.push(node);
let node = n(nodes.item(i));
result.push(node);
}
return result;
}
......@@ -28,7 +28,8 @@ import {
import {
DocumentObservations,
findLinksInDocument,
DocumentStatus
DocumentStatus,
OriginKind
} from "../common/data";
import { Viewer, newViewer } from "../common/viewer";
import { LDBrowser, newBrowser } from "../common/browser";
......@@ -140,10 +141,7 @@ function onObservations(observation: DocumentObservations) {
return;
}
if (
observation.preemptable ||
(observation.negotiated !== null && observation.negotiated !== undefined)
) {
if (observation.preemptable || observation.negotiated !== null) {
let browser = initializeBrowser();
browser
.onReachedUri(window.location.href, observation.origin)
......@@ -163,7 +161,7 @@ function main() {
// Gets data about this tab from the background and reply with the HTML head data
getObservationsFor(undefined)
.then((observation: DocumentObservations) => {
if (observation === null) {
if (observation.origin.kind === OriginKind.Unkown) {
waitActivation();
} else {
onObservations(observation);
......
......@@ -34,7 +34,7 @@ class Page extends React.Component<{}, State> {
constructor(props: any) {
super(props);
let index = window.location.search.indexOf("uri=");
var uri = window.location.search.substring(
let uri = window.location.search.substring(
index + "uri=".length,
window.location.search.length
);
......@@ -51,10 +51,14 @@ class Page extends React.Component<{}, State> {
name: uri,
uri: uri
};
addViewSource(newSource).then((registry: definition.ViewRegistry) => {
self.state.pane = "ok";
self.setState(self.state);
});
addViewSource(newSource)
.then((registry: definition.ViewRegistry) => {
self.state.pane = "ok";
self.setState(self.state);
})
.catch((reason: any) => {
console.log(reason);
});
}
render() {
......
......@@ -52,19 +52,27 @@ class Popup extends React.Component<{}, State> {
this.onRegistryRemoveSource = this.onRegistryRemoveSource.bind(this);
let self = this;
getViewRegistry().then((registry: definition.ViewRegistry) => {
self.state.pane = "sources";
self.state.registry = registry;
self.setState(self.state);
});
getViewRegistry()
.then((registry: definition.ViewRegistry) => {
self.state.pane = "sources";
self.state.registry = registry;
self.setState(self.state);
})
.catch((reason: any) => {
console.log(reason);
});
}
onButtonReloadRegistry() {
let self = this;
reloadViewRegistry().then((registry: definition.ViewRegistry) => {
self.state.registry = registry;
self.setState(self.state);
});
reloadViewRegistry()
.then((registry: definition.ViewRegistry) => {
self.state.registry = registry;
self.setState(self.state);
})
.catch((reason: any) => {
console.log(reason);
});
}
onPaneSources() {
......@@ -84,18 +92,26 @@ class Popup extends React.Component<{}, State> {
uri: uri
};
let self = this;