Skip to content

Commit

Permalink
Allow $ in @template names
Browse files Browse the repository at this point in the history
$ is sometimes used in TypeScript template params names to describe observables, e.g.:

    export interface Foo<T, S$ extends EntitySelectors$<T>> {...}

Tsickle does not tranforms template param names. Closure Compiler currently only allows characters, digits and underscores. This means the code above results in  a JSC_TOO_MANY_TEMPLATE_PARAMS error, when tsickle instantiates the interfaces with 2 template args.

This change adds $ to allowed template name characters.

PiperOrigin-RevId: 597805467
  • Loading branch information
frigus02 authored and copybara-github committed Jan 12, 2024
1 parent 70cc650 commit 8c52080
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/com/google/javascript/jscomp/parsing/JsDocInfoParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public final class JsDocInfoParser {
private static final String TTL_END_DELIMITER = "=:";

private static final CharMatcher TEMPLATE_NAME_MATCHER =
CharMatcher.javaLetterOrDigit().or(CharMatcher.is('_'));
CharMatcher.javaLetterOrDigit().or(CharMatcher.anyOf("_$"));

@VisibleForTesting
public static final String BAD_TYPE_WIKI_LINK =
Expand Down Expand Up @@ -1279,7 +1279,10 @@ private JsDocToken parseAnnotation(JsDocToken token, List<ExtendedTypeInfo> exte
return ttlParser.getTypeTransformationAst();
}

/** The types in @template annotations must contain only letters, digits, and underscores. */
/**
* The types in @template annotations must contain only letters, digits, underscores and dollar
* signs.
*/
private static boolean validTemplateTypeName(String name) {
return name != null && !name.isEmpty() && TEMPLATE_NAME_MATCHER.matchesAllOf(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3931,6 +3931,12 @@ public void testParserWithLowercaseTemplateTypeName() {
parse("@template key,value */");
}

@Test
public void testParserWithTemplateTypeNameWithDollar() {
JSDocInfo info = parse("@template A,B$,C */");
assertThat(info.getTemplateTypeNames()).containsExactly("A", "B$", "C");
}

@Test
public void testParserWithTypeTransformationNewline() {
parse("@template R := \n 'string' =:*/");
Expand Down

0 comments on commit 8c52080

Please sign in to comment.