{"size": 4327, "ext": "js", "max_stars_repo_path": "test/stringify.js", "max_stars_repo_name": "yogesh-1952/css-2", "max_stars_repo_stars_event_min_datetime": "2018-12-25T08:52:12.000Z", "max_stars_repo_stars_event_max_datetime": "2018-12-25T08:52:12.000Z", "max_issues_repo_path": "test/stringify.js", "max_issues_repo_name": "yogesh-1952/css-2", "max_issues_repo_issues_event_min_datetime": "2021-03-09T13:41:48.000Z", "max_issues_repo_issues_event_max_datetime": "2021-03-09T13:41:48.000Z", "max_forks_repo_path": "test/stringify.js", "max_forks_repo_name": "yogesh-1952/css-2", "max_forks_repo_forks_event_min_datetime": "2020-02-19T16:51:54.000Z", "max_forks_repo_forks_event_max_datetime": "2021-02-20T10:40:24.000Z"}
{"max_stars_count": 1, "max_issues_count": 1, "max_forks_count": 2, "avg_line_length": 38.2920353982, "max_line_length": 87, "alphanum_fraction": 0.6563438872}
var stringify = require('../').stringify;
var parse = require('../').parse;
var path = require('path');
var read = require('fs').readFileSync;
var SourceMapConsumer = require('source-map').SourceMapConsumer;
var SourceMapGenerator = require('source-map').SourceMapGenerator;

describe('stringify(obj, {sourcemap: true})', function() {
  var file = 'test/source-map/test.css';
  var src = read(file, 'utf8');
  var stylesheet = parse(src, { source: file });
  function loc(line, column) {
    return { line: line, column: column, source: file, name: null };
  }

  var locs = {
    tobiSelector: loc(1, 0),
    tobiNameName: loc(2, 2),
    tobiNameValue: loc(2, 2),
    mediaBlock: loc(11, 0),
    mediaOnly: loc(12, 2),
    comment: loc(17, 0),
  };

  it('should generate source maps alongside when using identity compiler', function() {
    var result = stringify(stylesheet, { sourcemap: true });
    result.should.have.property('code');
    result.should.have.property('map');
    var map = new SourceMapConsumer(result.map);
    map.originalPositionFor({ line: 1, column: 0 }).should.eql(locs.tobiSelector);
    map.originalPositionFor({ line: 2, column: 2 }).should.eql(locs.tobiNameName);
    map.originalPositionFor({ line: 2, column: 8 }).should.eql(locs.tobiNameValue);
    map.originalPositionFor({ line: 11, column: 0 }).should.eql(locs.mediaBlock);
    map.originalPositionFor({ line: 12, column: 2 }).should.eql(locs.mediaOnly);
    map.originalPositionFor({ line: 17, column: 0 }).should.eql(locs.comment);
    map.sourceContentFor(file).should.eql(src);
  });

  it('should generate source maps alongside when using compress compiler', function() {
    var result = stringify(stylesheet, { compress: true, sourcemap: true });
    result.should.have.property('code');
    result.should.have.property('map');
    var map = new SourceMapConsumer(result.map);
    map.originalPositionFor({ line: 1, column: 0 }).should.eql(locs.tobiSelector);
    map.originalPositionFor({ line: 1, column: 5 }).should.eql(locs.tobiNameName);
    map.originalPositionFor({ line: 1, column: 10 }).should.eql(locs.tobiNameValue);
    map.originalPositionFor({ line: 1, column: 50 }).should.eql(locs.mediaBlock);
    map.originalPositionFor({ line: 1, column: 64 }).should.eql(locs.mediaOnly);
    map.sourceContentFor(file).should.eql(src);
  });

  it('should apply included source maps, with paths adjusted to CWD', function() {
    var file = 'test/source-map/apply.css';
    var src = read(file, 'utf8');
    var stylesheet = parse(src, { source: file });
    var result = stringify(stylesheet, { sourcemap: true });
    result.should.have.property('code');
    result.should.have.property('map');

    var map = new SourceMapConsumer(result.map);
    map.originalPositionFor({ line: 1, column: 0 }).should.eql({
      column: 0,
      line: 1,
      name: null,
      source: 'test/source-map/apply.scss'
    });

    map.originalPositionFor({ line: 2, column: 2 }).should.eql({
      column: 7,
      line: 1,
      name: null,
      source: 'test/source-map/apply.scss'
    });
  });

  it('should not apply included source maps when inputSourcemap is false', function() {
    var file = 'test/source-map/apply.css';
    var src = read(file, 'utf8');
    var stylesheet = parse(src, { source: file });
    var result = stringify(stylesheet, { sourcemap: true, inputSourcemaps: false });

    var map = new SourceMapConsumer(result.map);
    map.originalPositionFor({ line: 1, column: 0 }).should.eql({
      column: 0,
      line: 1,
      name: null,
      source: file
    });
  });

  it('should convert Windows-style paths to URLs', function() {
    var originalSep = path.sep;
    path.sep = '\\'; // Pretend we’re on Windows (if we aren’t already).

    var src = 'C:\\test\\source.css';
    var css = 'a { color: black; }'
    var stylesheet = parse(css, { source: src });
    var result = stringify(stylesheet, { sourcemap: true });

    result.map.sources.should.eql(['/test/source.css']);

    path.sep = originalSep;
  });

  it('should return source map generator when sourcemap: "generator"', function(){
    var css = 'a { color: black; }';
    var stylesheet = parse(css);
    var result = stringify(stylesheet, { sourcemap: 'generator' });

    result.map.should.be.an.instanceOf(SourceMapGenerator);
  });
});
