-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/sourcemaps' into dev
* feature/sourcemaps: added: simple test for sourcemaps updated: skip empty lines added: initial sourcemaps support #56
- Loading branch information
Showing
11 changed files
with
190 additions
and
56 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
var SourceMapGenerator = require('source-map').SourceMapGenerator | ||
var stringSimilarity = require('string-similarity') | ||
|
||
var LINES_RE = /[\r\n]/ | ||
var INDENTATION_RE = /^\s*/ | ||
|
||
/** | ||
* Find the source code using Levenshtein algorithm. | ||
* At moment riot doesn't use a real parser so that's the easiest solution to create sourcemaps | ||
* @param {string} sourceLine - original source code line | ||
* @param {array} outputLines - output lines | ||
* @returns {object} sourcemap position | ||
*/ | ||
function findSource(sourceLine, outputLines) { | ||
// figure out where this line could be located in the source code | ||
var bestMatch = stringSimilarity.findBestMatch(sourceLine, outputLines).bestMatch | ||
// find the index of the line matched | ||
var index = outputLines.indexOf(bestMatch.target) | ||
|
||
// figure out the column | ||
var column = bestMatch.target.indexOf(sourceLine) | ||
|
||
return { | ||
line: ++index, | ||
column: (column > -1 ? column : getIndentation(bestMatch.target)) + 1 | ||
} | ||
} | ||
|
||
/** | ||
* Return the indentation amount of a line containing code | ||
* @param {string} code - string containing code | ||
* @returns {number} amount of indentation | ||
*/ | ||
function getIndentation(code) { | ||
return code.match(INDENTATION_RE)[0].length | ||
} | ||
|
||
/** | ||
* Remove the indentation from the source code | ||
* @param {string} code - target code | ||
* @returns {string} code without indentation | ||
*/ | ||
function cleanLine(code) { | ||
return code.replace(INDENTATION_RE, '').trim() | ||
} | ||
|
||
/** | ||
* Generate the sourcemap | ||
* @param {object} opts - object containing the source/generated code and the file path | ||
* @returns {SourceMapGenerator} - SourceMapGenerator instance | ||
*/ | ||
module.exports = function generate(opts) { | ||
var map = new SourceMapGenerator({ | ||
file: opts.file | ||
}) | ||
|
||
var outputLines = opts.generated.split(LINES_RE).map(cleanLine).filter(l => l.length) | ||
|
||
opts.source.split(LINES_RE).forEach(function(line, index) { | ||
// skip empty lines | ||
if (!cleanLine(line).length) return | ||
map.addMapping({ | ||
source: opts.file, | ||
original:{ | ||
line: ++index, | ||
column: getIndentation(line) | ||
}, | ||
generated: findSource(line, outputLines) | ||
}) | ||
}) | ||
|
||
return map | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/*eslint-env mocha */ | ||
/*global compiler, expect */ | ||
/*eslint max-len: 0, no-console: 0 */ | ||
|
||
var fs = require('fs'), | ||
path = require('path') | ||
|
||
describe('Sourcemaps generation', function () { | ||
// in Win __dirname is a real path, in Lunux is symlink, path.relative uses symlink | ||
var | ||
basepath = path.resolve(__dirname, './'), | ||
fixtures = path.join(basepath, 'fixtures') | ||
|
||
|
||
function render (str, name, opts) { | ||
return compiler.compile(str, opts, name ? path.join(fixtures, name) : '') | ||
} | ||
|
||
function cat (dir, filename) { | ||
return fs.readFileSync(path.join(dir, filename), 'utf8') | ||
} | ||
|
||
function compile (name) { | ||
var src = cat(fixtures, name + '.tag') | ||
return render(src, name + '.tag', { sourcemap: true }) | ||
} | ||
|
||
it('It generates source and sourcemaps', function () { | ||
var out = compile('timetable') | ||
|
||
expect(out.code).to.be.ok | ||
expect(out.sourcemap).to.be.ok | ||
}) | ||
}) |