Pass spec code through Babel before serving it to the browser

This will allow to use ES2015 constructs in spec code.

The change required introducing a small server, which serves both PEG.js
and spec code passed through Babel and bundled together. This allowed to
convert the specs to regular modules and get rid of the hackery that was
previously needed to make them run both in Node.js and in the browser.

Note the specs no longer exercise the browser version. This will allow
to spec PEG.js internals in the future.

See #442.
redux
David Majda 8 years ago
parent d239bf0107
commit 5c40fff136

@ -24,6 +24,8 @@ PARSER_OUT_FILE_NEW = $(LIB_DIR)/parser.js.new
BROWSER_FILE_DEV = $(BROWSER_DIR)/peg-$(PEGJS_VERSION).js BROWSER_FILE_DEV = $(BROWSER_DIR)/peg-$(PEGJS_VERSION).js
BROWSER_FILE_MIN = $(BROWSER_DIR)/peg-$(PEGJS_VERSION).min.js BROWSER_FILE_MIN = $(BROWSER_DIR)/peg-$(PEGJS_VERSION).min.js
SPEC_SERVER_FILE = $(SPEC_DIR)/server
VERSION_FILE = VERSION VERSION_FILE = VERSION
# ===== Executables ===== # ===== Executables =====
@ -103,6 +105,7 @@ lint:
$(ESLINT) \ $(ESLINT) \
`find $(LIB_DIR) -name '*.js'` \ `find $(LIB_DIR) -name '*.js'` \
`find $(SPEC_DIR) -name '*.js' -and -not -path '$(SPEC_DIR)/vendor/*'` \ `find $(SPEC_DIR) -name '*.js' -and -not -path '$(SPEC_DIR)/vendor/*'` \
$(SPEC_SERVER_FILE) \
$(BENCHMARK_DIR)/*.js \ $(BENCHMARK_DIR)/*.js \
$(BENCHMARK_RUN) \ $(BENCHMARK_RUN) \
$(PEGJS) $(PEGJS)

@ -1,5 +1,6 @@
{ {
"env": { "env": {
"commonjs": true,
"jasmine": true "jasmine": true
} }
} }

@ -38,18 +38,12 @@ All commands in the following steps need to be executed in PEG.js root directory
$ npm install $ npm install
``` ```
3. Build browser version of PEG.js: 3. Serve the spec suite using a web server:
```console ```console
$ make browser $ spec/server
``` ```
4. Serve PEG.js root directory using a web server: 4. Point your browser to the [spec suite](http://localhost:8000/).
```console 5. Watch the specs pass (or fail).
$ node_modules/.bin/http-server
```
5. Point your browser to the [spec suite](http://localhost:8080/spec/index.html).
6. Watch the specs pass (or fail).

@ -1,8 +1,10 @@
/* eslint no-console: 0 */ /* eslint no-console: 0 */
/* global peg, console */ /* global console */
"use strict"; "use strict";
var peg = require("../../lib/peg");
describe("generated parser API", function() { describe("generated parser API", function() {
describe("parse", function() { describe("parse", function() {
it("parses input", function() { it("parses input", function() {

@ -1,7 +1,7 @@
/* global peg */
"use strict"; "use strict";
var peg = require("../../lib/peg");
describe("PEG.js API", function() { describe("PEG.js API", function() {
describe("generate", function() { describe("generate", function() {
it("generates a parser", function() { it("generates a parser", function() {

@ -1,7 +1,7 @@
/* global peg */
"use strict"; "use strict";
var peg = require("../../lib/peg");
describe("plugin API", function() { describe("plugin API", function() {
beforeEach(function() { beforeEach(function() {
this.addMatchers({ this.addMatchers({

@ -1,8 +1,10 @@
/* eslint no-console: 0 */ /* eslint no-console: 0 */
/* global peg, console */ /* global console */
"use strict"; "use strict";
var peg = require("../../lib/peg");
describe("generated parser behavior", function() { describe("generated parser behavior", function() {
function varyOptimizationOptions(block) { function varyOptimizationOptions(block) {
function clone(object) { function clone(object) {

@ -1,9 +0,0 @@
/* global require */
"use strict";
(function(root) {
if (typeof module !== 'undefined') {
root.peg = require("../lib/peg.js");
}
}(this));

@ -6,21 +6,7 @@
<link rel="stylesheet" href="vendor/jasmine/jasmine.css"> <link rel="stylesheet" href="vendor/jasmine/jasmine.css">
<script src="vendor/jasmine/jasmine.js"></script> <script src="vendor/jasmine/jasmine.js"></script>
<script src="vendor/jasmine/jasmine-html.js"></script> <script src="vendor/jasmine/jasmine-html.js"></script>
<script src="../browser/peg-0.10.0.js"></script> <script src="bundle.js"></script>
<script src="helpers.js"></script>
<script src="unit/parser.spec.js"></script>
<script src="unit/compiler/passes/helpers.js"></script>
<script src="unit/compiler/passes/report-undefined-rules.spec.js"></script>
<script src="unit/compiler/passes/report-duplicate-rules.spec.js"></script>
<script src="unit/compiler/passes/report-duplicate-labels.spec.js"></script>
<script src="unit/compiler/passes/report-infinite-recursion.spec.js"></script>
<script src="unit/compiler/passes/report-infinite-repetition.spec.js"></script>
<script src="unit/compiler/passes/remove-proxy-rules.spec.js"></script>
<script src="unit/compiler/passes/generate-bytecode.spec.js"></script>
<script src="api/pegjs-api.spec.js"></script>
<script src="api/plugin-api.spec.js"></script>
<script src="api/generated-parser-api.spec.js"></script>
<script src="behavior/generated-parser-behavior.spec.js"></script>
<script> <script>
(function() { (function() {
var env = jasmine.getEnv(), var env = jasmine.getEnv(),

@ -0,0 +1,37 @@
#!/usr/bin/env node
/* eslint-env node */
/* eslint no-console: 0 */
/*
* Small server whose main purpose is to ensure that both the specced code and
* the specs get passed through Babel & Browserify before they are served to the
* browser.
*/
var express = require("express"),
logger = require("morgan"),
glob = require("glob"),
browserify = require("browserify"),
babelify = require("babelify");
var app = express();
app.use(logger("dev"));
app.use(express.static(__dirname));
app.get("/bundle.js", function(req, res) {
var files = glob.sync(__dirname + "/**/*.js", {
ignore: __dirname + "/vendor/**/*"
});
browserify(files)
.transform(babelify, { presets: "es2015", compact: false })
.bundle()
.pipe(res);
});
app.listen(8000, function() {
console.log("Spec server running at http://localhost:8000...");
});

@ -1,7 +1,7 @@
/* global peg */
"use strict"; "use strict";
var peg = require("../../../../lib/peg");
describe("compiler pass |generateBytecode|", function() { describe("compiler pass |generateBytecode|", function() {
var pass = peg.compiler.passes.generate.generateBytecode; var pass = peg.compiler.passes.generate.generateBytecode;

@ -1,7 +1,7 @@
/* global peg */
"use strict"; "use strict";
var peg = require("../../../../lib/peg");
beforeEach(function() { beforeEach(function() {
this.addMatchers({ this.addMatchers({
toChangeAST: function(grammar, details, options) { toChangeAST: function(grammar, details, options) {

@ -1,7 +1,7 @@
/* global peg */
"use strict"; "use strict";
var peg = require("../../../../lib/peg");
describe("compiler pass |removeProxyRules|", function() { describe("compiler pass |removeProxyRules|", function() {
var pass = peg.compiler.passes.transform.removeProxyRules; var pass = peg.compiler.passes.transform.removeProxyRules;

@ -1,7 +1,7 @@
/* global peg */
"use strict"; "use strict";
var peg = require("../../../../lib/peg");
describe("compiler pass |reportDuplicateLabels|", function() { describe("compiler pass |reportDuplicateLabels|", function() {
var pass = peg.compiler.passes.check.reportDuplicateLabels; var pass = peg.compiler.passes.check.reportDuplicateLabels;

@ -1,7 +1,7 @@
/* global peg */
"use strict"; "use strict";
var peg = require("../../../../lib/peg");
describe("compiler pass |reportDuplicateRules|", function() { describe("compiler pass |reportDuplicateRules|", function() {
var pass = peg.compiler.passes.check.reportDuplicateRules; var pass = peg.compiler.passes.check.reportDuplicateRules;

@ -1,7 +1,7 @@
/* global peg */
"use strict"; "use strict";
var peg = require("../../../../lib/peg");
describe("compiler pass |reportInfiniteRecursion|", function() { describe("compiler pass |reportInfiniteRecursion|", function() {
var pass = peg.compiler.passes.check.reportInfiniteRecursion; var pass = peg.compiler.passes.check.reportInfiniteRecursion;

@ -1,7 +1,7 @@
/* global peg */
"use strict"; "use strict";
var peg = require("../../../../lib/peg");
describe("compiler pass |reportInfiniteRepetition|", function() { describe("compiler pass |reportInfiniteRepetition|", function() {
var pass = peg.compiler.passes.check.reportInfiniteRepetition; var pass = peg.compiler.passes.check.reportInfiniteRepetition;

@ -1,7 +1,7 @@
/* global peg */
"use strict"; "use strict";
var peg = require("../../../../lib/peg");
describe("compiler pass |reportUndefinedRules|", function() { describe("compiler pass |reportUndefinedRules|", function() {
var pass = peg.compiler.passes.check.reportUndefinedRules; var pass = peg.compiler.passes.check.reportUndefinedRules;

@ -1,7 +1,7 @@
/* global peg */
"use strict"; "use strict";
var peg = require("../../lib/peg");
describe("PEG.js grammar parser", function() { describe("PEG.js grammar parser", function() {
var literalAbcd = { type: "literal", value: "abcd", ignoreCase: false }, var literalAbcd = { type: "literal", value: "abcd", ignoreCase: false },
literalEfgh = { type: "literal", value: "efgh", ignoreCase: false }, literalEfgh = { type: "literal", value: "efgh", ignoreCase: false },

Loading…
Cancel
Save