Skip to content

Commit

Permalink
Merge pull request #1 from rjackson/cast-default-without-promise
Browse files Browse the repository at this point in the history
Allow casting without input containing catch/finally.
  • Loading branch information
stefanpenner committed May 27, 2014
2 parents 8e4053a + 4a725df commit fb75bc7
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 1 deletion.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ module.exports.compile = function(source) {
return code;
};

var TEST_REGEX = module.exports.TEST_REGEX = /catch|finally/i;
var TEST_REGEX = module.exports.TEST_REGEX = buildTestRegex();
function buildTestRegex() {
var regexString = Object.keys(identifierToLiteral).join('|');
return new RegExp(regexString, 'i');
}

module.exports.visit = function(ast) {
new ES6Safe().visit(ast);
Expand Down
112 changes: 112 additions & 0 deletions tests/compiler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,115 @@ describe('object member', function() {
astEqual(actual, expected, 'expected input.js and output.js to match');
});
});

describe('catch', function() {
it('works with literal syntax', function() {
var actual = compiler.compile('var object = {\ncatch: null,\n};');
var expected = 'var object = {\n"catch": null,\n};'

astEqual(actual, expected, 'expected input.js and output.js to match');
});

it('works with member syntax', function() {
var actual = compiler.compile('object.catch(function(){\n\n});');
var expected = 'object["catch"](function(){\n\n});';

astEqual(actual, expected, 'expected input.js and output.js to match');
});
});

describe('finally', function() {
it('works with literal syntax', function() {
var actual = compiler.compile('var object = {\nfinally: null,\n};');
var expected = 'var object = {\n"finally": null,\n};'

astEqual(actual, expected, 'expected input.js and output.js to match');
});

it('works with member syntax', function() {
var actual = compiler.compile('object.finally(function(){\n\n});');
var expected = 'object["finally"](function(){\n\n});';

astEqual(actual, expected, 'expected input.js and output.js to match');
});
});

describe('default', function() {
it('works with literal syntax', function() {
var actual = compiler.compile('var object = {\ndefault: null,\n};');
var expected = 'var object = {\n"default": null,\n};'

astEqual(actual, expected, 'expected input.js and output.js to match');
});

it('works with member syntax', function() {
var actual = compiler.compile('object.default(function(){\n\n});');
var expected = 'object["default"](function(){\n\n});';

astEqual(actual, expected, 'expected input.js and output.js to match');
});
});

describe('new', function() {
it('works with literal syntax', function() {
var actual = compiler.compile('var object = {\nnew: null,\n};');
var expected = 'var object = {\n"new": null,\n};'

astEqual(actual, expected, 'expected input.js and output.js to match');
});

it('works with member syntax', function() {
var actual = compiler.compile('object.new(function(){\n\n});');
var expected = 'object["new"](function(){\n\n});';

astEqual(actual, expected, 'expected input.js and output.js to match');
});
});

describe('throw', function() {
it('works with literal syntax', function() {
var actual = compiler.compile('var object = {\nthrow: null,\n};');
var expected = 'var object = {\n"throw": null,\n};'

astEqual(actual, expected, 'expected input.js and output.js to match');
});

it('works with member syntax', function() {
var actual = compiler.compile('object.throw(function(){\n\n});');
var expected = 'object["throw"](function(){\n\n});';

astEqual(actual, expected, 'expected input.js and output.js to match');
});
});

describe('return', function() {
it('works with literal syntax', function() {
var actual = compiler.compile('var object = {\nreturn: null,\n};');
var expected = 'var object = {\n"return": null,\n};'

astEqual(actual, expected, 'expected input.js and output.js to match');
});

it('works with member syntax', function() {
var actual = compiler.compile('object.return(function(){\n\n});');
var expected = 'object["return"](function(){\n\n});';

astEqual(actual, expected, 'expected input.js and output.js to match');
});
});

describe('import', function() {
it('works with literal syntax', function() {
var actual = compiler.compile('var object = {\nimport: null,\n};');
var expected = 'var object = {\n"import": null,\n};'

astEqual(actual, expected, 'expected input.js and output.js to match');
});

it('works with member syntax', function() {
var actual = compiler.compile('object.import(function(){\n\n});');
var expected = 'object["import"](function(){\n\n});';

astEqual(actual, expected, 'expected input.js and output.js to match');
});
});

0 comments on commit fb75bc7

Please sign in to comment.