Skip to content

Commit

Permalink
#9 API changes for File module. Add get methods for retrive content a…
Browse files Browse the repository at this point in the history
…nd sourceMap
  • Loading branch information
tormozz48 committed Sep 4, 2015
1 parent 6c07fdd commit 002071a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 40 deletions.
24 changes: 19 additions & 5 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,26 @@ File.prototype = {
};
},

/**
* Returns sourceMap if it exists. null otherwise
* @returns {Object}
*/
getSourceMap: function () {
return this._map ? JSON.parse(this._map.toString()) : null;
},

/**
* Returns content joined with EOL.
* @returns {String}
*/
getContent: function () {
return this._content.join(os.EOL);
},

render: function () {
var content = this._content.join(os.EOL);
if (this._map) {
content = utils.joinContentAndSourceMap(content, this._map, this._opts);
}
return content;
return this._map
? utils.joinContentAndSourceMap(this.getContent(), this._map, this._opts)
: this.getContent();
}
};

Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
* @return {SourceMapConsumer}
*/
getSourceMap: function (content) {
return Array.isArray(content)
return Array.isArray(content)
? getFromLastLine_(content)
: getFromText_(content);

Expand Down
62 changes: 28 additions & 34 deletions tests/file.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var os = require('os');
var format = require('util').format;
var sinon = require('sinon');
var SourceMapConsumer = require('source-map').SourceMapConsumer;
var SourceMapGenerator = require('source-map').SourceMapGenerator;
var File = require('../lib/file');
var utils = require('../lib/utils');
Expand All @@ -16,23 +16,23 @@ describe('File', function () {
it('should add a new line to the output', function () {
file.writeLine('line 1');
file.writeLine('line 2');
file.render().should.equal('line 1\nline 2\n');
file.getContent().should.equal('line 1\nline 2\n');
});
});

describe('writeContent()', function () {
it('should add content to the output', function () {
file.writeContent('line 1\nline 2');
file.writeContent('line 3\nline 4');
file.render().should.equal('line 1\nline 2\nline 3\nline 4\n');
file.getContent().should.equal('line 1\nline 2\nline 3\nline 4\n');
});
});

describe('writeFileContent()', function () {
it('should add content to the output', function () {
file.writeFileContent('2.js', 'line 1\nline 2');
file.writeFileContent('2.js', 'line 3\nline 4');
file.render().should.equal('line 1\nline 2\nline 3\nline 4\n');
file.getContent().should.equal('line 1\nline 2\nline 3\nline 4\n');
});
});

Expand All @@ -42,7 +42,7 @@ describe('File', function () {
file.write('2');
file.write('3\n');
file.write('4\n5');
file.render().should.equal('123\n4\n5');
file.getContent().should.equal('123\n4\n5');
});

it('should move cursor forward', function () {
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('File', function () {
it('should add content to the output', function () {
file.writeFileFragment('2.js', 'line 1\nline 2', 1, 0);
file.writeFileFragment('2.js', 'line 3\nline 4', 2, 0);
file.render().should.equal('line 1\nline 2line 3\nline 4');
file.getContent().should.equal('line 1\nline 2line 3\nline 4');
});
});
});
Expand All @@ -84,17 +84,17 @@ describe('File', function () {
it('should add a new line to the output', function () {
file.writeLine('line 1');
file.writeLine('line 2');
hasSourceMap(file.render()).should.equal(true);
stripSourceMap(file.render()).should.equal('line 1\nline 2\n');

file.getContent().should.equal('line 1\nline 2\n');
});
});

describe('writeContent()', function () {
it('should add content to the output', function () {
file.writeContent('line 1\nline 2');
file.writeContent('line 3\nline 4');
hasSourceMap(file.render()).should.equal(true);
stripSourceMap(file.render()).should.equal('line 1\nline 2\nline 3\nline 4\n');

file.getContent().should.equal('line 1\nline 2\nline 3\nline 4\n');
});
});

Expand All @@ -104,8 +104,8 @@ describe('File', function () {
file.write('2');
file.write('3\n');
file.write('4\n5\n');
hasSourceMap(file.render()).should.equal(true);
stripSourceMap(file.render()).should.equal('123\n4\n5\n');

file.getContent().should.equal('123\n4\n5\n');
});

it('should move cursor forward', function () {
Expand Down Expand Up @@ -134,10 +134,9 @@ describe('File', function () {
file.write('_');
file.writeFileFragment('2.js', 'line 1\nline 2', 1, 0);
file.writeFileFragment('3.js', 'line 3\nline 4', 2, 3);
hasSourceMap(file.render()).should.equal(true);
stripSourceMap(file.render()).should.equal('_line 1\nline 2line 3\nline 4\n');

toReadableString(utils.getSourceMap(file.render())).should.equal(
file.getContent().should.equal('_line 1\nline 2line 3\nline 4');
toReadableString(file.getSourceMap()).should.equal(
[
'1, 1 -> 1, 0 2.js',
'2, 0 -> 2, 0 2.js',
Expand All @@ -152,8 +151,7 @@ describe('File', function () {
it('should add content to the output', function () {
file.writeFileContent('2.js', 'line 1\nline 2');
file.writeFileContent('2.js', 'line 3\nline 4');
hasSourceMap(file.render()).should.equal(true);
stripSourceMap(file.render()).should.equal('line 1\nline 2\nline 3\nline 4\n');
file.getContent().should.equal('line 1\nline 2\nline 3\nline 4\n');
});

describe('with existing source map', function() {
Expand All @@ -164,7 +162,7 @@ describe('File', function () {

file.writeFileContent('some-file.js', middleContent);

var pos = utils.getSourceMap(file.render()).originalPositionFor({line: 1, column: 0});
var pos = getOriginalSourceMapPosition({ line: 1, column: 0 }, file.getSourceMap());
pos.source.should.equal('source.js');
pos.line.should.equal(1);
pos.column.should.equal(0);
Expand All @@ -177,7 +175,7 @@ describe('File', function () {

file.writeFileContent('../some/path/some-file.js', middleContent);

var pos = utils.getSourceMap(file.render()).originalPositionFor({line: 1, column: 0});
var pos = getOriginalSourceMapPosition({ line: 1, column: 0 }, file.getSourceMap());
pos.source.should.equal('../some/other/path/source.js');
});

Expand All @@ -188,7 +186,7 @@ describe('File', function () {

file.writeFileContent('/some/path/some-file.js', middleContent);

var pos = utils.getSourceMap(file.render()).originalPositionFor({line: 1, column: 0});
var pos = getOriginalSourceMapPosition({ line: 1, column: 0 }, file.getSourceMap());
pos.source.should.equal('/some/other/path/source.js');
});

Expand All @@ -199,7 +197,7 @@ describe('File', function () {

file.writeFileContent('/some/path/some-file.js', middleContent);

var pos = utils.getSourceMap(file.render()).originalPositionFor({line: 1, column: 0});
var pos = getOriginalSourceMapPosition({ line: 1, column: 0 }, file.getSourceMap());
pos.source.should.equal('/other/path/source.js');
});

Expand All @@ -222,7 +220,7 @@ describe('File', function () {
file.writeFileContent('some-file.js', middleContent);

var expected = toReadableString(utils.getSourceMap(middleContent));
var actual = toReadableString(utils.getSourceMap(file.render()));
var actual = toReadableString(file.getSourceMap());
expected.should.equal(actual);
});

Expand Down Expand Up @@ -342,19 +340,11 @@ describe('File', function () {
});
});

function hasSourceMap(source) {
var lines = source.split('\n');
return lines[lines.length - 1].indexOf('//# sourceMappingURL=') === 0;
}

function stripSourceMap(source) {
var lines = source.split(os.EOL);
lines.pop();
return lines.join(os.EOL) + '\n';
}

///
function toReadableString(consumer) {
function toReadableString(sourceMap) {
var consumer = sourceMap instanceof SourceMapConsumer
? sourceMap
: new SourceMapConsumer(sourceMap);
var pieces = [];
consumer.eachMapping(function(mapping) {
pieces.push(format('%s, %s -> %s, %s %s',
Expand All @@ -367,3 +357,7 @@ function toReadableString(consumer) {
});
return pieces.join('\n');
}

function getOriginalSourceMapPosition(position, sourceMap) {
return (new SourceMapConsumer(sourceMap)).originalPositionFor(position);
}

0 comments on commit 002071a

Please sign in to comment.