Skip to content

Commit

Permalink
Merge pull request #34 from parcel-bundler/offset-utils
Browse files Browse the repository at this point in the history
Offset utilities
  • Loading branch information
DeMoorJasper authored Sep 30, 2020
2 parents 5f29c9f + d524146 commit 0154b1e
Show file tree
Hide file tree
Showing 11 changed files with 374 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ prebuilds
wasm-node
wasm-browser
yarn.lock
.vscode/settings
42 changes: 42 additions & 0 deletions src/MappingContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,45 @@ void MappingContainer::addIndexedMapping(int generatedLine, int generatedColumn,

addMapping(generatedPosition, originalPosition, sourceIndex, nameIndex);
}

// 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<Mapping> 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});
}

this->_mapping_lines[lineIndex].clearMappings();
}
} else {
for (int lineIndex = line; lineIndex < lineCount; ++lineIndex) {
std::vector<Mapping> 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});
}

this->_mapping_lines[lineIndex].clearMappings();
}
}
}

void MappingContainer::offsetColumns(int line, int column, int columnOffset) {
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;
}
}
6 changes: 5 additions & 1 deletion src/MappingContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,18 @@ 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();

void extends(const void *buf);

void addBufferMappings(const void *buf, int lineOffset = 0, int columnOffset = 0);

void offsetLines(int line, 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:
Expand Down
4 changes: 4 additions & 0 deletions src/MappingLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ int MappingLine::lineNumber() {
return _line_number;
}

void MappingLine::clearMappings() {
this->_segments.clear();
this->_last_column = 0;
}
2 changes: 2 additions & 0 deletions src/MappingLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class MappingLine {

void addMapping(Mapping m);

void clearMappings();

void sort();

bool isSorted();
Expand Down
37 changes: 37 additions & 0 deletions src/SourceMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,43 @@ 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 lineOffset the amount of lines to offset mappings by
*/
offsetLines(line: number, lineOffset: number): ?IndexedMapping<string> {
if (line < 1 || line + lineOffset < 1) {
throw new Error('Line has to be positive');
}

if (lineOffset === 0) {
return;
}

this.sourceMapInstance.offsetLines(line - 1, 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<string> {
if (line < 1 || column < 0 || column + columnOffset < 0) {
throw new Error('Line and Column has to be positive');
}

if (columnOffset === 0) {
return;
}

this.sourceMapInstance.offsetColumns(line - 1, column, columnOffset);
}

/**
* Returns a flatbuffer that represents this sourcemap, used for caching
*/
Expand Down
28 changes: 27 additions & 1 deletion src/napi/SourceMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -422,6 +422,30 @@ 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() < 2 || !info[0].IsNumber() || !info[1].IsNumber()) {
Napi::TypeError::New(env, "2 parameters of type number").ThrowAsJavaScriptException();
return;
}

_mapping_container.offsetLines(info[0].As<Napi::Number>().Int32Value(), info[1].As<Napi::Number>().Int32Value());
}

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;
}

_mapping_container.offsetColumns(info[0].As<Napi::Number>().Int32Value(), info[1].As<Napi::Number>().Int32Value(), info[2].As<Napi::Number>().Int32Value());
}

Napi::Object SourceMapBinding::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);

Expand All @@ -443,6 +467,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);
Expand Down
4 changes: 4 additions & 0 deletions src/napi/SourceMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class SourceMapBinding : public Napi::ObjectWrap<SourceMapBinding> {

void setSourceContent(const Napi::CallbackInfo &info);

void offsetLines(const Napi::CallbackInfo &info);

void offsetColumns(const Napi::CallbackInfo &info);

std::vector<int> _addSources(Napi::Array &sourcesArray, Napi::Array &sourcesContentArray);

std::vector<int> _addNames(Napi::Array &namesArray);
Expand Down
10 changes: 10 additions & 0 deletions src/wasm/SourceMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>("VectorString");
emscripten::register_vector<Mapping>("VectorMapping");
Expand Down Expand Up @@ -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>("Mapping")
Expand Down
3 changes: 3 additions & 0 deletions src/wasm/SourceMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Loading

0 comments on commit 0154b1e

Please sign in to comment.