Skip to content

Commit c1e6f7d

Browse files
Closure Teamcopybara-github
authored andcommitted
Update ReplaceCssNames transform to support arbitrary code between the object key and the goog.getCssName() call.
PiperOrigin-RevId: 876351497
1 parent 5229c3e commit c1e6f7d

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/com/google/javascript/jscomp/ReplaceCssNames.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
2424
import com.google.javascript.rhino.IR;
2525
import com.google.javascript.rhino.Node;
26+
import com.google.javascript.rhino.Node.AncestorIterable;
2627
import java.util.ArrayList;
2728
import java.util.LinkedHashMap;
2829
import java.util.LinkedHashSet;
@@ -416,8 +417,18 @@ boolean isCssClosureFileClassesMember() {
416417
@Nullable String getCssClosureClassesMemberName() {
417418
checkNotNull(
418419
cssClosureClassesQualifiedName, "Not a css closure file classes object: %s", callNode);
419-
String memberName = callNode.getParent().getString();
420-
return cssClosureClassesQualifiedName + "." + memberName;
420+
// We support 2 structures getCssName() calls in Sass-generated files:
421+
// 1. { Foo: goog.getCssName('foo') }
422+
// 2. { Foo: expression ? … : goog.getCssName('foo') }
423+
// Traversing our ancestry allows us to find the `Foo` string key in both cases.
424+
AncestorIterable ancestors = callNode.getAncestors();
425+
for (Node ancestor : ancestors) {
426+
if (ancestor.isStringKey()) {
427+
return cssClosureClassesQualifiedName + "." + ancestor.getString();
428+
}
429+
}
430+
throw new IllegalStateException(
431+
String.format("No css closure classes member name found: %s", callNode));
421432
}
422433

423434
@Nullable String getCssClassName() {

test/com/google/javascript/jscomp/ReplaceCssNamesTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,57 @@ public void testVariableReferencesToCssClasses() {
516516
assertThat(cssNames).containsExactly("fooStylesBar");
517517
}
518518

519+
@Test
520+
public void testVariableReferencesToXidCssClasses() {
521+
SourceFile cssVarsDefinition =
522+
SourceFile.fromCode(
523+
"foo/styles.css.closure.js",
524+
"""
525+
/**
526+
* @fileoverview generated from foo/styles.css
527+
* @sassGeneratedCssTs
528+
*/
529+
goog.module('foo.styles$2ecss');
530+
// These files will be automatically generated, so we know the
531+
// exported 'classes' property will always be declared like this.
532+
/** @type {{Bar: string, Baz: string}} */
533+
exports.classes = {
534+
'Bar': obfuscate ? xid('fooStylesBar') : goog.getCssName('fooStylesBar'),
535+
'Baz': obfuscate ? xid('fooStylesBaz') : goog.getCssName('fooStylesBaz'),
536+
}
537+
""");
538+
SourceFile cssVarsExpected =
539+
SourceFile.fromCode(
540+
"foo/styles.css.closure.js",
541+
"""
542+
/**
543+
* @fileoverview generated from foo/styles.css
544+
* @sassGeneratedCssTs
545+
*/
546+
goog.module('foo.styles$2ecss');
547+
/** @type {{Bar: string, Baz: string}} */
548+
exports.classes = {
549+
'Bar': obfuscate ? xid('fooStylesBar') : 'fsr',
550+
'Baz': obfuscate ? xid('fooStylesBaz') : 'fsz',
551+
}
552+
""");
553+
SourceFile importer =
554+
SourceFile.fromCode(
555+
"foo/importer.closure.js",
556+
"""
557+
goog.module('foo.importer');
558+
/** @type {string} */
559+
const foo_styles_css = goog.require('foo.styles$2ecss')
560+
// Even when the original TS import was
561+
// `import {classes as foo} from './path/to/file';`
562+
// tsickle will convert references to `foo` into a fully qualified
563+
// name like this rather than creating an alias variable named `foo`.
564+
var x = foo_styles_css.classes.Bar
565+
""");
566+
test(srcs(cssVarsDefinition, importer), expected(cssVarsExpected, importer));
567+
assertThat(cssNames).containsExactly("fooStylesBar");
568+
}
569+
519570
@Test
520571
public void testMixedVariableAndStringReferences() {
521572
SourceFile cssVarsDefinition =

0 commit comments

Comments
 (0)