Skip to content

Commit

Permalink
Handle basic ASI
Browse files Browse the repository at this point in the history
  • Loading branch information
vesln committed Nov 19, 2013
1 parent 858a7d3 commit 7f3a6d6
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 27 deletions.
7 changes: 5 additions & 2 deletions lib/jsmd/rewriter.js
Expand Up @@ -60,7 +60,7 @@ Rewriter.prototype.compile = function(fn) {

Rewriter.prototype.build = function(code, fn) {
var tree = this.parse(code);
var ast = this.buildAst(tree);
var ast = this.buildAst(tree, code);
ast.body.unshift(this.header);
fn(generate(tree));
};
Expand Down Expand Up @@ -94,18 +94,21 @@ Rewriter.prototype.parse = function(code) {
* Build the new ast.
*
* @param {Object} ast
* @param {String} input
* @returns {Object} new ast
* @api private
*/

Rewriter.prototype.buildAst = function(ast) {
Rewriter.prototype.buildAst = function(ast, input) {
var assertions = this.assertions;
var self = this;

return replace(ast, {
leave: function(node) {
if (node.type !== EXPRESSION) return node;
var line = node.loc.end.line;
var newlines = input.substring(node.range[0], node.range[1]).match(/\n(?=\s*$)/g) || [];
line -= newlines.length;
if (!assertions.hasOwnProperty(line)) return node;
return self.assertion(assertions[line], node.expression);
}
Expand Down
17 changes: 17 additions & 0 deletions test/compile.test.js
@@ -0,0 +1,17 @@
var fs = require('fs');
var Rewriter = require('../lib/jsmd/rewriter');

describe('jsmd', function() {
it('compiles files as expected', function() {
var base = __dirname + '/compile/';

fs.readdirSync(base).forEach(function(file) {
if (!/\.md$/.test(file)) return;
var md = fs.readFileSync(base + file, 'utf8');
new Rewriter(md).compile(function(compiled) {
var expected = fs.readFileSync(base + file.replace(/md$/, 'js'), 'utf8');
compiled.should.eq(expected.trim());
});
});
});
});
6 changes: 6 additions & 0 deletions test/compile/ASI.js
@@ -0,0 +1,6 @@
var __jsmd__ = require('assert').deepEqual;
__jsmd__(1, Math.min(1, 2));
__jsmd__(2, Math.min(2, 3));
__jsmd__(3, Math.min(3, 4));
foo();
__jsmd__(4, bar());
11 changes: 11 additions & 0 deletions test/compile/ASI.md
@@ -0,0 +1,11 @@
```js
Math.min(1, 2) // => 1
Math.min(2, 3) // => 2

Math.min(3, 4)

// => 3
foo()

bar() // => 4
```
3 changes: 3 additions & 0 deletions test/compile/multi-line.js
@@ -0,0 +1,3 @@
var __jsmd__ = require('assert').deepEqual;
__jsmd__(2, Math.min(3, 2));
__jsmd__(4, Math.min(5, 4));
2 changes: 1 addition & 1 deletion test/fixtures/multi-line.md → test/compile/multi-line.md
Expand Up @@ -3,4 +3,4 @@ Math
.min(3, 2); // => 2
Math
.min(5, 4); // => 4
```
```
4 changes: 4 additions & 0 deletions test/compile/simple.js
@@ -0,0 +1,4 @@
var __jsmd__ = require('assert').deepEqual;
__jsmd__(1, Math.min(1, 2));
__jsmd__(2, Math.min(1, 2));
__jsmd__(3, Math.min(1, 2));
10 changes: 10 additions & 0 deletions test/compile/simple.md
@@ -0,0 +1,10 @@
```js
Math.min(1, 2) // => 1


Math.min(1, 2); // => 2



Math.min(1, 2) // => 3
```
4 changes: 0 additions & 4 deletions test/fixtures/ASI.md

This file was deleted.

20 changes: 0 additions & 20 deletions test/jsmd.test.js
Expand Up @@ -3,11 +3,6 @@ var verifyFile = require('..').verifyFile;
var run = function run(file, fn) {
verifyFile(fixture(file), fn);
};
var compile = function(file, fn){
var path = fixture(file);
var md = require('fs').readFileSync(path, 'utf8');
new Rewriter(md).compile(fn);
};

describe('jsmd', function() {
it('ignores code different than javascript', function(done) {
Expand Down Expand Up @@ -44,19 +39,4 @@ describe('jsmd', function() {
done();
});
});

it('can handle multi-line expressions', function(done) {
run('multi-line', function(err) {
should.not.exist(err);
done();
});
});

it.skip('supports automatic semicolon insertion (ASI)', function(done) {
compile('ASI', function(js) {
js.should.match(/__jsmd__\(1,/);
js.should.match(/__jsmd__\(2,/);
done();
});
});
});
1 change: 1 addition & 0 deletions test/support/bootstrap.js
Expand Up @@ -3,6 +3,7 @@
*/

var join = require('path').join;
var fs = require('fs');

/**
* External dependencies.
Expand Down

0 comments on commit 7f3a6d6

Please sign in to comment.