Skip to content

Commit

Permalink
feat: support extending processor
Browse files Browse the repository at this point in the history
  • Loading branch information
whxaxes committed Jul 4, 2017
1 parent 8d41e07 commit 5feda67
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/compile/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ class Ast {
this.blockEnd = options.blockEnd || '%}';
this.variableStart = options.variableStart || '{{';
this.variableEnd = options.variableEnd || '}}';
this.processor = processor;
this.commentStart = '{#';
this.commentEnd = '#}';

// support extending processor
if (options.processor) {
this.processor = Object.assign({}, processor, options.processor);
}

if (this.blockStart === this.variableStart) {
throw new Error('blockStart should be different with variableStart!');
}
Expand Down Expand Up @@ -100,7 +106,8 @@ class Ast {
const matches = expression.match(tagNameRE);
const tagName = matches[0];
const isCustom = mus && mus.customTags.has(tagName);
const tagHandle = processor[tagName] || (isCustom ? processor.custom : null);
const tagHandle = this.processor[tagName]
|| (isCustom ? this.processor.custom : null);

if (tagHandle) {
// create ast node
Expand Down Expand Up @@ -134,7 +141,7 @@ class Ast {
collectChars(collector, leftStart + expression + rightEnd);
} else {
element = this.genNode(3);
processor.variable(element, expression);
this.processor.variable(element, expression);
}
}

Expand Down
11 changes: 11 additions & 0 deletions test/ast.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,15 @@ describe('lib#compile#ast', () => {
const m = a.genMacro();
assert(m === a.genMacro());
});

it('ast should support extending processor', done => {
const str = '{{ test }}';
ast(str, {
processor: {
variable() {
done();
}
}
});
});
});

0 comments on commit 5feda67

Please sign in to comment.