Skip to content

Commit

Permalink
Internal
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 576331619
  • Loading branch information
Jesse-Good authored and copybara-github committed Oct 25, 2023
1 parent fe76537 commit 450abbd
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private void process(TemplateNode template, HtmlTagNode tagNode, IdGenerator nod
call.setHtmlContext(HtmlContext.HTML_PCDATA);
tagNode.getParent().replaceChild(tagNode, call);

ImmutableMap<String, SoyType> parameterMap = templateType.getParameterMap();
ImmutableMap<String, TemplateType.Parameter> parameterMap = templateType.getParameterMap();

Set<String> seenAttr = new HashSet<>();
tagNode.getChildren().stream()
Expand Down Expand Up @@ -340,7 +340,7 @@ private void maybeConsumeAttribute(
CallBasicNode call,
IdGenerator nodeIdGen,
Set<String> seenAttr,
Map<String, SoyType> parameterMap,
Map<String, TemplateType.Parameter> parameterMap,
Map<String, AttrParam> attrs,
@Nullable CallParamContentNode attributesNode,
Optional<ExprNode> conditional) {
Expand Down Expand Up @@ -445,7 +445,7 @@ private CallParamNode consumeAttribute(
HtmlAttributeNode attr,
IdGenerator nodeIdGen,
Set<String> seenAttr,
Map<String, SoyType> parameterMap,
Map<String, TemplateType.Parameter> parameterMap,
Map<String, AttrParam> attrs,
CallParamContentNode attributesNode,
CallBasicNode call,
Expand Down Expand Up @@ -501,7 +501,7 @@ private CallParamNode consumeAttribute(
new CommandTagAttribute(
Identifier.create("kind", unknown),
QuoteStyle.SINGLE,
getKind(parameterMap.get(paramName)),
getKind(parameterMap.get(paramName).getType()),
unknown,
unknown),
errorReporter);
Expand Down Expand Up @@ -529,7 +529,8 @@ private CallParamNode consumeAttribute(
"$__internal_call_" + paramName + nodeIdGen.genId(),
unknown,
parameterMap.containsKey(paramName)
? SanitizedContentKind.fromAttributeValue(getKind(parameterMap.get(paramName)))
? SanitizedContentKind.fromAttributeValue(
getKind(parameterMap.get(paramName).getType()))
.get()
: SanitizedContentKind.TEXT);
call.getParent().addChild(call.getParent().getChildIndex(call), letContentNode);
Expand Down
42 changes: 42 additions & 0 deletions java/src/com/google/template/soy/soytree/SoyTreeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
Expand Down Expand Up @@ -651,4 +652,45 @@ public static Stream<TypeNode> allTypeNodes(SoyNode root) {
}
});
}

public static ExprNodeToHolderIndex buildExprNodeToHolderIndex(SoyNode root) {
return new ExprNodeToHolderIndex(root);
}

/** Index mapping an expression node to the soy node that holds it. */
public static class ExprNodeToHolderIndex {

private final ImmutableMap<ExprRootNode, ExprHolderNode> index;

ExprNodeToHolderIndex(SoyNode root) {
ImmutableMap.Builder<ExprRootNode, ExprHolderNode> index = ImmutableMap.builder();
allNodesOfType(root, ExprHolderNode.class)
.forEach(
holder -> {
for (ExprRootNode exprRoot : holder.getExprList()) {
index.put(exprRoot, holder);
}
});
this.index = index.buildOrThrow();
}

ExprHolderNode getHolder(ExprRootNode root) {
return Preconditions.checkNotNull(index.get(root));
}

public ExprHolderNode getHolder(ExprNode node) {
while (node.getParent() != null) {
node = node.getParent();
}
return getHolder((ExprRootNode) node);
}

@Nullable
public ExprHolderNode getNullableHolder(ExprNode node) {
while (node.getParent() != null) {
node = node.getParent();
}
return index.get((ExprRootNode) node);
}
}
}
15 changes: 11 additions & 4 deletions java/src/com/google/template/soy/types/RecordType.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.NavigableMap;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

/**
* Dict type - classic dictionary type with string keys. Only works with field (dot) access.
Expand Down Expand Up @@ -56,13 +57,12 @@ public static Member memberOf(String name, boolean optional, SoyType type) {
}

private final ImmutableList<Member> members;
private final ImmutableMap<String, SoyType> memberIndex;
private final ImmutableMap<String, Member> memberIndex;

private RecordType(Iterable<Member> members) {
this.members = ImmutableList.copyOf(members);
this.memberIndex =
Streams.stream(members)
.collect(ImmutableMap.toImmutableMap(Member::name, Member::checkedType));
Streams.stream(members).collect(ImmutableMap.toImmutableMap(Member::name, m -> m));
}

/**
Expand Down Expand Up @@ -111,10 +111,17 @@ public ImmutableList<Member> getMembers() {
return members;
}

public SoyType getMemberType(String fieldName) {
@Nullable
public Member getMember(String fieldName) {
return memberIndex.get(fieldName);
}

@Nullable
public SoyType getMemberType(String fieldName) {
Member member = memberIndex.get(fieldName);
return member != null ? member.checkedType() : null;
}

public Iterable<String> getMemberNames() {
return members.stream().map(Member::name).collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private static SoyType bindParametersToTemplate(
boolean reportedErrors = false;
TemplateType.Builder builder = base.toBuilder();
for (RecordType.Member member : parameters.getMembers()) {
if (!base.getParameterMap().containsKey(member.name())) {
if (base.getParameter(member.name()) == null) {
if (member.name().equals(TemplateType.EXTRA_ROOT_ELEMENT_ATTRIBUTES)
&& base.getAllowExtraAttributes()) {
builder.setAllowExtraAttributes(false);
Expand All @@ -91,12 +91,12 @@ private static SoyType bindParametersToTemplate(
reportedErrors = true;
continue;
}
if (!base.getParameterMap().get(member.name()).isAssignableFromLoose(member.checkedType())) {
if (!base.getParameter(member.name()).getType().isAssignableFromLoose(member.checkedType())) {
errorReporter.report(
PARAMETER_TYPE_MISMATCH,
member.name(),
base,
base.getParameterMap().get(member.name()),
base.getParameter(member.name()).getType(),
member.checkedType());
reportedErrors = true;
}
Expand Down
10 changes: 8 additions & 2 deletions java/src/com/google/template/soy/types/TemplateType.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,14 @@ public ImmutableList<Parameter> getActualParameters() {
return builder.build();
}

public final ImmutableMap<String, SoyType> getParameterMap() {
return getParameters().stream().collect(toImmutableMap(Parameter::getName, Parameter::getType));
@Memoized
public ImmutableMap<String, Parameter> getParameterMap() {
return getParameters().stream().collect(toImmutableMap(Parameter::getName, p -> p));
}

@Nullable
public final Parameter getParameter(String name) {
return getParameterMap().get(name);
}

public abstract ImmutableList<DataAllCallSituation> getDataAllCallSituations();
Expand Down

0 comments on commit 450abbd

Please sign in to comment.