Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#9 API changes for File and Utils modules #10

Merged
merged 2 commits into from
Sep 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 6 additions & 6 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ module.exports = {
* @return {SourceMapConsumer}
*/
getSourceMap: function (content) {
return Array.isArray(content)
? getFromLastLine_(content)
: getFromText_(content);
return Array.isArray(content)
? getFromLastLine_(content)
: getFromText_(content);

function getFromLastLine_(lines) {
return mkSourceMap(split(lines[lines.length - 1]).sourceMap);
Expand Down Expand Up @@ -62,9 +62,9 @@ module.exports = {
* @return {String}
*/
joinContentAndSourceMap: function (content, sourceMap, opts) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А нигде больше SourceMapGenerator не передаётся в этот хелпер?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нет, joinContentAndSourceMap вызывается только из метода render модуля File

if (!(sourceMap instanceof SourceMapGenerator)) {
throw new Error('sourceMap should be an instance of SourceMapGenerator');
}
sourceMap = sourceMap instanceof SourceMapGenerator
? sourceMap.toString()
: JSON.stringify(sourceMap);

opts = opts || {};
opts.comment = opts.comment || 'inline';
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');
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не вижу смысла в этом тесте вместе с тестом с использованием render. Ты можешь заюзать getContent и getSourceMap, где раньше использовался render (поскольку в них логики нет). А на render там есть отдельные тесты

});

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);
}
8 changes: 4 additions & 4 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ describe('Utils', function() {
result.should.be.equal(['line1', 'line2', SOURCE_MAP_LINE].join(os.EOL));
});

it('should throw if not a SourceMapGenerator passed', function() {
(function() {
return utils.joinContentAndSourceMap('some-content', SOURCE_MAP_CONSUMER);
}).should.throw();
it('should be able to take sourceMap without SouceMapGenerator wrapper', function () {
var sourceMap = JSON.parse(SOURCE_MAP_GENERATOR.toString());
var result = utils.joinContentAndSourceMap(['line1', 'line2'].join(os.EOL), sourceMap);
result.should.be.equal(['line1', 'line2', SOURCE_MAP_LINE].join(os.EOL));
});

it('should throw if source map not passed', function() {
Expand Down