Skip to content

Commit

Permalink
feat: support sourcemap, #12
Browse files Browse the repository at this point in the history
  • Loading branch information
Javey committed Aug 15, 2018
1 parent 2e49cc8 commit f63059a
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 164 deletions.
126 changes: 74 additions & 52 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ var Options = {
return data[key];
},
disableSplitText: false, // split text with <!---->
sourceMap: true
sourceMap: false,
indent: ' ' // code indent style
};

var hasOwn = Object.prototype.hasOwnProperty;
Expand Down Expand Up @@ -570,7 +571,7 @@ Parser.prototype = {
this.source = trimRight(source);
this.index = 0;
this.line = 1;
this.column = 1;
this.column = 0;
this.length = this.source.length;

this.options = extend({}, configure(), options);
Expand Down Expand Up @@ -649,6 +650,9 @@ Parser.prototype = {
this._updateLine();
} else {
this._updateIndex();
if (this._char() === '\n') {
this._updateLine();
}
}
this._updateIndex();
break;
Expand Down Expand Up @@ -1211,7 +1215,10 @@ Parser.prototype = {

_updateLine: function _updateLine() {
this.line++;
this.column = 0;
// because we call _updateLine firstly then call _updateIndex
// it will add column in _updateIndex
// set it to -1 here
this.column = -1;
},

_updateIndex: function _updateIndex(value) {
Expand All @@ -1229,6 +1236,7 @@ Parser.prototype = {
line = _ref.line,
column = _ref.column;

column++;
var error$$1 = new Error(msg + ' (' + line + ':' + column + ')\n' + ('> ' + line + ' | ' + lines[line - 1] + '\n') + (' ' + new Array(String(line).length + 1).join(' ') + ' | ' + new Array(column).join(' ') + '^'));
error$$1.line = line;
error$$1.column = column;
Expand Down Expand Up @@ -1294,10 +1302,11 @@ Stringifier.prototype = {
this.indent = 0;

this.buffer = [];
this.queue = [];
this.mappings = [];

this.line = 1;
this.column = 1;
this.column = 0;
},
_start: function _start(ast) {
var _this = this;
Expand Down Expand Up @@ -1427,60 +1436,46 @@ Stringifier.prototype = {
__visitJSXElement: function __visitJSXElement(element) {
this._append('h(\'' + element.value + '\'', element);

// a flag to remove redundant params for
// converting h('div', null, 'a', null, null) to h('div', null, 'a')
var start = this.buffer.length;
this._append(', ');
this._appendQueue(', ');

var _visitJSXAttribute = this._visitJSXAttribute(element, true, true),
attributes = _visitJSXAttribute.attributes,
hasProps = _visitJSXAttribute.hasProps;
var _visitJSXAttribute = this._visitJSXAttribute(element, true, true, true /* appendQueue */),
attributes = _visitJSXAttribute.attributes;

if (hasProps) {
start = this.buffer.length;
}
this._appendQueue(', ');
this._visitJSXChildren(element.children, true /* appendQueue */);

this._append(', ');
this._visitJSXChildren(element.children);
if (element.children.length) {
start = this.buffer.length;
}

this._append(', ');
this._appendQueue(', ');
if (attributes.className) {
this._visitJSXAttributeClassName(attributes.className);
start = this.buffer.length;
} else {
this._append('null');
this._appendQueue('null');
}

this._append(', ');
this._appendQueue(', ');
if (attributes.key) {
this._visitJSXAttributeValue(attributes.key);
start = this.buffer.length;
} else {
this._append('null');
this._appendQueue('null');
}

this._append(', ');
this._appendQueue(', ');
if (attributes.ref) {
this._visitJSXAttributeRef(attributes.ref);
start = this.buffer.length;
} else {
this._append('null');
}

if (start !== this.buffer.length) {
this.buffer.splice(start, this.buffer.length - start);
}
this._clearQueue();
this._append(')');
},


_visitJSXChildren: function _visitJSXChildren(children) {
_visitJSXChildren: function _visitJSXChildren(children, appendQueue) {
var length = children.length;
if (!length) {
this._append('null');
if (appendQueue) {
this._appendQueue('null');
} else {
this._append('null');
}
}
if (length > 1) {
this._append('[\n');
Expand Down Expand Up @@ -1542,7 +1537,8 @@ Stringifier.prototype = {

_visitJSXDirectiveIf: function _visitJSXDirectiveIf(directive, element, body) {
var hasElse = false,
next = element;
next = element,
indent = this.indent;

this._visitJSXAttributeValue(directive.value);
this._append(' ?\n');
Expand All @@ -1566,15 +1562,15 @@ Stringifier.prototype = {
}
if (nextDirectives['v-else']) {
this._visit(next);
this._dedent();
hasElse = true;
}

break;
}

if (!hasElse) this._append('undefined');
this._dedent();

this.indent = indent;
},

_visitJSXDirectiveFor: function _visitJSXDirectiveFor(directive, element, body) {
Expand Down Expand Up @@ -1613,7 +1609,7 @@ Stringifier.prototype = {
this.enterStringExpression = false;
},

_visitJSXAttribute: function _visitJSXAttribute(element, individualClassName, individualKeyAndRef) {
_visitJSXAttribute: function _visitJSXAttribute(element, individualClassName, individualKeyAndRef, appendQueue) {
var _this4 = this;

var set = {},
Expand Down Expand Up @@ -1742,7 +1738,11 @@ Stringifier.prototype = {
this._dedent();
this._append('}');
} else {
this._append('null');
if (appendQueue) {
this._appendQueue('null');
} else {
this._append('null');
}
}

return { attributes: set, hasProps: isFirst !== undefined };
Expand Down Expand Up @@ -2062,36 +2062,58 @@ Stringifier.prototype = {
line: this.line,
column: this.column
},
original: {
original: element && element.line !== undefined ? {
line: element.line,
column: element.column
}
} : undefined
});
},
_append: function _append(code, element) {
if (element) {
var buffer = this.buffer;
var _options = this.options,
sourceMap = _options.sourceMap,
indent = _options.indent;


this._flushQueue();
if (sourceMap) {
this._addMapping(element);
}
var buffer = this.buffer;

// add indent if the last line ends with \n
if (this.indent && this.last && this.last[this.last.length - 1] === '\n' && code[0] !== '\n') {
buffer.push(new Array(this.indent + 1).join(' '));
if (indent && this.indent && this.last && this.last[this.last.length - 1] === '\n' && code[0] !== '\n') {
buffer.push(new Array(this.indent + 1).join(indent));
this.column += indent.length * this.indent;
}

this.last = code;

buffer.push(code);

for (var i = 0; i < code.length; i++) {
if (code[i] === '\n') {
this.line++;
this.column = 0;
} else {
this.column++;
if (sourceMap) {
for (var i = 0; i < code.length; i++) {
if (code[i] === '\n') {
this.line++;
this.column = 0;
} else {
this.column++;
}
}
}
},
_appendQueue: function _appendQueue(code, element) {
this.queue.push([code, element]);
},
_flushQueue: function _flushQueue() {
var queue = this.queue;
var item = void 0;
while (item = queue.shift()) {
this._append(item[0], item[1]);
}
},
_clearQueue: function _clearQueue() {
this.queue = [];
},
_indent: function _indent() {
this.indent++;
},
Expand Down
Loading

0 comments on commit f63059a

Please sign in to comment.