Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
feat($parse): add a hidden interface to retrieve an expression's AST
Browse files Browse the repository at this point in the history
This PR adds a new private method to the `$parse` service, `$$getAst`,
which takes an Angular expression as its only argument and returns
the computed AST. This feature is not meant to be part of the public
API and might be subject to changes, so use it with caution.

Closes #16253

Closes #16260
  • Loading branch information
fpipita authored and gkalpak committed Nov 30, 2017
1 parent 57b626a commit f33d95c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1644,11 +1644,26 @@ Parser.prototype = {
constructor: Parser,

parse: function(text) {
var ast = this.ast.ast(text);
var fn = this.astCompiler.compile(ast);
fn.literal = isLiteral(ast);
fn.constant = isConstant(ast);
var ast = this.getAst(text);
var fn = this.astCompiler.compile(ast.ast);
fn.literal = isLiteral(ast.ast);
fn.constant = isConstant(ast.ast);
fn.oneTime = ast.oneTime;
return fn;
},

getAst: function(exp) {
var oneTime = false;
exp = exp.trim();

if (exp.charAt(0) === ':' && exp.charAt(1) === ':') {
oneTime = true;
exp = exp.substring(2);
}
return {
ast: this.ast.ast(exp),
oneTime: oneTime
};
}
};

Expand Down Expand Up @@ -1771,10 +1786,11 @@ function $ParseProvider() {
isIdentifierStart: isFunction(identStart) && identStart,
isIdentifierContinue: isFunction(identContinue) && identContinue
};
$parse.$$getAst = $$getAst;
return $parse;

function $parse(exp, interceptorFn) {
var parsedExpression, oneTime, cacheKey;
var parsedExpression, cacheKey;

switch (typeof exp) {
case 'string':
Expand All @@ -1784,16 +1800,12 @@ function $ParseProvider() {
parsedExpression = cache[cacheKey];

if (!parsedExpression) {
if (exp.charAt(0) === ':' && exp.charAt(1) === ':') {
oneTime = true;
exp = exp.substring(2);
}
var lexer = new Lexer($parseOptions);
var parser = new Parser(lexer, $filter, $parseOptions);
parsedExpression = parser.parse(exp);
if (parsedExpression.constant) {
parsedExpression.$$watchDelegate = constantWatchDelegate;
} else if (oneTime) {
} else if (parsedExpression.oneTime) {
parsedExpression.$$watchDelegate = parsedExpression.literal ?
oneTimeLiteralWatchDelegate : oneTimeWatchDelegate;
} else if (parsedExpression.inputs) {
Expand All @@ -1811,6 +1823,12 @@ function $ParseProvider() {
}
}

function $$getAst(exp) {
var lexer = new Lexer($parseOptions);
var parser = new Parser(lexer, $filter, $parseOptions);
return parser.getAst(exp).ast;
}

function expressionInputDirtyCheck(newValue, oldValueOfValue, compareObjectIdentity) {

if (newValue == null || oldValueOfValue == null) { // null/undefined
Expand Down
44 changes: 44 additions & 0 deletions test/ng/parseSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3994,4 +3994,48 @@ describe('parser', function() {
});
});
});

describe('hidden/unsupported features', function() {
describe('$$getAst()', function() {
it('should be a method exposed on the `$parse` service', inject(function($parse) {
expect(isFunction($parse.$$getAst)).toBeTruthy();
}));

it('should accept a string expression argument and return the corresponding AST', inject(function($parse) {
var ast = $parse.$$getAst('foo.bar');
expect(ast).toEqual({
type: 'Program',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'MemberExpression',
object: { type: 'Identifier', name: 'foo' },
property: { type: 'Identifier', name: 'bar' },
computed: false
}
}
]
});
}));

it('should parse one time binding expressions', inject(function($parse) {
var ast = $parse.$$getAst('::foo.bar');
expect(ast).toEqual({
type: 'Program',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'MemberExpression',
object: { type: 'Identifier', name: 'foo' },
property: { type: 'Identifier', name: 'bar' },
computed: false
}
}
]
});
}));
});
});
});

0 comments on commit f33d95c

Please sign in to comment.