Skip to content

Commit

Permalink
Parser should be able to handle babel json (#5224)
Browse files Browse the repository at this point in the history
parser should be able to get the correct es6 module
from a stats file that contains both es5 and es6 content,
  • Loading branch information
caalador committed Mar 11, 2019
1 parent 665214a commit 2a83f86
Show file tree
Hide file tree
Showing 3 changed files with 20,020 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,13 @@ public static Element parseTemplateElement(String fileName, String source) {
NodeUtil.visitPreOrder(parseResult.ast, visitor,
Predicates.alwaysTrue());

Document templateDocument = Jsoup
.parse(visitor.getterContent.get(TEMPLATE_TAG_NAME));
Document templateDocument;

if(visitor.getterContent.containsKey(TEMPLATE_TAG_NAME)) {
templateDocument = Jsoup.parse(visitor.getterContent.get(TEMPLATE_TAG_NAME));
} else {
templateDocument = new Document("");
}

Element template = templateDocument.createElement(TEMPLATE_TAG_NAME);

Expand All @@ -173,6 +178,11 @@ private static String getSourceFromObject(JsonObject module, String fileName) {
if (source == null && validKey(module, NAME, STRING) && validKey(module, SOURCE, STRING)) {
String name = module.getString(NAME);

// If the found module is
if (name.endsWith("es5")) {
return source;
}

// append `.js` extension if not yet as webpack does
fileName = fileName.replaceFirst("(\\.js|)$", ".js");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,34 @@ public void should_throwException_when_ResourceNotFoundInStatsFile() {
instance.getTemplateContent(FooView.class, "foo-view", service);
}

@Test
public void bableStats_shouldAlwaysParseCorrectly() {
Mockito.when(configuration.getStringProperty(Mockito.anyString(), Mockito.anyString()))
.thenReturn("build/babel_stats.json");
TemplateParser instance = NpmTemplateParser.getInstance();
TemplateParser.TemplateData templateContent = instance
.getTemplateContent(MyComponent.class, "my-component", service);

Assert.assertEquals("Parent element ID not the expected one.",
"my-component",
templateContent.getTemplateElement().parent().id());

Assert.assertEquals("Expected template element to have 2 children", 2,
templateContent.getTemplateElement().childNodeSize());

Assert.assertEquals(
"Template element should have contained a div element with the id 'button'",
"button",
templateContent.getTemplateElement().getElementById("button")
.tag().toString());

Assert.assertEquals(
"Template element should have contained a div element with the id 'content'",
"div",
templateContent.getTemplateElement().getElementById("content")
.tag().toString());
}

@Tag("likeable-element")
@JsModule("./frontend/LikeableElement.js")
public class Likeable extends PolymerTemplate<TemplateModel> {
Expand All @@ -143,4 +171,8 @@ public class ReviewList extends PolymerTemplate<TemplateModel> {
public class FooView extends PolymerTemplate<TemplateModel> {
}

@Tag("likeable-element")
@JsModule("./my-component.js")
public class MyComponent extends PolymerTemplate<TemplateModel> {
}
}

0 comments on commit 2a83f86

Please sign in to comment.