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

refactor($parse): Create idents lazily #9229

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,11 @@ Lexer.prototype = {
this.tokens.push({
index: start,
text: ident,
fn: CONSTANTS[ident] || getterFn(ident, this.options, expression)
// An indent may reference a primary, a filter or an object literal property
// If the indent is not a constant then we create a lazy getter, this can be turned
// into a real getter if it turns out to be a primary
fn: CONSTANTS[ident],
lazyFn: CONSTANTS[ident] ? undefined : lazyGetterFn(ident, this.options, expression)
});

if (methodName) {
Expand Down Expand Up @@ -426,7 +430,7 @@ Parser.prototype = {
primary = this.object();
} else {
var token = this.expect();
primary = token.fn;
primary = token.fn || token.lazyFn && token.lazyFn();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this line change to

primary = CONSTANTS[token.text] || getterFn(token.text, this.options, this.text);

...and avoid the lazyFn + fn setup above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a primary can be an identifier or a constant, so it would look like something like this

primary = token.fn || token.identifier && (CONSTANTS[token.text] || getterFn(token.text, this.options, token.expression));

where expression and identifier need to be added

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expression text already exists (this.text in Parser), I guess the identifier flag and token.fn || would be needed though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, we already have expression

if (!primary) {
this.throwError('not a primary expression', token);
}
Expand Down Expand Up @@ -945,6 +949,12 @@ function getterFn(path, options, fullExp) {
return fn;
}

function lazyGetterFn(ident, options, expression) {
return function() {
return getterFn(ident, options, expression);
};
}

///////////////////////////////////

/**
Expand Down