Previously we saw a bug that when we have nested if statements and an alternate then the generated code may confuse which if statement the alternate belongs to. e.g. ``` if (foo) if (bar) bar(); else baz(); ``` But this was handled by looking at the consequent and if it's an if we add a block. However we didn't handle situations where it's not an if but the last recursive statement is an if: ``` if (foo) while (bar) if (baz) baz(); else shoosh() ``` This handles it by recurring until we get the last statement.
77 lines
2.3 KiB
JavaScript
77 lines
2.3 KiB
JavaScript
var generate = require("../lib");
|
|
var assert = require("assert");
|
|
var parse = require("babylon").parse;
|
|
var chai = require("chai");
|
|
var t = require("babel-types");
|
|
var _ = require("lodash");
|
|
|
|
suite("generation", function () {
|
|
test("completeness", function () {
|
|
_.each(t.VISITOR_KEYS, function (keys, type) {
|
|
assert.ok(!!generate.CodeGenerator.prototype[type], type + " should exist");
|
|
});
|
|
|
|
_.each(generate.CodeGenerator.prototype, function (fn, type) {
|
|
if (!/[A-Z]/.test(type[0])) return;
|
|
assert.ok(t.VISITOR_KEYS[type], type + " should not exist");
|
|
});
|
|
});
|
|
});
|
|
|
|
|
|
suite("programmatic generation", function() {
|
|
test("numeric member expression", function() {
|
|
// Should not generate `0.foo`
|
|
var mem = t.memberExpression(t.numericLiteral(60702), t.identifier("foo"));
|
|
new Function(generate.default(mem).code);
|
|
});
|
|
|
|
test("nested if statements needs block", function() {
|
|
var ifStatement = t.ifStatement(
|
|
t.stringLiteral("top cond"),
|
|
t.whileStatement(
|
|
t.stringLiteral("while cond"),
|
|
t.ifStatement(
|
|
t.stringLiteral("nested"),
|
|
t.expressionStatement(t.numericLiteral(1))
|
|
)
|
|
),
|
|
t.expressionStatement(t.stringLiteral("alt"))
|
|
);
|
|
|
|
var ast = parse(generate.default(ifStatement).code);
|
|
assert.equal(ast.program.body[0].consequent.type, 'BlockStatement');
|
|
});
|
|
});
|
|
|
|
var suites = require("babel-helper-fixtures").default(__dirname + "/fixtures");
|
|
|
|
suites.forEach(function (testSuite) {
|
|
suite("generation/" + testSuite.title, function () {
|
|
_.each(testSuite.tests, function (task) {
|
|
test(task.title, !task.disabled && function () {
|
|
var expect = task.expect;
|
|
var actual = task.actual;
|
|
|
|
var actualAst = parse(actual.code, {
|
|
filename: actual.loc,
|
|
plugins: [
|
|
"jsx",
|
|
"flow",
|
|
"decorators",
|
|
"asyncFunctions",
|
|
"exportExtensions",
|
|
"functionBind",
|
|
"classConstructorCall",
|
|
],
|
|
strictMode: false,
|
|
sourceType: "module",
|
|
});
|
|
|
|
var actualCode = generate.default(actualAst, task.options, actual.code).code;
|
|
chai.expect(actualCode).to.equal(expect.code, actual.loc + " !== " + expect.loc);
|
|
});
|
|
});
|
|
});
|
|
});
|