From 9523cbcd103a56d55ec06bd9606a15f48f2ae2d2 Mon Sep 17 00:00:00 2001 From: Jasper De Moor Date: Mon, 28 Sep 2020 17:52:11 +0200 Subject: [PATCH 1/7] init --- .vscode/settings.json | 69 ++++++++++++++++++++++++++++++++++++++++ src/MappingContainer.cpp | 8 +++++ src/MappingContainer.h | 6 +++- src/SourceMap.js | 22 +++++++++++++ src/napi/SourceMap.cpp | 30 ++++++++++++++++- src/napi/SourceMap.h | 4 +++ 6 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..12568969 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,69 @@ +{ + "files.associations": { + "__bit_reference": "cpp", + "__config": "cpp", + "__debug": "cpp", + "__errc": "cpp", + "__functional_base": "cpp", + "__hash_table": "cpp", + "__locale": "cpp", + "__mutex_base": "cpp", + "__node_handle": "cpp", + "__nullptr": "cpp", + "__split_buffer": "cpp", + "__string": "cpp", + "__threading_support": "cpp", + "__tree": "cpp", + "__tuple": "cpp", + "algorithm": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "chrono": "cpp", + "cmath": "cpp", + "complex": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "exception": "cpp", + "functional": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "ios": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "iterator": "cpp", + "limits": "cpp", + "locale": "cpp", + "memory": "cpp", + "mutex": "cpp", + "new": "cpp", + "optional": "cpp", + "ostream": "cpp", + "queue": "cpp", + "ratio": "cpp", + "set": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "unordered_map": "cpp", + "utility": "cpp", + "vector": "cpp" + } +} diff --git a/src/MappingContainer.cpp b/src/MappingContainer.cpp index 2c1859fb..f7d2af50 100644 --- a/src/MappingContainer.cpp +++ b/src/MappingContainer.cpp @@ -504,3 +504,11 @@ void MappingContainer::addIndexedMapping(int generatedLine, int generatedColumn, addMapping(generatedPosition, originalPosition, sourceIndex, nameIndex); } + +void MappingContainer::offsetLines(int line, int column, int lineOffset) { + // TODO: Write this function +} + +void MappingContainer::offsetColumns(int line, int column, int columnOffset) { + // TODO: Write this function +} diff --git a/src/MappingContainer.h b/src/MappingContainer.h index 1fe19657..8b78a1fe 100644 --- a/src/MappingContainer.h +++ b/src/MappingContainer.h @@ -52,7 +52,7 @@ class MappingContainer { Mapping findClosestMapping(int line, int column); - void addEmptyMap(std::string& sourceName, std::string &sourceContent, int lineOffset = 0); + void addEmptyMap(std::string &sourceName, std::string &sourceContent, int lineOffset = 0); flatbuffers::FlatBufferBuilder toBuffer(); @@ -60,6 +60,10 @@ class MappingContainer { void addBufferMappings(const void *buf, int lineOffset = 0, int columnOffset = 0); + void offsetLines(int line, int column, int lineOffset); + + void offsetColumns(int line, int column, int columnOffset); + void addIndexedMapping(int generatedLine, int generatedColumn, int originalLine, int originalColumn, std::string source, std::string name); private: diff --git a/src/SourceMap.js b/src/SourceMap.js index b0a20cb0..67434233 100644 --- a/src/SourceMap.js +++ b/src/SourceMap.js @@ -277,6 +277,28 @@ export default class SourceMap { throw new Error('SourceMap.findClosestMapping() must be implemented when extending SourceMap'); } + /** + * Offset mapping lines from a certain position + * + * @param line the line in the generated code (starts at 1) + * @param column the column in the generated code (starts at 0) + * @param lineOffset the amount of lines to offset mappings by + */ + offsetLines(line: number, column: number, lineOffset: number): ?IndexedMapping { + this.sourceMapInstance.offsetLines(line, column, lineOffset); + } + + /** + * Offset mapping columns from a certain position + * + * @param line the line in the generated code (starts at 1) + * @param column the column in the generated code (starts at 0) + * @param columnOffset the amount of columns to offset mappings by + */ + offsetColumns(line: number, column: number, columnOffset: number): ?IndexedMapping { + this.sourceMapInstance.offsetColumns(line, column, columnOffset); + } + /** * Returns a flatbuffer that represents this sourcemap, used for caching */ diff --git a/src/napi/SourceMap.cpp b/src/napi/SourceMap.cpp index 657820e1..e7d81e37 100644 --- a/src/napi/SourceMap.cpp +++ b/src/napi/SourceMap.cpp @@ -413,7 +413,7 @@ Napi::Value SourceMapBinding::findClosestMapping(const Napi::CallbackInfo &info) Napi::HandleScope scope(env); if (info.Length() < 2 || !info[0].IsNumber() || !info[1].IsNumber()) { - Napi::TypeError::New(env, "Expected 1 parameter of type buffer").ThrowAsJavaScriptException(); + Napi::TypeError::New(env, "Expected 2 parameters of type number").ThrowAsJavaScriptException(); return env.Null(); } @@ -422,6 +422,32 @@ Napi::Value SourceMapBinding::findClosestMapping(const Napi::CallbackInfo &info) return _mappingToObject(env, m); } +void SourceMapBinding::offsetLines(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + if (info.Length() < 3 || !info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) { + Napi::TypeError::New(env, "3 parameters of type number").ThrowAsJavaScriptException(); + return env.Null(); + } + + Mapping m = _mapping_container.offsetLines(info[0].As().Int32Value(), info[1].As().Int32Value(), info[2].As().Int32Value()); + return _mappingToObject(env, m); +} + +void SourceMapBinding::offsetColumns(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + if (info.Length() < 3 || !info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) { + Napi::TypeError::New(env, "3 parameters of type number").ThrowAsJavaScriptException(); + return env.Null(); + } + + Mapping m = _mapping_container.offsetColumns(info[0].As().Int32Value(), info[1].As().Int32Value(), info[2].As().Int32Value()); + return _mappingToObject(env, m); +} + Napi::Object SourceMapBinding::Init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); @@ -443,6 +469,8 @@ Napi::Object SourceMapBinding::Init(Napi::Env env, Napi::Object exports) { InstanceMethod("extends", &SourceMapBinding::extends), InstanceMethod("addEmptyMap", &SourceMapBinding::addEmptyMap), InstanceMethod("findClosestMapping", &SourceMapBinding::findClosestMapping), + InstanceMethod("offsetLines", &SourceMapBinding::offsetLines), + InstanceMethod("offsetColumns", &SourceMapBinding::offsetColumns), }); exports.Set("SourceMap", func); diff --git a/src/napi/SourceMap.h b/src/napi/SourceMap.h index baf52ed2..ab46e6aa 100644 --- a/src/napi/SourceMap.h +++ b/src/napi/SourceMap.h @@ -25,6 +25,10 @@ class SourceMapBinding : public Napi::ObjectWrap { void setSourceContent(const Napi::CallbackInfo &info); + void offsetLines(const Napi::CallbackInfo &info); + + void offsetColumns(const Napi::CallbackInfo &info); + std::vector _addSources(Napi::Array &sourcesArray, Napi::Array &sourcesContentArray); std::vector _addNames(Napi::Array &namesArray); From c50b4d5dbb23836396a8ebac8474d6b81bd2edf4 Mon Sep 17 00:00:00 2001 From: Jasper De Moor Date: Mon, 28 Sep 2020 20:44:20 +0200 Subject: [PATCH 2/7] implement offset column --- src/MappingContainer.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/MappingContainer.cpp b/src/MappingContainer.cpp index f7d2af50..e72647b3 100644 --- a/src/MappingContainer.cpp +++ b/src/MappingContainer.cpp @@ -510,5 +510,14 @@ void MappingContainer::offsetLines(int line, int column, int lineOffset) { } void MappingContainer::offsetColumns(int line, int column, int columnOffset) { - // TODO: Write this function + this->_mapping_lines[line].sort(); + + auto mappingsCount = this->_mapping_lines[line]._segments.size(); + for (int i = 0; i < mappingsCount; i++) { + if (this->_mapping_lines[line]._segments[i].generated.column < column) { + continue; + } + + this->_mapping_lines[line]._segments[i].generated.column += columnOffset; + } } From 09f24432225468e0359b68988b4438e34ed10e5a Mon Sep 17 00:00:00 2001 From: Jasper De Moor Date: Mon, 28 Sep 2020 20:55:10 +0200 Subject: [PATCH 3/7] offset lines --- src/MappingContainer.cpp | 23 +++++++++++++++++++++-- src/MappingLine.h | 2 ++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/MappingContainer.cpp b/src/MappingContainer.cpp index e72647b3..d934417e 100644 --- a/src/MappingContainer.cpp +++ b/src/MappingContainer.cpp @@ -506,14 +506,33 @@ void MappingContainer::addIndexedMapping(int generatedLine, int generatedColumn, } void MappingContainer::offsetLines(int line, int column, int lineOffset) { - // TODO: Write this function + // TODO: Finetune this function as it's pretty complex with our current data structure + // If lineOffset is less than 0, remove lines... + // If lineOffset is more than 0, insert extra lines... + // If column is not 0 and matches any mappings, than it should split up into 2 line instances... + + auto linesCount = this->_mapping_lines.size(); + for (int lineIndex = line; lineIndex < linesCount; ++lineIndex) { + auto mappingsCount = this->_mapping_lines[lineIndex]._segments.size(); + this->_mapping_lines[lineIndex].setLineNumber(lineIndex + lineOffset); + for (int mappingIndex = 0; mappingIndex < mappingsCount; ++mappingsCount) { + if (lineIndex == line && this->_mapping_lines[lineIndex]._segments[mappingIndex].generated.column < column) { + continue; + } + + // TODO: Optimise code to exclude lineIndex in Generated Position? As the MappingLine already contains this data? + this->_mapping_lines[lineIndex]._segments[mappingIndex].generated.line += lineOffset; + } + } + + // TODO: Resort lines and fill any created voids in the lines vector } void MappingContainer::offsetColumns(int line, int column, int columnOffset) { this->_mapping_lines[line].sort(); auto mappingsCount = this->_mapping_lines[line]._segments.size(); - for (int i = 0; i < mappingsCount; i++) { + for (int i = 0; i < mappingsCount; ++i) { if (this->_mapping_lines[line]._segments[i].generated.column < column) { continue; } diff --git a/src/MappingLine.h b/src/MappingLine.h index b248c428..c4f94f35 100644 --- a/src/MappingLine.h +++ b/src/MappingLine.h @@ -7,6 +7,8 @@ class MappingLine { void addMapping(Mapping m); + void setLineNumber(int newLineNumber); + void sort(); bool isSorted(); From 3f24bdf4f26b076b0629b80bc3e2e834c9008fbd Mon Sep 17 00:00:00 2001 From: Jasper De Moor Date: Tue, 29 Sep 2020 10:26:21 +0200 Subject: [PATCH 4/7] column offset tests --- src/SourceMap.js | 12 +++++- src/napi/SourceMap.cpp | 10 ++--- test/offset-utils.test.js | 89 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 test/offset-utils.test.js diff --git a/src/SourceMap.js b/src/SourceMap.js index 67434233..47233eed 100644 --- a/src/SourceMap.js +++ b/src/SourceMap.js @@ -285,7 +285,11 @@ export default class SourceMap { * @param lineOffset the amount of lines to offset mappings by */ offsetLines(line: number, column: number, lineOffset: number): ?IndexedMapping { - this.sourceMapInstance.offsetLines(line, column, lineOffset); + if (line < 1 || column < 0 || line + lineOffset < 1) { + throw new Error('Line and Column has to be positive'); + } + + this.sourceMapInstance.offsetLines(line - 1, column, lineOffset); } /** @@ -296,7 +300,11 @@ export default class SourceMap { * @param columnOffset the amount of columns to offset mappings by */ offsetColumns(line: number, column: number, columnOffset: number): ?IndexedMapping { - this.sourceMapInstance.offsetColumns(line, column, columnOffset); + if (line < 1 || column < 0 || column + columnOffset < 0) { + throw new Error('Line and Column has to be positive'); + } + + this.sourceMapInstance.offsetColumns(line - 1, column, columnOffset); } /** diff --git a/src/napi/SourceMap.cpp b/src/napi/SourceMap.cpp index e7d81e37..65cb785a 100644 --- a/src/napi/SourceMap.cpp +++ b/src/napi/SourceMap.cpp @@ -428,11 +428,10 @@ void SourceMapBinding::offsetLines(const Napi::CallbackInfo &info) { if (info.Length() < 3 || !info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) { Napi::TypeError::New(env, "3 parameters of type number").ThrowAsJavaScriptException(); - return env.Null(); + return; } - Mapping m = _mapping_container.offsetLines(info[0].As().Int32Value(), info[1].As().Int32Value(), info[2].As().Int32Value()); - return _mappingToObject(env, m); + _mapping_container.offsetLines(info[0].As().Int32Value(), info[1].As().Int32Value(), info[2].As().Int32Value()); } void SourceMapBinding::offsetColumns(const Napi::CallbackInfo &info) { @@ -441,11 +440,10 @@ void SourceMapBinding::offsetColumns(const Napi::CallbackInfo &info) { if (info.Length() < 3 || !info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) { Napi::TypeError::New(env, "3 parameters of type number").ThrowAsJavaScriptException(); - return env.Null(); + return; } - Mapping m = _mapping_container.offsetColumns(info[0].As().Int32Value(), info[1].As().Int32Value(), info[2].As().Int32Value()); - return _mappingToObject(env, m); + _mapping_container.offsetColumns(info[0].As().Int32Value(), info[1].As().Int32Value(), info[2].As().Int32Value()); } Napi::Object SourceMapBinding::Init(Napi::Env env, Napi::Object exports) { diff --git a/test/offset-utils.test.js b/test/offset-utils.test.js new file mode 100644 index 00000000..ffde95fb --- /dev/null +++ b/test/offset-utils.test.js @@ -0,0 +1,89 @@ +import assert from 'assert'; +import SourceMap from '.'; + +const SIMPLE_SOURCE_MAP = { + version: 3, + file: 'helloworld.js', + sources: ['helloworld.coffee'], + names: [], + mappings: 'AAAA;AAAA,EAAA,OAAO,CAAC,GAAR,CAAY,aAAZ,CAAA,CAAA;AAAA', +}; + +describe('SourceMap - Offset Utils', () => { + it('Should be able to offset columns', () => { + let map = new SourceMap('/test-root'); + + map.addRawMappings({ + mappings: SIMPLE_SOURCE_MAP.mappings, + sources: SIMPLE_SOURCE_MAP.sources, + names: SIMPLE_SOURCE_MAP.names, + }); + + map.offsetColumns(1, 0, 2); + + map.offsetColumns(2, 15, -4); + + assert.deepEqual(map.getMap(), { + sources: ['./helloworld.coffee'], + sourcesContent: [''], + names: [], + mappings: [ + { + generated: { line: 1, column: 2 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 2, column: 0 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 2, column: 2 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 2, column: 9 }, + original: { line: 1, column: 7 }, + source: 0, + }, + { + generated: { line: 2, column: 10 }, + original: { line: 1, column: 8 }, + source: 0, + }, + { + generated: { line: 2, column: 13 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 2, column: 14 }, + original: { line: 1, column: 12 }, + source: 0, + }, + { + generated: { line: 2, column: 23 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 2, column: 24 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 2, column: 25 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 3, column: 0 }, + original: { line: 1, column: 0 }, + source: 0, + }, + ], + }); + }); +}); From 6320a84e7b3a2a27eaa390730df16418bc94ede9 Mon Sep 17 00:00:00 2001 From: Jasper De Moor Date: Tue, 29 Sep 2020 13:58:44 +0200 Subject: [PATCH 5/7] line and column offset tests --- src/MappingContainer.cpp | 44 ++++++----- src/MappingContainer.h | 2 +- src/MappingLine.cpp | 4 + src/MappingLine.h | 2 +- src/SourceMap.js | 17 +++-- src/napi/SourceMap.cpp | 6 +- test/offset-utils.test.js | 150 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 196 insertions(+), 29 deletions(-) diff --git a/src/MappingContainer.cpp b/src/MappingContainer.cpp index d934417e..f7cb47a6 100644 --- a/src/MappingContainer.cpp +++ b/src/MappingContainer.cpp @@ -505,32 +505,38 @@ void MappingContainer::addIndexedMapping(int generatedLine, int generatedColumn, addMapping(generatedPosition, originalPosition, sourceIndex, nameIndex); } -void MappingContainer::offsetLines(int line, int column, int lineOffset) { - // TODO: Finetune this function as it's pretty complex with our current data structure - // If lineOffset is less than 0, remove lines... - // If lineOffset is more than 0, insert extra lines... - // If column is not 0 and matches any mappings, than it should split up into 2 line instances... - - auto linesCount = this->_mapping_lines.size(); - for (int lineIndex = line; lineIndex < linesCount; ++lineIndex) { - auto mappingsCount = this->_mapping_lines[lineIndex]._segments.size(); - this->_mapping_lines[lineIndex].setLineNumber(lineIndex + lineOffset); - for (int mappingIndex = 0; mappingIndex < mappingsCount; ++mappingsCount) { - if (lineIndex == line && this->_mapping_lines[lineIndex]._segments[mappingIndex].generated.column < column) { - continue; +// TODO: This can be improved performance wise, by not having line stored in both MappingLine and Mapping +void MappingContainer::offsetLines(int line, int lineOffset) { + int lineCount = this->_mapping_lines.size(); + + if (lineOffset > 0) { + this->createLinesIfUndefined(lineCount + lineOffset); + + for (int lineIndex = lineCount - 1; lineIndex >= line; --lineIndex) { + std::vector mappings = this->_mapping_lines[lineIndex]._segments; + for (int mappingIndex = 0; mappingIndex < mappings.size(); ++mappingIndex) { + Mapping &mapping = mappings[mappingIndex]; + Position generatedPosition = Position{mapping.generated.line + lineOffset, mapping.generated.column}; + _mapping_lines[generatedPosition.line].addMapping(Mapping{generatedPosition, mapping.original, mapping.source, mapping.name}); } - // TODO: Optimise code to exclude lineIndex in Generated Position? As the MappingLine already contains this data? - this->_mapping_lines[lineIndex]._segments[mappingIndex].generated.line += lineOffset; + this->_mapping_lines[lineIndex].clearMappings(); } - } + } else { + for (int lineIndex = line; lineIndex < lineCount; ++lineIndex) { + std::vector mappings = this->_mapping_lines[lineIndex]._segments; + for (int mappingIndex = 0; mappingIndex < mappings.size(); ++mappingIndex) { + Mapping &mapping = mappings[mappingIndex]; + Position generatedPosition = Position{mapping.generated.line + lineOffset, mapping.generated.column}; + _mapping_lines[generatedPosition.line].addMapping(Mapping{generatedPosition, mapping.original, mapping.source, mapping.name}); + } - // TODO: Resort lines and fill any created voids in the lines vector + this->_mapping_lines[lineIndex].clearMappings(); + } + } } void MappingContainer::offsetColumns(int line, int column, int columnOffset) { - this->_mapping_lines[line].sort(); - auto mappingsCount = this->_mapping_lines[line]._segments.size(); for (int i = 0; i < mappingsCount; ++i) { if (this->_mapping_lines[line]._segments[i].generated.column < column) { diff --git a/src/MappingContainer.h b/src/MappingContainer.h index 8b78a1fe..f5b565b0 100644 --- a/src/MappingContainer.h +++ b/src/MappingContainer.h @@ -60,7 +60,7 @@ class MappingContainer { void addBufferMappings(const void *buf, int lineOffset = 0, int columnOffset = 0); - void offsetLines(int line, int column, int lineOffset); + void offsetLines(int line, int lineOffset); void offsetColumns(int line, int column, int columnOffset); diff --git a/src/MappingLine.cpp b/src/MappingLine.cpp index a80970af..b1a83b14 100644 --- a/src/MappingLine.cpp +++ b/src/MappingLine.cpp @@ -34,3 +34,7 @@ int MappingLine::lineNumber() { return _line_number; } +void MappingLine::clearMappings() { + this->_segments.clear(); + this->_last_column = 0; +} diff --git a/src/MappingLine.h b/src/MappingLine.h index c4f94f35..8510c568 100644 --- a/src/MappingLine.h +++ b/src/MappingLine.h @@ -7,7 +7,7 @@ class MappingLine { void addMapping(Mapping m); - void setLineNumber(int newLineNumber); + void clearMappings(); void sort(); diff --git a/src/SourceMap.js b/src/SourceMap.js index 47233eed..a349c790 100644 --- a/src/SourceMap.js +++ b/src/SourceMap.js @@ -281,15 +281,18 @@ export default class SourceMap { * Offset mapping lines from a certain position * * @param line the line in the generated code (starts at 1) - * @param column the column in the generated code (starts at 0) * @param lineOffset the amount of lines to offset mappings by */ - offsetLines(line: number, column: number, lineOffset: number): ?IndexedMapping { - if (line < 1 || column < 0 || line + lineOffset < 1) { - throw new Error('Line and Column has to be positive'); + offsetLines(line: number, lineOffset: number): ?IndexedMapping { + if (line < 1 || line + lineOffset < 1) { + throw new Error('Line has to be positive'); + } + + if (lineOffset === 0) { + return; } - this.sourceMapInstance.offsetLines(line - 1, column, lineOffset); + this.sourceMapInstance.offsetLines(line - 1, lineOffset); } /** @@ -304,6 +307,10 @@ export default class SourceMap { throw new Error('Line and Column has to be positive'); } + if (columnOffset === 0) { + return; + } + this.sourceMapInstance.offsetColumns(line - 1, column, columnOffset); } diff --git a/src/napi/SourceMap.cpp b/src/napi/SourceMap.cpp index 65cb785a..08206799 100644 --- a/src/napi/SourceMap.cpp +++ b/src/napi/SourceMap.cpp @@ -426,12 +426,12 @@ void SourceMapBinding::offsetLines(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); - if (info.Length() < 3 || !info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) { - Napi::TypeError::New(env, "3 parameters of type number").ThrowAsJavaScriptException(); + if (info.Length() < 2 || !info[0].IsNumber() || !info[1].IsNumber()) { + Napi::TypeError::New(env, "2 parameters of type number").ThrowAsJavaScriptException(); return; } - _mapping_container.offsetLines(info[0].As().Int32Value(), info[1].As().Int32Value(), info[2].As().Int32Value()); + _mapping_container.offsetLines(info[0].As().Int32Value(), info[1].As().Int32Value()); } void SourceMapBinding::offsetColumns(const Napi::CallbackInfo &info) { diff --git a/test/offset-utils.test.js b/test/offset-utils.test.js index ffde95fb..0bdb99a9 100644 --- a/test/offset-utils.test.js +++ b/test/offset-utils.test.js @@ -86,4 +86,154 @@ describe('SourceMap - Offset Utils', () => { ], }); }); + + it('Positive line offset', () => { + let map = new SourceMap('/test-root'); + + map.addRawMappings({ + mappings: SIMPLE_SOURCE_MAP.mappings, + sources: SIMPLE_SOURCE_MAP.sources, + names: SIMPLE_SOURCE_MAP.names, + }); + + map.offsetLines(1, 2); + + assert.deepEqual(map.getMap(), { + sources: ['./helloworld.coffee'], + sourcesContent: [''], + names: [], + mappings: [ + { + generated: { line: 3, column: 0 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 4, column: 0 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 4, column: 2 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 4, column: 9 }, + original: { line: 1, column: 7 }, + source: 0, + }, + { + generated: { line: 4, column: 10 }, + original: { line: 1, column: 8 }, + source: 0, + }, + { + generated: { line: 4, column: 13 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 4, column: 14 }, + original: { line: 1, column: 12 }, + source: 0, + }, + { + generated: { line: 4, column: 27 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 4, column: 28 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 4, column: 29 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 5, column: 0 }, + original: { line: 1, column: 0 }, + source: 0, + }, + ], + }); + }); + + it('Negative line offset', () => { + let map = new SourceMap('/test-root'); + + map.addRawMappings({ + mappings: SIMPLE_SOURCE_MAP.mappings, + sources: SIMPLE_SOURCE_MAP.sources, + names: SIMPLE_SOURCE_MAP.names, + }); + + map.offsetLines(2, -1); + + assert.deepEqual(map.getMap(), { + sources: ['./helloworld.coffee'], + sourcesContent: [''], + names: [], + mappings: [ + { + generated: { line: 1, column: 0 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 1, column: 0 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 1, column: 2 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 1, column: 9 }, + original: { line: 1, column: 7 }, + source: 0, + }, + { + generated: { line: 1, column: 10 }, + original: { line: 1, column: 8 }, + source: 0, + }, + { + generated: { line: 1, column: 13 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 1, column: 14 }, + original: { line: 1, column: 12 }, + source: 0, + }, + { + generated: { line: 1, column: 27 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 1, column: 28 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 1, column: 29 }, + original: { line: 1, column: 0 }, + source: 0, + }, + { + generated: { line: 2, column: 0 }, + original: { line: 1, column: 0 }, + source: 0, + }, + ], + }); + }); }); From c43bf6367cc16c108ce81392911119e79427bce0 Mon Sep 17 00:00:00 2001 From: Jasper De Moor Date: Tue, 29 Sep 2020 14:31:44 +0200 Subject: [PATCH 6/7] wasm --- src/wasm/SourceMap.cpp | 10 ++++++++++ src/wasm/SourceMap.h | 3 +++ 2 files changed, 13 insertions(+) diff --git a/src/wasm/SourceMap.cpp b/src/wasm/SourceMap.cpp index 865ae8b1..c0dcf336 100644 --- a/src/wasm/SourceMap.cpp +++ b/src/wasm/SourceMap.cpp @@ -129,6 +129,14 @@ Mapping SourceMap::findClosestMapping(int line, int column) { return _mapping_container.findClosestMapping(line - 1, column); } +void SourceMap::offsetLines(int line, int lineOffset) { + _mapping_container.offsetLines(line, lineOffset); +} + +void SourceMap::offsetColumns(int line, int column, int columnOffset) { + _mapping_container.offsetColumns(line, column, columnOffset); +} + EMSCRIPTEN_BINDINGS(my_class_example) { emscripten::register_vector("VectorString"); emscripten::register_vector("VectorMapping"); @@ -157,6 +165,8 @@ EMSCRIPTEN_BINDINGS(my_class_example) { .function("addEmptyMap", &SourceMap::addEmptyMap) .function("extends", &SourceMap::extends) .function("findClosestMapping", &SourceMap::findClosestMapping) + .function("offsetLines", &SourceMap::offsetLines) + .function("offsetColumns", &SourceMap::offsetColumns) ; emscripten::value_object("Mapping") diff --git a/src/wasm/SourceMap.h b/src/wasm/SourceMap.h index 80cd416b..26370f97 100644 --- a/src/wasm/SourceMap.h +++ b/src/wasm/SourceMap.h @@ -34,6 +34,9 @@ class SourceMap { Mapping findClosestMapping(int line, int column); + void offsetLines(int line, int lineOffset); + void offsetColumns(int line, int column, int columnOffset); + private: MappingContainer _mapping_container; }; From d5241464feb31fb4fe5c0ae3a4804eac0424c105 Mon Sep 17 00:00:00 2001 From: Jasper De Moor Date: Tue, 29 Sep 2020 14:56:19 +0200 Subject: [PATCH 7/7] remove settings --- .gitignore | 1 + .vscode/settings.json | 69 ------------------------------------------- 2 files changed, 1 insertion(+), 69 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index e6015378..eebb9a66 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ prebuilds wasm-node wasm-browser yarn.lock +.vscode/settings diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 12568969..00000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,69 +0,0 @@ -{ - "files.associations": { - "__bit_reference": "cpp", - "__config": "cpp", - "__debug": "cpp", - "__errc": "cpp", - "__functional_base": "cpp", - "__hash_table": "cpp", - "__locale": "cpp", - "__mutex_base": "cpp", - "__node_handle": "cpp", - "__nullptr": "cpp", - "__split_buffer": "cpp", - "__string": "cpp", - "__threading_support": "cpp", - "__tree": "cpp", - "__tuple": "cpp", - "algorithm": "cpp", - "array": "cpp", - "atomic": "cpp", - "bit": "cpp", - "bitset": "cpp", - "cctype": "cpp", - "chrono": "cpp", - "cmath": "cpp", - "complex": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "exception": "cpp", - "functional": "cpp", - "initializer_list": "cpp", - "iomanip": "cpp", - "ios": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "iterator": "cpp", - "limits": "cpp", - "locale": "cpp", - "memory": "cpp", - "mutex": "cpp", - "new": "cpp", - "optional": "cpp", - "ostream": "cpp", - "queue": "cpp", - "ratio": "cpp", - "set": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "string": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "typeinfo": "cpp", - "unordered_map": "cpp", - "utility": "cpp", - "vector": "cpp" - } -}