Skip to content

Commit be22c40

Browse files
conradzNicolas Gallagher
authored andcommitted
Add tests for applying source maps and refactor
1 parent 8c63bd1 commit be22c40

File tree

6 files changed

+40
-3
lines changed

6 files changed

+40
-3
lines changed

lib/source-map-support.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ exports.emit = function(str, pos, startOnly) {
101101

102102
exports.addFile = function(pos) {
103103
var file = pos.source || 'source.css';
104-
if (this.files.hasOwnProperty(file)) return;
104+
if (Object.prototype.hasOwnProperty.call(this.files, file)) return;
105+
if (typeof pos.content !== 'string') return;
105106
this.files[file] = pos.content;
106107
};
107108

@@ -110,14 +111,14 @@ exports.addFile = function(pos) {
110111
*/
111112

112113
exports.applySourceMaps = function() {
113-
for (var file in this.files) {
114+
Object.keys(this.files).forEach(function(file) {
114115
var originalMap = sourceMapResolve.resolveSync(
115116
this.files[file], file, fs.readFileSync);
116117
if (originalMap) {
117118
originalMap = new SourceMapConsumer(originalMap.map);
118119
this.map.applySourceMap(originalMap, file);
119120
}
120-
}
121+
}, this);
121122
};
122123

123124
/**

test/cases/source-map-apply.scss

Whitespace-only changes.

test/css-stringify.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,28 @@ describe('stringify(obj, {sourcemap: true})', function(){
6969
map.originalPositionFor({ line: 1, column: 64 }).should.eql(locs.mediaOnly);
7070
map.sourceContentFor('rules.css').should.eql(src);
7171
});
72+
73+
it('should apply included source maps', function(){
74+
var file = 'test/source-map-apply.css';
75+
var src = read(file, 'utf8');
76+
var stylesheet = parse(src, { source: file, position: true });
77+
var result = stringify(stylesheet, { sourcemap: true });
78+
result.should.have.property('code');
79+
result.should.have.property('map');
80+
81+
var map = new SourceMapConsumer(result.map);
82+
map.originalPositionFor({ line: 1, column: 0 }).should.eql({
83+
column: 0,
84+
line: 1,
85+
name: null,
86+
source: 'source-map-apply.scss'
87+
});
88+
89+
map.originalPositionFor({ line: 2, column: 2 }).should.eql({
90+
column: 7,
91+
line: 1,
92+
name: null,
93+
source: 'source-map-apply.scss'
94+
});
95+
});
7296
});

test/source-map-apply.css

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/source-map-apply.css.map

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/source-map-apply.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tobi { name: 'tobi'; }

0 commit comments

Comments
 (0)