1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-03 04:48:52 -05:00

Fix and enable linting of deno_typescript/*, tools/*, website/* (#2962)

This commit is contained in:
Bert Belder 2019-09-16 23:25:32 +02:00
parent c6afe87feb
commit 02cb34d8ad
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
11 changed files with 185 additions and 133 deletions

View file

@ -19,17 +19,22 @@
"@typescript-eslint/no-parameter-properties": ["off"], "@typescript-eslint/no-parameter-properties": ["off"],
"@typescript-eslint/no-unused-vars": [ "@typescript-eslint/no-unused-vars": [
"error", "error",
{ "argsIgnorePattern": "^_" } { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" }
], ],
"@typescript-eslint/ban-ts-ignore": ["off"], "@typescript-eslint/ban-ts-ignore": ["off"],
"@typescript-eslint/no-empty-function": ["off"], "@typescript-eslint/no-empty-function": ["off"]
"@typescript-eslint/explicit-function-return-type": ["off"]
}, },
"overrides": [ "overrides": [
{ {
"files": ["*.ts", "*.tsx"], "files": ["*.js"],
"rules": { "rules": {
"@typescript-eslint/explicit-function-return-type": ["error"] "@typescript-eslint/explicit-function-return-type": ["off"]
}
},
{
"files": ["tools/node_*.js"],
"rules": {
"@typescript-eslint/no-var-requires": ["off"]
} }
} }
] ]

View file

@ -1,3 +1,2 @@
js/flatbuffers.js
tests/error_syntax.js tests/error_syntax.js
tests/badly_formatted.js tests/badly_formatted.js

View file

@ -106,7 +106,7 @@ class TestWritePermissions(BaseReadWritePermissionsTest,
class TestNetFetchPermissions(BaseComplexPermissionTest): class TestNetFetchPermissions(BaseComplexPermissionTest):
test_type = "net_fetch" test_type = "netFetch"
def test_allow_localhost_4545(self): def test_allow_localhost_4545(self):
code, _stdout, stderr = self._run_deno( code, _stdout, stderr = self._run_deno(
@ -143,7 +143,7 @@ class TestNetFetchPermissions(BaseComplexPermissionTest):
class TestNetDialPermissions(BaseComplexPermissionTest): class TestNetDialPermissions(BaseComplexPermissionTest):
test_type = "net_dial" test_type = "netDial"
def test_allow_localhost_ip_4555(self): def test_allow_localhost_ip_4555(self):
code, _stdout, stderr = self._run_deno( code, _stdout, stderr = self._run_deno(
@ -177,7 +177,7 @@ class TestNetDialPermissions(BaseComplexPermissionTest):
class TestNetListenPermissions(BaseComplexPermissionTest): class TestNetListenPermissions(BaseComplexPermissionTest):
test_type = "net_listen" test_type = "netListen"
def test_allow_localhost_4555(self): def test_allow_localhost_4555(self):
code, _stdout, stderr = self._run_deno( code, _stdout, stderr = self._run_deno(

View file

@ -1,28 +1,26 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
const { args, readFileSync, writeFileSync, exit, dial } = Deno; const { args, readFileSync, writeFileSync, exit } = Deno;
const name = args[1]; const name = args[1];
const test: (args: string[]) => void = { const test: (args: string[]) => void = {
read: (files: string[]): void => { read(files: string[]): void {
files.forEach((file): any => readFileSync(file)); files.forEach(file => readFileSync(file));
}, },
write: (files: string[]): void => { write(files: string[]): void {
files.forEach( files.forEach(file =>
(file): any => writeFileSync(file, new Uint8Array(), { append: true }) writeFileSync(file, new Uint8Array(0), { append: true })
); );
}, },
net_fetch: (hosts: string[]): void => { netFetch(hosts: string[]): void {
hosts.forEach((host): any => fetch(host)); hosts.forEach(host => fetch(host));
}, },
net_listen: (hosts: string[]): void => { netListen(hosts: string[]): void {
hosts.forEach( hosts.forEach(host => {
(host): any => {
const listener = Deno.listen("tcp", host); const listener = Deno.listen("tcp", host);
listener.close(); listener.close();
} });
);
}, },
net_dial: async (hosts: string[]): Promise<void> => { async netDial(hosts: string[]): Promise<void> {
for (const host of hosts) { for (const host of hosts) {
const listener = await Deno.dial("tcp", host); const listener = await Deno.dial("tcp", host);
listener.close(); listener.close();

View file

@ -15,7 +15,7 @@ async function main(): Promise<void> {
} }
} }
async function proxyRequest(req: ServerRequest) { async function proxyRequest(req: ServerRequest): Promise<void> {
const url = `http://${originAddr}${req.url}`; const url = `http://${originAddr}${req.url}`;
const resp = await fetch(url, { const resp = await fetch(url, {
method: req.method, method: req.method,

View file

@ -38,12 +38,8 @@ def eslint():
print "eslint" print "eslint"
script = os.path.join(third_party_path, "node_modules", "eslint", "bin", script = os.path.join(third_party_path, "node_modules", "eslint", "bin",
"eslint") "eslint")
# TODO: Files in 'deno_typescript', 'tools' and 'website' directories are
# currently not linted, but they should.
source_files = git_ls_files(
root_path,
["*.js", "*.ts", ":!:deno_typescript/", ":!:tools/", ":!:website/"])
# Find all *directories* in the main repo that contain .ts/.js files. # Find all *directories* in the main repo that contain .ts/.js files.
source_files = git_ls_files(root_path, ["*.js", "*.ts"])
source_dirs = set([os.path.dirname(f) for f in source_files]) source_dirs = set([os.path.dirname(f) for f in source_files])
# Within the source dirs, eslint does its own globbing, taking into account # Within the source dirs, eslint does its own globbing, taking into account
# the exclusion rules listed in '.eslintignore'. # the exclusion rules listed in '.eslintignore'.

View file

@ -9,7 +9,7 @@ const response = Buffer.from(
); );
async function write(socket, buffer) { async function write(socket, buffer) {
let p = new Promise((resolve, reject) => { const p = new Promise((resolve, _) => {
socket.write(buffer, resolve); socket.write(buffer, resolve);
}); });
return p; return p;
@ -19,7 +19,7 @@ Server(async socket => {
socket.on("error", _ => { socket.on("error", _ => {
socket.destroy(); socket.destroy();
}); });
for await (const data of socket) { for await (const _ of socket) {
write(socket, response); write(socket, response);
} }
}).listen(port); }).listen(port);

View file

@ -5,7 +5,7 @@ const firstCheckFailedMessage = "First check failed";
const name = args[1]; const name = args[1];
const test = { const test = {
needsRead: async () => { async needsRead(): Promise<void> {
try { try {
readFileSync("package.json"); readFileSync("package.json");
} catch (e) { } catch (e) {
@ -13,7 +13,7 @@ const test = {
} }
readFileSync("package.json"); readFileSync("package.json");
}, },
needsWrite: () => { needsWrite(): void {
try { try {
makeTempDirSync(); makeTempDirSync();
} catch (e) { } catch (e) {
@ -21,7 +21,7 @@ const test = {
} }
makeTempDirSync(); makeTempDirSync();
}, },
needsEnv: () => { needsEnv(): void {
try { try {
env().home; env().home;
} catch (e) { } catch (e) {
@ -29,7 +29,7 @@ const test = {
} }
env().home; env().home;
}, },
needsNet: () => { needsNet(): void {
try { try {
listen("tcp", "127.0.0.1:4540"); listen("tcp", "127.0.0.1:4540");
} catch (e) { } catch (e) {
@ -37,9 +37,9 @@ const test = {
} }
listen("tcp", "127.0.0.1:4541"); listen("tcp", "127.0.0.1:4541");
}, },
needsRun: () => { needsRun(): void {
try { try {
const process = run({ run({
args: [ args: [
"python", "python",
"-c", "-c",
@ -49,7 +49,7 @@ const test = {
} catch (e) { } catch (e) {
console.log(firstCheckFailedMessage); console.log(firstCheckFailedMessage);
} }
const process = run({ run({
args: [ args: [
"python", "python",
"-c", "-c",

View file

@ -3,17 +3,71 @@
// How much to multiply time values in order to process log graphs properly. // How much to multiply time values in order to process log graphs properly.
const TimeScaleFactor = 10000; const TimeScaleFactor = 10000;
export async function getJson(path) { export interface BenchmarkExecTimeResult {
min?: number;
max?: number;
mean?: number;
stddev?: number;
system?: number;
user?: number;
}
export interface BenchmarkExecTimeResultSet {
[variant: string]: BenchmarkExecTimeResult;
}
export interface BenchmarkVariantsResultSet {
[variant: string]: number;
}
export interface BenchmarkRun {
created_at: string;
sha1: string;
benchmark: BenchmarkExecTimeResultSet;
binary_size?: BenchmarkVariantsResultSet | number;
max_memory?: BenchmarkVariantsResultSet | number;
bundle_size?: BenchmarkVariantsResultSet;
max_latency?: BenchmarkVariantsResultSet;
req_per_sec?: BenchmarkVariantsResultSet;
req_per_sec_proxy?: BenchmarkVariantsResultSet;
syscall_count?: BenchmarkVariantsResultSet;
thread_count?: BenchmarkVariantsResultSet;
throughput?: BenchmarkVariantsResultSet;
}
export type BenchmarkName = Exclude<keyof BenchmarkRun, "created_at" | "sha1">;
type Column = [string, ...Array<number | null>];
interface C3DataNode {
id: string;
index: number;
name: string;
value: number;
x: number;
}
type C3OnClickCallback = (C3DataNode, unknown) => void;
type C3OnRenderedCallback = () => void;
type C3TickFormatter = (number) => number | string;
export async function getJson(path: string): Promise<unknown> {
return (await fetch(path)).json(); return (await fetch(path)).json();
} }
function getBenchmarkVarieties(data, benchmarkName) { function getBenchmarkVarieties(
data: BenchmarkRun[],
benchmarkName: BenchmarkName
): string[] {
// Look at last sha hash. // Look at last sha hash.
const last = data[data.length - 1]; const last = data[data.length - 1];
return Object.keys(last[benchmarkName]); return Object.keys(last[benchmarkName]);
} }
export function createColumns(data, benchmarkName) { export function createColumns(
data: BenchmarkRun[],
benchmarkName: BenchmarkName
): Column[] {
const varieties = getBenchmarkVarieties(data, benchmarkName); const varieties = getBenchmarkVarieties(data, benchmarkName);
return varieties.map(variety => [ return varieties.map(variety => [
variety, variety,
@ -35,11 +89,11 @@ export function createColumns(data, benchmarkName) {
} }
export function createNormalizedColumns( export function createNormalizedColumns(
data, data: BenchmarkRun[],
benchmarkName, benchmarkName: BenchmarkName,
baselineBenchmark, baselineBenchmark: BenchmarkName,
baselineVariety baselineVariety: string
) { ): Column[] {
const varieties = getBenchmarkVarieties(data, benchmarkName); const varieties = getBenchmarkVarieties(data, benchmarkName);
return varieties.map(variety => [ return varieties.map(variety => [
variety, variety,
@ -65,19 +119,19 @@ export function createNormalizedColumns(
]); ]);
} }
export function createExecTimeColumns(data) { export function createExecTimeColumns(data: BenchmarkRun[]): Column[] {
return createColumns(data, "benchmark"); return createColumns(data, "benchmark");
} }
export function createThroughputColumns(data) { export function createThroughputColumns(data: BenchmarkRun[]): Column[] {
return createColumns(data, "throughput"); return createColumns(data, "throughput");
} }
export function createProxyColumns(data) { export function createProxyColumns(data: BenchmarkRun[]): Column[] {
return createColumns(data, "req_per_sec_proxy"); return createColumns(data, "req_per_sec_proxy");
} }
export function createNormalizedProxyColumns(data) { export function createNormalizedProxyColumns(data: BenchmarkRun[]): Column[] {
return createNormalizedColumns( return createNormalizedColumns(
data, data,
"req_per_sec_proxy", "req_per_sec_proxy",
@ -86,23 +140,25 @@ export function createNormalizedProxyColumns(data) {
); );
} }
export function createReqPerSecColumns(data) { export function createReqPerSecColumns(data: BenchmarkRun[]): Column[] {
return createColumns(data, "req_per_sec"); return createColumns(data, "req_per_sec");
} }
export function createNormalizedReqPerSecColumns(data) { export function createNormalizedReqPerSecColumns(
data: BenchmarkRun[]
): Column[] {
return createNormalizedColumns(data, "req_per_sec", "req_per_sec", "hyper"); return createNormalizedColumns(data, "req_per_sec", "req_per_sec", "hyper");
} }
export function createMaxLatencyColumns(data) { export function createMaxLatencyColumns(data: BenchmarkRun[]): Column[] {
return createColumns(data, "max_latency"); return createColumns(data, "max_latency");
} }
export function createMaxMemoryColumns(data) { export function createMaxMemoryColumns(data: BenchmarkRun[]): Column[] {
return createColumns(data, "max_memory"); return createColumns(data, "max_memory");
} }
export function createBinarySizeColumns(data) { export function createBinarySizeColumns(data: BenchmarkRun[]): Column[] {
const propName = "binary_size"; const propName = "binary_size";
const binarySizeNames = Object.keys(data[data.length - 1][propName]); const binarySizeNames = Object.keys(data[data.length - 1][propName]);
return binarySizeNames.map(name => [ return binarySizeNames.map(name => [
@ -122,7 +178,7 @@ export function createBinarySizeColumns(data) {
]); ]);
} }
export function createThreadCountColumns(data) { export function createThreadCountColumns(data: BenchmarkRun[]): Column[] {
const propName = "thread_count"; const propName = "thread_count";
const threadCountNames = Object.keys(data[data.length - 1][propName]); const threadCountNames = Object.keys(data[data.length - 1][propName]);
return threadCountNames.map(name => [ return threadCountNames.map(name => [
@ -137,7 +193,7 @@ export function createThreadCountColumns(data) {
]); ]);
} }
export function createSyscallCountColumns(data) { export function createSyscallCountColumns(data: BenchmarkRun[]): Column[] {
const propName = "syscall_count"; const propName = "syscall_count";
const syscallCountNames = Object.keys(data[data.length - 1][propName]); const syscallCountNames = Object.keys(data[data.length - 1][propName]);
return syscallCountNames.map(name => [ return syscallCountNames.map(name => [
@ -152,27 +208,27 @@ export function createSyscallCountColumns(data) {
]); ]);
} }
export function createBundleSizeColumns(data) { export function createBundleSizeColumns(data: BenchmarkRun[]): Column[] {
return createColumns(data, "bundle_size"); return createColumns(data, "bundle_size");
} }
export function createSha1List(data) { export function createSha1List(data: BenchmarkRun[]): string[] {
return data.map(d => d.sha1); return data.map(d => d.sha1);
} }
export function formatKB(bytes) { export function formatKB(bytes: number): string {
return (bytes / 1024).toFixed(2); return (bytes / 1024).toFixed(2);
} }
export function formatMB(bytes) { export function formatMB(bytes: number): string {
return (bytes / (1024 * 1024)).toFixed(2); return (bytes / (1024 * 1024)).toFixed(2);
} }
export function formatReqSec(reqPerSec) { export function formatReqSec(reqPerSec: number): string {
return reqPerSec / 1000; return (reqPerSec / 1000).toFixed(3);
} }
export function formatPercentage(decimal) { export function formatPercentage(decimal: number): string {
return (decimal * 100).toFixed(2); return (decimal * 100).toFixed(2);
} }
@ -186,15 +242,15 @@ export function formatPercentage(decimal) {
* @param {boolean} zoomEnabled enables the zoom feature * @param {boolean} zoomEnabled enables the zoom feature
*/ */
function generate( function generate(
id, id: string,
categories, categories: string[],
columns, columns: Column[],
onclick, onclick: C3OnClickCallback,
yLabel = "", yLabel = "",
yTickFormat = null, yTickFormat?: C3TickFormatter,
zoomEnabled = true, zoomEnabled = true,
onrendered = () => {} onrendered?: C3OnRenderedCallback
) { ): void {
const yAxis = { const yAxis = {
padding: { bottom: 0 }, padding: { bottom: 0 },
min: 0, min: 0,
@ -207,12 +263,12 @@ function generate(
}; };
if (yTickFormat == logScale) { if (yTickFormat == logScale) {
delete yAxis.min; delete yAxis.min;
for (let col of columns) { for (const col of columns) {
for (let i = 1; i < col.length; i++) { for (let i = 1; i < col.length; i++) {
if (col[i] == null || col[i] === 0) { if (col[i] == null || col[i] === 0) {
continue; continue;
} }
col[i] = Math.log10(col[i] * TimeScaleFactor); col[i] = Math.log10((col[i] as number) * TimeScaleFactor);
} }
} }
} }
@ -240,21 +296,14 @@ function generate(
}); });
} }
function logScale(t) { function logScale(t: number): string {
return (Math.pow(10, t) / TimeScaleFactor).toFixed(4); return (Math.pow(10, t) / TimeScaleFactor).toFixed(4);
} }
function formatSecsAsMins(t) {
// TODO use d3.round()
const a = t % 60;
const min = Math.floor(t / 60);
return a < 30 ? min : min + 1;
}
/** /**
* @param dataUrl The url of benchmark data json. * @param dataUrl The url of benchmark data json.
*/ */
export function drawCharts(dataUrl) { export function drawCharts(dataUrl: string): Promise<void> {
// TODO Using window["location"]["hostname"] instead of // TODO Using window["location"]["hostname"] instead of
// window.location.hostname because when deno runs app_test.js it gets a type // window.location.hostname because when deno runs app_test.js it gets a type
// error here, not knowing about window.location. Ideally Deno would skip // error here, not knowing about window.location. Ideally Deno would skip
@ -265,11 +314,8 @@ export function drawCharts(dataUrl) {
return drawChartsFromBenchmarkData(dataUrl); return drawChartsFromBenchmarkData(dataUrl);
} }
const proxyFields = [ const proxyFields: BenchmarkName[] = ["req_per_sec"];
"req_per_sec" function extractProxyFields(data: BenchmarkRun[]): void {
//"max_latency"
];
function extractProxyFields(data) {
for (const row of data) { for (const row of data) {
for (const field of proxyFields) { for (const field of proxyFields) {
const d = row[field]; const d = row[field];
@ -290,8 +336,10 @@ function extractProxyFields(data) {
/** /**
* Draws the charts from the benchmark data stored in gh-pages branch. * Draws the charts from the benchmark data stored in gh-pages branch.
*/ */
export async function drawChartsFromBenchmarkData(dataUrl) { export async function drawChartsFromBenchmarkData(
const data = await getJson(dataUrl); dataUrl: string
): Promise<void> {
const data = (await getJson(dataUrl)) as BenchmarkRun[];
// hack to extract proxy fields from req/s fields // hack to extract proxy fields from req/s fields
extractProxyFields(data); extractProxyFields(data);
@ -311,25 +359,23 @@ export async function drawChartsFromBenchmarkData(dataUrl) {
const sha1List = createSha1List(data); const sha1List = createSha1List(data);
const sha1ShortList = sha1List.map(sha1 => sha1.substring(0, 6)); const sha1ShortList = sha1List.map(sha1 => sha1.substring(0, 6));
const viewCommitOnClick = _sha1List => d => { function viewCommitOnClick(d: C3DataNode, _: unknown): void {
// @ts-ignore // @ts-ignore
window.open( window.open(`https://github.com/denoland/deno/commit/${sha1List[d.index]}`);
`https://github.com/denoland/deno/commit/${_sha1List[d["index"]]}` }
);
};
function gen( function gen(
id, id: string,
columns, columns: Column[],
yLabel = "", yLabel = "",
yTickFormat = null, yTickFormat?: C3TickFormatter,
onrendered = () => {} onrendered?: C3OnRenderedCallback
) { ): void {
generate( generate(
id, id,
sha1ShortList, sha1ShortList,
columns, columns,
viewCommitOnClick(sha1List), viewCommitOnClick,
yLabel, yLabel,
yTickFormat, yTickFormat,
true, true,
@ -363,8 +409,8 @@ export async function drawChartsFromBenchmarkData(dataUrl) {
gen("#bundle-size-chart", bundleSizeColumns, "kilobytes", formatKB); gen("#bundle-size-chart", bundleSizeColumns, "kilobytes", formatKB);
} }
function hideOnRender(elementID) { function hideOnRender(elementID: string): C3OnRenderedCallback {
return () => { return (): void => {
const chart = window["document"].getElementById(elementID); const chart = window["document"].getElementById(elementID);
if (!chart.getAttribute("data-inital-hide-done")) { if (!chart.getAttribute("data-inital-hide-done")) {
chart.setAttribute("data-inital-hide-done", "true"); chart.setAttribute("data-inital-hide-done", "true");
@ -373,12 +419,16 @@ function hideOnRender(elementID) {
}; };
} }
function registerNormalizedSwitcher(checkboxID, chartID, normalizedChartID) { function registerNormalizedSwitcher(
checkboxID: string,
chartID: string,
normalizedChartID: string
): void {
const checkbox = window["document"].getElementById(checkboxID); const checkbox = window["document"].getElementById(checkboxID);
const regularChart = window["document"].getElementById(chartID); const regularChart = window["document"].getElementById(chartID);
const normalizedChart = window["document"].getElementById(normalizedChartID); const normalizedChart = window["document"].getElementById(normalizedChartID);
checkbox.addEventListener("change", event => { checkbox.addEventListener("change", _ => {
// If checked is true the normalized variant should be shown // If checked is true the normalized variant should be shown
// @ts-ignore // @ts-ignore
if (checkbox.checked) { if (checkbox.checked) {
@ -395,15 +445,15 @@ export function main(): void {
window["chartWidth"] = 800; window["chartWidth"] = 800;
const overlay = window["document"].getElementById("spinner-overlay"); const overlay = window["document"].getElementById("spinner-overlay");
function showSpinner() { function showSpinner(): void {
overlay.style.display = "block"; overlay.style.display = "block";
} }
function hideSpinner() { function hideSpinner(): void {
overlay.style.display = "none"; overlay.style.display = "none";
} }
function updateCharts() { function updateCharts(): void {
const u = window.location.hash.match("all") ? "./data.json" : "recent.json"; const u = window.location.hash.match("all") ? "./data.json" : "recent.json";
showSpinner(); showSpinner();

View file

@ -1,8 +1,9 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, testPerm, assert, assertEquals } from "../js/test_util.ts"; import { test, assertEquals } from "../js/test_util.ts";
import { runIfMain } from "../js/deps/https/deno.land/std/testing/mod.ts"; import { runIfMain } from "../js/deps/https/deno.land/std/testing/mod.ts";
import { import {
BenchmarkRun,
createBinarySizeColumns, createBinarySizeColumns,
createExecTimeColumns, createExecTimeColumns,
createThreadCountColumns, createThreadCountColumns,
@ -10,7 +11,8 @@ import {
createSha1List createSha1List
} from "./app.ts"; } from "./app.ts";
const regularData = [ /* eslint-disable @typescript-eslint/camelcase */
const regularData: BenchmarkRun[] = [
{ {
created_at: "2018-01-01T01:00:00Z", created_at: "2018-01-01T01:00:00Z",
sha1: "abcdef", sha1: "abcdef",
@ -97,7 +99,7 @@ const regularData = [
} }
]; ];
const irregularData = [ const irregularData: BenchmarkRun[] = [
{ {
created_at: "2018-01-01T01:00:00Z", created_at: "2018-01-01T01:00:00Z",
sha1: "123", sha1: "123",
@ -132,6 +134,7 @@ const irregularData = [
} }
} }
]; ];
/* eslint-enable @typescript-eslint/camelcase */
test(function createExecTimeColumnsRegularData() { test(function createExecTimeColumnsRegularData() {
const columns = createExecTimeColumns(regularData); const columns = createExecTimeColumns(regularData);

View file

@ -25,13 +25,14 @@
} }
function getHeaderEntriesInNodeJs(sourceHtml) { function getHeaderEntriesInNodeJs(sourceHtml) {
var cheerio = require("cheerio"); // eslint-disable-next-line @typescript-eslint/no-var-requires
var $ = cheerio.load(sourceHtml); const cheerio = require("cheerio");
var headers = $("h1, h2, h3, h4, h5, h6"); const $ = cheerio.load(sourceHtml);
const headers = $("h1, h2, h3, h4, h5, h6");
var headerList = []; const headerList = [];
for (var i = 0; i < headers.length; i++) { for (let i = 0; i < headers.length; i++) {
var el = headers[i]; const el = headers[i];
headerList.push(new TocEntry(el.name, $(el).text(), $(el).attr("id"))); headerList.push(new TocEntry(el.name, $(el).text(), $(el).attr("id")));
} }
@ -40,14 +41,14 @@
function getHeaderEntriesInBrowser(sourceHtml) { function getHeaderEntriesInBrowser(sourceHtml) {
// Generate dummy element // Generate dummy element
var source = document.createElement("div"); const source = document.createElement("div");
source.innerHTML = sourceHtml; source.innerHTML = sourceHtml;
// Find headers // Find headers
var headers = source.querySelectorAll("h1, h2, h3, h4, h5, h6"); const headers = source.querySelectorAll("h1, h2, h3, h4, h5, h6");
var headerList = []; const headerList = [];
for (var i = 0; i < headers.length; i++) { for (let i = 0; i < headers.length; i++) {
var el = headers[i]; const el = headers[i];
headerList.push(new TocEntry(el.tagName, el.textContent, el.id)); headerList.push(new TocEntry(el.tagName, el.textContent, el.id));
} }
@ -65,8 +66,8 @@
if (this.children.length === 0) { if (this.children.length === 0) {
return ""; return "";
} }
var result = "<ul>\n"; let result = "<ul>\n";
for (var i = 0; i < this.children.length; i++) { for (let i = 0; i < this.children.length; i++) {
result += this.children[i].toString(); result += this.children[i].toString();
} }
result += "</ul>\n"; result += "</ul>\n";
@ -74,7 +75,7 @@
}; };
TocEntry.prototype.toString = function() { TocEntry.prototype.toString = function() {
var result = "<li>"; let result = "<li>";
if (this.text) { if (this.text) {
result += '<a href="#' + this.anchor + '">' + this.text + "</a>"; result += '<a href="#' + this.anchor + '">' + this.text + "</a>";
} }
@ -85,9 +86,8 @@
function sortHeader(tocEntries, level) { function sortHeader(tocEntries, level) {
level = level || 1; level = level || 1;
var tagName = "H" + level, const tagName = "H" + level;
result = [], const result = [];
currentTocEntry;
function push(tocEntry) { function push(tocEntry) {
if (tocEntry !== undefined) { if (tocEntry !== undefined) {
@ -98,8 +98,9 @@
} }
} }
for (var i = 0; i < tocEntries.length; i++) { let currentTocEntry;
var tocEntry = tocEntries[i]; for (let i = 0; i < tocEntries.length; i++) {
const tocEntry = tocEntries[i];
if (tocEntry.tagName.toUpperCase() !== tagName) { if (tocEntry.tagName.toUpperCase() !== tagName) {
if (currentTocEntry === undefined) { if (currentTocEntry === undefined) {
currentTocEntry = new TocEntry(); currentTocEntry = new TocEntry();
@ -118,7 +119,7 @@
return { return {
type: "output", type: "output",
filter: function(sourceHtml) { filter: function(sourceHtml) {
var headerList = getHeaderEntries(sourceHtml); let headerList = getHeaderEntries(sourceHtml);
// No header found // No header found
if (headerList.length === 0) { if (headerList.length === 0) {
@ -134,7 +135,7 @@
} }
// Build result and replace all [toc] // Build result and replace all [toc]
var result = const result =
'<div class="toc">\n<ul>\n' + headerList.join("") + "</ul>\n</div>\n"; '<div class="toc">\n<ul>\n' + headerList.join("") + "</ul>\n</div>\n";
return sourceHtml.replace(/\[toc\]/gi, result); return sourceHtml.replace(/\[toc\]/gi, result);
} }