Skip to content

Commit

Permalink
handlebars inspection for 'if'/'each' built-in helpers tests е
Browse files Browse the repository at this point in the history
 Conflicts:
	handlebars/src/com/dmarcotte/handlebars/inspections/HbBlockMismatchAnnotator.java
	handlebars/src/com/dmarcotte/handlebars/inspections/HbBlockMismatchInspection.java
	src/com/dmarcotte/handlebars/inspections/HbBlockMismatchInspection.java
  • Loading branch information
anstarovoyt authored and dmarcotte committed Jul 26, 2015
1 parent e292cf6 commit daa6c8e
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 2 deletions.
5 changes: 5 additions & 0 deletions resources/inspectionDescriptions/HbEmptyBlock.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
Handlebars block helpers "if", "each" and some others usually require a parameter
</body>
</html>
6 changes: 6 additions & 0 deletions resources/messages/HbBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ hb.block.mismatch.inspection.close.block=''{1}'' does not match ''{0}'' from blo
hb.block.mismatch.inspection.missing.end.block=''{0}'' block not closed
hb.block.mismatch.inspection.missing.start.block=No block start for ''{0}''
hb.block.mismatch.inspection.open.block=''{0}'' does not match ''{1}'' from block end
hb.block.mismatch.inspection.empty.block=Block helper {0} usually requires a parameter

hb.block.mismatch.intention.rename.close=Change block end ''{0}'' to ''{1}''
hb.block.mismatch.intention.rename.open=Change block start ''{0}'' to ''{1}''
hb.files.file.type.description=Handlebars/Mustache
Expand All @@ -23,6 +25,7 @@ hb.pages.options.complete.close.tooltip=Insert close mustache ("}}" or "}}}") wh
hb.pages.options.title=Handlebars/Mustache
hb.pages.options.html.as.hb=&Open HTML files as Handlebars/Mustache
hb.parsing.comment.unclosed=Unclosed comment
hb.parsing.element.expected.boolean=Expected "true" or "false"
hb.parsing.element.expected.close=Expected Close "}}"
hb.parsing.element.expected.close.unescaped=Expected Close "}}}"
Expand All @@ -45,6 +48,7 @@ hb.parsing.element.expected.outer_element_type=Expected Handlebars Content
hb.parsing.element.expected.separator=Expected a Separator "/" or "."
hb.parsing.element.expected.string=Expected a String
hb.parsing.element.expected.white_space=Expected White Space
hb.parsing.expected.hash=Expected a hash
hb.parsing.expected.parameter=Expected a parameter
hb.parsing.expected.partial.name=Expected partial name
Expand All @@ -54,3 +58,5 @@ hb.parsing.no.open.mustache=No corresponding open mustache
inspections.group.name=Handlebars
mark.as.hb.file=Mark as Handlebars file
unmark.hb.file=Unmark Handlebars file
handlebars.inspections.helpers.empty=Handlebars empty block helper inspection
inspection.group=Handlebars/Mustache
12 changes: 11 additions & 1 deletion src/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,24 @@
<codeFoldingOptionsProvider
instance="com.dmarcotte.handlebars.config.HbFoldingOptionsProvider"/>
<lang.psiStructureViewFactory language="Handlebars" implementationClass="com.dmarcotte.handlebars.structure.HbStructureViewFactory"/>
<annotator language="Handlebars" implementationClass="com.dmarcotte.handlebars.inspections.HbBlockMismatchInspection"/>
<annotator language="Handlebars" implementationClass="com.dmarcotte.handlebars.inspections.HbBlockMismatchAnnotator"/>
<xml.zenCodingGenerator implementation="com.dmarcotte.handlebars.editor.templates.HbEmmetGenerator"/>

<iconProvider implementation="com.dmarcotte.handlebars.file.HbIconProvider"/>
<lang.substitutor language="HTML" implementationClass="com.dmarcotte.handlebars.file.HbLanguageSubstitutor"/>

<liveTemplateContext implementation="com.dmarcotte.handlebars.editor.templates.HbTemplateContextType"/>
<defaultLiveTemplatesProvider implementation="com.dmarcotte.handlebars.editor.templates.HbsLiveTemplatesProvider"/>


<localInspection bundle="messages.HbBundle"
key="handlebars.inspections.helpers.empty"
groupBundle="messages.HbBundle"
groupKey="inspection.group"
language="Handlebars"
enabledByDefault="true"
level="WARNING"
implementationClass="com.dmarcotte.handlebars.inspections.HbEmptyBlockInspection"/>
</extensions>

<extensions defaultExtensionNs="JavaScript">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;

public class HbBlockMismatchInspection implements Annotator {
public class HbBlockMismatchAnnotator implements Annotator {
@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
if (element instanceof HbOpenBlockMustache) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.dmarcotte.handlebars.inspections;


import com.dmarcotte.handlebars.HbBundle;
import com.dmarcotte.handlebars.psi.HbOpenBlockMustache;
import com.dmarcotte.handlebars.psi.HbParam;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;

import java.util.Set;

public class HbEmptyBlockInspection extends LocalInspectionTool {
private static final Set<String> HELPERS_WITH_ARGUMENTS = ContainerUtil.immutableSet("if", "each", "with");

@NotNull
@Override
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
return new PsiElementVisitor() {

@Override
public void visitElement(PsiElement element) {
if (!(element instanceof HbOpenBlockMustache)) {
return;
}

String name = ((HbOpenBlockMustache)element).getName();
if (HELPERS_WITH_ARGUMENTS.contains(name) &&
null == PsiTreeUtil.getChildrenOfType(element, HbParam.class)) {
holder.registerProblem(element, HbBundle.message("hb.block.mismatch.inspection.empty.block", name));
}
}
};
}
}
14 changes: 14 additions & 0 deletions test/data/inspections/EmptyBlock/expected.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>test.hbs</file>
<line>3</line>
<description>Block helper each usually requires a parameter</description>
</problem>
<problem>
<file>test.hbs</file>
<line>5</line>
<description>Block helper if usually requires a parameter</description>
</problem>
</problems>

12 changes: 12 additions & 0 deletions test/data/inspections/EmptyBlock/src/test.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{#if aaa}}
{{aaa}}
{{#each}}
{{aaa}}
{{#if}}
{{aaa}}
{{#each bbb}}
{{aaa}}{{aaa}}
{{/each}}
{{/if}}
{{/each}}
{{/if}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.dmarcotte.handlebars.inspections;


import com.dmarcotte.handlebars.util.HbTestUtils;
import com.intellij.codeInspection.ex.LocalInspectionToolWrapper;
import com.intellij.testFramework.PlatformTestCase;
import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;

public class HbInspectionsTest extends LightPlatformCodeInsightFixtureTestCase {

@SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors")
public HbInspectionsTest() {
PlatformTestCase.initPlatformLangPrefix();
}

@Override
protected boolean isWriteActionRequired() {
return false;
}

public void testEmptyBlock() {
doTest();
}

private void doTest() {
myFixture.testInspection(getTestName(false), new LocalInspectionToolWrapper(new HbEmptyBlockInspection()));
}

@NotNull
@Override
protected String getTestDataPath() {
return HbTestUtils.BASE_TEST_DATA_PATH + "/inspections";
}
}

0 comments on commit daa6c8e

Please sign in to comment.