From ff8e877fce9e1cdd6b28293e53c6332e8f739229 Mon Sep 17 00:00:00 2001 From: David Majda Date: Sun, 4 May 2014 14:11:44 +0200 Subject: [PATCH] Change module exporting style Modules now generally store the exported object in a named variable or function first and only assign |module.exports| at the very end. This is a difference when compared to style used until now, where most modules started with a |module.exports| assignment. I think the explicit name helps readability and understandability. --- lib/compiler.js | 4 +++- lib/compiler/opcodes.js | 4 +++- lib/compiler/passes/generate-bytecode.js | 6 ++++-- lib/compiler/passes/generate-javascript.js | 6 ++++-- lib/compiler/passes/remove-proxy-rules.js | 6 ++++-- lib/compiler/passes/report-left-recursion.js | 6 ++++-- lib/compiler/passes/report-missing-rules.js | 6 ++++-- lib/grammar-error.js | 8 +++++--- lib/peg.js | 4 +++- 9 files changed, 34 insertions(+), 16 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index fdaf739..95e682b 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -1,6 +1,6 @@ var utils = require("./utils"); -module.exports = { +var compiler = { /* * Compiler passes. * @@ -51,3 +51,5 @@ module.exports = { } } }; + +module.exports = compiler; diff --git a/lib/compiler/opcodes.js b/lib/compiler/opcodes.js index 7c46ce9..aa99a6c 100644 --- a/lib/compiler/opcodes.js +++ b/lib/compiler/opcodes.js @@ -1,5 +1,5 @@ /* Bytecode instruction opcodes. */ -module.exports = { +var opcodes = { /* Stack Manipulation */ PUSH: 0, // PUSH c @@ -44,3 +44,5 @@ module.exports = { SILENT_FAILS_ON: 24, // SILENT_FAILS_ON SILENT_FAILS_OFF: 25 // SILENT_FAILS_FF }; + +module.exports = opcodes; diff --git a/lib/compiler/passes/generate-bytecode.js b/lib/compiler/passes/generate-bytecode.js index 18611cd..97605ff 100644 --- a/lib/compiler/passes/generate-bytecode.js +++ b/lib/compiler/passes/generate-bytecode.js @@ -169,7 +169,7 @@ var utils = require("../../utils"), * * silentFails--; */ -module.exports = function(ast) { +function generateBytecode(ast) { var consts = []; function addConst(value) { @@ -595,4 +595,6 @@ module.exports = function(ast) { }); generate(ast); -}; +} + +module.exports = generateBytecode; diff --git a/lib/compiler/passes/generate-javascript.js b/lib/compiler/passes/generate-javascript.js index 2f1a6ed..85b7a98 100644 --- a/lib/compiler/passes/generate-javascript.js +++ b/lib/compiler/passes/generate-javascript.js @@ -2,7 +2,7 @@ var utils = require("../../utils"), op = require("../opcodes"); /* Generates parser JavaScript code. */ -module.exports = function(ast, options) { +function generateJavaScript(ast, options) { /* These only indent non-empty lines to avoid trailing whitespace. */ function indent2(code) { return code.replace(/^(.+)$/gm, ' $1'); } function indent4(code) { return code.replace(/^(.+)$/gm, ' $1'); } @@ -974,4 +974,6 @@ module.exports = function(ast, options) { ].join('\n')); ast.code = parts.join('\n'); -}; +} + +module.exports = generateJavaScript; diff --git a/lib/compiler/passes/remove-proxy-rules.js b/lib/compiler/passes/remove-proxy-rules.js index c269328..406d257 100644 --- a/lib/compiler/passes/remove-proxy-rules.js +++ b/lib/compiler/passes/remove-proxy-rules.js @@ -3,7 +3,7 @@ var utils = require("../../utils"); /* * Removes proxy rules -- that is, rules that only delegate to other rule. */ -module.exports = function(ast, options) { +function removeProxyRules(ast, options) { function isProxyRule(node) { return node.type === "rule" && node.expression.type === "rule_ref"; } @@ -71,4 +71,6 @@ module.exports = function(ast, options) { utils.each(indices, function(index) { ast.rules.splice(index, 1); }); -}; +} + +module.exports = removeProxyRules; diff --git a/lib/compiler/passes/report-left-recursion.js b/lib/compiler/passes/report-left-recursion.js index f868b6d..0387971 100644 --- a/lib/compiler/passes/report-left-recursion.js +++ b/lib/compiler/passes/report-left-recursion.js @@ -2,7 +2,7 @@ var utils = require("../../utils"), GrammarError = require("../../grammar-error"); /* Checks that no left recursion is present. */ -module.exports = function(ast) { +function reportLeftRecursion(ast) { function nop() {} function checkExpression(node, appliedRules) { @@ -62,4 +62,6 @@ module.exports = function(ast) { }); check(ast, []); -}; +} + +module.exports = reportLeftRecursion; diff --git a/lib/compiler/passes/report-missing-rules.js b/lib/compiler/passes/report-missing-rules.js index cf3afc9..22d1104 100644 --- a/lib/compiler/passes/report-missing-rules.js +++ b/lib/compiler/passes/report-missing-rules.js @@ -2,7 +2,7 @@ var utils = require("../../utils"), GrammarError = require("../../grammar-error"); /* Checks that all referenced rules exist. */ -module.exports = function(ast) { +function reportMissingRules(ast) { function nop() {} function checkExpression(node) { check(node.expression); } @@ -43,4 +43,6 @@ module.exports = function(ast) { }); check(ast); -}; +} + +module.exports = reportMissingRules; diff --git a/lib/grammar-error.js b/lib/grammar-error.js index 91e3f62..7f8d5b4 100644 --- a/lib/grammar-error.js +++ b/lib/grammar-error.js @@ -1,9 +1,11 @@ var utils = require("./utils"); /* Thrown when the grammar contains an error. */ -module.exports = function(message) { +function GrammarError(message) { this.name = "GrammarError"; this.message = message; -}; +} -utils.subclass(module.exports, Error); +utils.subclass(GrammarError, Error); + +module.exports = GrammarError; diff --git a/lib/peg.js b/lib/peg.js index b83aa03..2005219 100644 --- a/lib/peg.js +++ b/lib/peg.js @@ -1,6 +1,6 @@ var utils = require("./utils"); -module.exports = { +var PEG = { /* PEG.js version (uses semantic versioning). */ VERSION: "0.8.0", @@ -48,3 +48,5 @@ module.exports = { ); } }; + +module.exports = PEG;