-
Notifications
You must be signed in to change notification settings - Fork 2
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
+57
−49
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
@@ -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'); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не вижу смысла в этом тесте вместе с тестом с использованием |
||
}); | ||
|
||
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'); | ||
}); | ||
}); | ||
|
||
|
@@ -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 () { | ||
|
@@ -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'); | ||
}); | ||
}); | ||
}); | ||
|
@@ -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'); | ||
}); | ||
}); | ||
|
||
|
@@ -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 () { | ||
|
@@ -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', | ||
|
@@ -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() { | ||
|
@@ -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); | ||
|
@@ -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'); | ||
}); | ||
|
||
|
@@ -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'); | ||
}); | ||
|
||
|
@@ -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'); | ||
}); | ||
|
||
|
@@ -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); | ||
}); | ||
|
||
|
@@ -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', | ||
|
@@ -367,3 +357,7 @@ function toReadableString(consumer) { | |
}); | ||
return pieces.join('\n'); | ||
} | ||
|
||
function getOriginalSourceMapPosition(position, sourceMap) { | ||
return (new SourceMapConsumer(sourceMap)).originalPositionFor(position); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А нигде больше
SourceMapGenerator
не передаётся в этот хелпер?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Нет,
joinContentAndSourceMap
вызывается только из методаrender
модуляFile