Skip to content

Commit

Permalink
Merge pull request #41 from hildjj/const
Browse files Browse the repository at this point in the history
Move to using const wherever possible
  • Loading branch information
phpnode authored Apr 16, 2021
2 parents e69be31 + 19bd44e commit 2b2cf87
Show file tree
Hide file tree
Showing 31 changed files with 437 additions and 440 deletions.
5 changes: 1 addition & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,10 +627,7 @@ module.exports = {
// allow to make exceptions for these cases.
"prefer-arrow-callback": "off",

// Disabled because using `const` for anything else than for immutable
// variables of permanent character (generally spelled in `ALL_CAPS`) feels
// confusing.
"prefer-const": "off",
"prefer-const": "error",

"prefer-numeric-literals": "error",

Expand Down
2 changes: 1 addition & 1 deletion benchmark/benchmarks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

let benchmarks = [
const benchmarks = [
{
id: "json",
title: "JSON",
Expand Down
10 changes: 5 additions & 5 deletions benchmark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

/* eslint-env browser, jquery */

let Runner = require("./runner.js");
let benchmarks = require("./benchmarks.js");
const Runner = require("./runner.js");
const benchmarks = require("./benchmarks.js");

$("#run").click(() => {
// Results Table Manipulation

let resultsTable = $("#results-table");
const resultsTable = $("#results-table");

function appendHeading(heading) {
resultsTable.append(
Expand Down Expand Up @@ -60,8 +60,8 @@ $("#run").click(() => {
//
// 2. To minimize random errors.

let runCount = parseInt($("#run-count").val(), 10);
let options = {
const runCount = parseInt($("#run-count").val(), 10);
const options = {
cache: $("#cache").is(":checked"),
optimize: $("#optimize").val()
};
Expand Down
14 changes: 7 additions & 7 deletions benchmark/runner.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use strict";

let peg = require("../lib/peg");
const peg = require("../lib/peg");

let Runner = {
const Runner = {
run(benchmarks, runCount, options, callbacks) {
// Queue

let Q = {
const Q = {
functions: [],

add(f) {
Expand Down Expand Up @@ -37,7 +37,7 @@ let Runner = {
// The enqueued functions share state, which is all stored in the properties
// of the |state| object.

let state = {};
const state = {};

function initialize() {
callbacks.start();
Expand All @@ -63,15 +63,15 @@ let Runner = {
return function() {
callbacks.testStart(benchmark, test);

let input = callbacks.readFile(benchmark.id + "/" + test.file);
const input = callbacks.readFile(benchmark.id + "/" + test.file);

let parseTime = 0;
for (let i = 0; i < runCount; i++) {
let t = (new Date()).getTime();
const t = (new Date()).getTime();
state.parser.parse(input);
parseTime += (new Date()).getTime() - t;
}
let averageParseTime = parseTime / runCount;
const averageParseTime = parseTime / runCount;

callbacks.testFinish(benchmark, test, input.length, averageParseTime);

Expand Down
6 changes: 3 additions & 3 deletions lib/compiler/asts.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";

let visitor = require("./visitor");
const visitor = require("./visitor");

// AST utilities.
let asts = {
const asts = {
findRule(ast, name) {
for (let i = 0; i < ast.rules.length; i++) {
if (ast.rules[i].name === name) {
Expand Down Expand Up @@ -32,7 +32,7 @@ let asts = {
return consumes(node.expression);
}

let consumes = visitor.build({
const consumes = visitor.build({
rule: consumesExpression,
named: consumesExpression,

Expand Down
22 changes: 11 additions & 11 deletions lib/compiler/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
"use strict";

let generateBytecode = require("./passes/generate-bytecode");
let generateJS = require("./passes/generate-js");
let removeProxyRules = require("./passes/remove-proxy-rules");
let reportDuplicateLabels = require("./passes/report-duplicate-labels");
let reportDuplicateRules = require("./passes/report-duplicate-rules");
let reportInfiniteRecursion = require("./passes/report-infinite-recursion");
let reportInfiniteRepetition = require("./passes/report-infinite-repetition");
let reportUndefinedRules = require("./passes/report-undefined-rules");
let visitor = require("./visitor");
const generateBytecode = require("./passes/generate-bytecode");
const generateJS = require("./passes/generate-js");
const removeProxyRules = require("./passes/remove-proxy-rules");
const reportDuplicateLabels = require("./passes/report-duplicate-labels");
const reportDuplicateRules = require("./passes/report-duplicate-rules");
const reportInfiniteRecursion = require("./passes/report-infinite-recursion");
const reportInfiniteRepetition = require("./passes/report-infinite-repetition");
const reportUndefinedRules = require("./passes/report-undefined-rules");
const visitor = require("./visitor");

function processOptions(options, defaults) {
let processedOptions = {};
const processedOptions = {};

Object.keys(options).forEach(name => {
processedOptions[name] = options[name];
Expand All @@ -26,7 +26,7 @@ function processOptions(options, defaults) {
return processedOptions;
}

let compiler = {
const compiler = {
// AST node visitor builder. Useful mainly for plugins which manipulate the
// AST.
visitor: visitor,
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function hex(ch) { return ch.charCodeAt(0).toString(16).toUpperCase(); }

// JavaScript code generation helpers.
let js = {
const js = {
stringEscape(s) {
// ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a string
// literal except for the closing quote character, backslash, carriage
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/opcodes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

// Bytecode instruction opcodes.
let opcodes = {
const opcodes = {
// Stack Manipulation

PUSH: 0, // PUSH c
Expand Down
57 changes: 29 additions & 28 deletions lib/compiler/passes/generate-bytecode.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";

let asts = require("../asts");
let js = require("../js");
let op = require("../opcodes");
let visitor = require("../visitor");
const asts = require("../asts");
const js = require("../js");
const op = require("../opcodes");
const visitor = require("../visitor");

// Generates bytecode.
//
Expand Down Expand Up @@ -188,10 +188,10 @@ let visitor = require("../visitor");
//
// silentFails--;
function generateBytecode(ast) {
let consts = [];
const consts = [];

function addConst(value) {
let index = consts.indexOf(value);
const index = consts.indexOf(value);

return index === -1 ? consts.push(value) - 1 : index;
}
Expand All @@ -203,7 +203,7 @@ function generateBytecode(ast) {
}

function cloneEnv(env) {
let clone = {};
const clone = {};

Object.keys(env).forEach(name => {
clone[name] = env[name];
Expand All @@ -229,7 +229,7 @@ function generateBytecode(ast) {
}

function buildCall(functionIndex, delta, env, sp) {
let params = Object.keys(env).map(name => sp - env[name]);
const params = Object.keys(env).map(name => sp - env[name]);

return [op.CALL, functionIndex, delta, params.length].concat(params);
}
Expand Down Expand Up @@ -261,7 +261,7 @@ function generateBytecode(ast) {
}

function buildSemanticPredicate(code, negative, context) {
let functionIndex = addFunctionConst(Object.keys(context.env), code);
const functionIndex = addFunctionConst(Object.keys(context.env), code);

return buildSequence(
[op.UPDATE_SAVED_POS],
Expand All @@ -287,7 +287,7 @@ function generateBytecode(ast) {
);
}

let generate = visitor.build({
const generate = visitor.build({
grammar(node) {
node.rules.forEach(generate);

Expand All @@ -303,7 +303,7 @@ function generateBytecode(ast) {
},

named(node, context) {
let nameIndex = addConst(
const nameIndex = addConst(
"peg$otherExpectation(\"" + js.stringEscape(node.name) + "\")"
);

Expand Down Expand Up @@ -344,15 +344,15 @@ function generateBytecode(ast) {
},

action(node, context) {
let env = cloneEnv(context.env);
let emitCall = node.expression.type !== "sequence"
const env = cloneEnv(context.env);
const emitCall = node.expression.type !== "sequence"
|| node.expression.elements.length === 0;
let expressionCode = generate(node.expression, {
const expressionCode = generate(node.expression, {
sp: context.sp + (emitCall ? 1 : 0),
env: env,
action: node
});
let functionIndex = addFunctionConst(Object.keys(env), node.code);
const functionIndex = addFunctionConst(Object.keys(env), node.code);

return emitCall
? buildSequence(
Expand All @@ -374,7 +374,8 @@ function generateBytecode(ast) {
sequence(node, context) {
function buildElementsCode(elements, context) {
if (elements.length > 0) {
let processedCount = node.elements.length - elements.slice(1).length;
const processedCount
= node.elements.length - elements.slice(1).length;

return buildSequence(
generate(elements[0], {
Expand All @@ -398,7 +399,7 @@ function generateBytecode(ast) {
);
} else {
if (context.action) {
let functionIndex = addFunctionConst(
const functionIndex = addFunctionConst(
Object.keys(context.env),
context.action.code
);
Expand Down Expand Up @@ -430,7 +431,7 @@ function generateBytecode(ast) {
},

labeled(node, context) {
let env = cloneEnv(context.env);
const env = cloneEnv(context.env);

context.env[node.label] = context.sp + 1;

Expand Down Expand Up @@ -481,7 +482,7 @@ function generateBytecode(ast) {
},

zero_or_more(node, context) {
let expressionCode = generate(node.expression, {
const expressionCode = generate(node.expression, {
sp: context.sp + 1,
env: cloneEnv(context.env),
action: null
Expand All @@ -496,7 +497,7 @@ function generateBytecode(ast) {
},

one_or_more(node, context) {
let expressionCode = generate(node.expression, {
const expressionCode = generate(node.expression, {
sp: context.sp + 1,
env: cloneEnv(context.env),
action: null
Expand Down Expand Up @@ -535,13 +536,13 @@ function generateBytecode(ast) {

literal(node) {
if (node.value.length > 0) {
let stringIndex = addConst("\""
const stringIndex = addConst("\""
+ js.stringEscape(
node.ignoreCase ? node.value.toLowerCase() : node.value
)
+ "\""
);
let expectedIndex = addConst(
const expectedIndex = addConst(
"peg$literalExpectation("
+ "\"" + js.stringEscape(node.value) + "\", "
+ node.ignoreCase
Expand All @@ -561,14 +562,14 @@ function generateBytecode(ast) {
[op.FAIL, expectedIndex]
);
} else {
let stringIndex = addConst("\"\"");
const stringIndex = addConst("\"\"");

return [op.PUSH, stringIndex];
}
},

class(node) {
let regexp = "/^["
const regexp = "/^["
+ (node.inverted ? "^" : "")
+ node.parts.map(part =>
Array.isArray(part)
Expand All @@ -578,15 +579,15 @@ function generateBytecode(ast) {
: js.regexpClassEscape(part)
).join("")
+ "]/" + (node.ignoreCase ? "i" : "");
let parts = "["
const parts = "["
+ node.parts.map(part =>
Array.isArray(part)
? "[\"" + js.stringEscape(part[0]) + "\", \"" + js.stringEscape(part[1]) + "\"]"
: "\"" + js.stringEscape(part) + "\""
).join(", ")
+ "]";
let regexpIndex = addConst(regexp);
let expectedIndex = addConst(
const regexpIndex = addConst(regexp);
const expectedIndex = addConst(
"peg$classExpectation("
+ parts + ", "
+ node.inverted + ", "
Expand All @@ -602,7 +603,7 @@ function generateBytecode(ast) {
},

any() {
let expectedIndex = addConst("peg$anyExpectation()");
const expectedIndex = addConst("peg$anyExpectation()");

return buildCondition(
[op.MATCH_ANY],
Expand Down
Loading

0 comments on commit 2b2cf87

Please sign in to comment.