WEB-27035 Redundant default attribute value assignment.

This commit is contained in:
Dmitry Avdeev
2017-07-20 15:20:24 +03:00
parent fa23553f5f
commit 72540b1b2f
2 changed files with 33 additions and 23 deletions
@@ -57,4 +57,10 @@ public class XmlInspectionsTest extends LightPlatformCodeInsightFixtureTestCase
"</html>\n");
myFixture.checkHighlighting();
}
public void testDefaultAttributeInHtml() {
myFixture.enableInspections(new XmlDefaultAttributeValueInspection());
myFixture.configureByText(HtmlFileType.INSTANCE, "<input type=\"text\"/>");
myFixture.checkHighlighting();
}
}
@@ -21,6 +21,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.XmlElementVisitor;
import com.intellij.psi.html.HtmlTag;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlAttributeValue;
@@ -40,30 +41,33 @@ public class XmlDefaultAttributeValueInspection extends XmlSuppressableInspectio
@Override
public void visitXmlAttributeValue(XmlAttributeValue value) {
PsiElement parent = value.getParent();
if (parent instanceof XmlAttribute) {
XmlAttributeDescriptor descriptor = ((XmlAttribute)parent).getDescriptor();
if (descriptor != null) {
String defaultValue = descriptor.getDefaultValue();
if (defaultValue != null && defaultValue.equals(value.getValue())) {
holder.registerProblem(value, "Redundant default attribute value assignment", ProblemHighlightType.LIKE_UNUSED_SYMBOL,
new LocalQuickFix() {
@Nls
@NotNull
@Override
public String getFamilyName() {
return XmlErrorMessages.message("remove.attribute.quickfix.family");
}
if (!(parent instanceof XmlAttribute)) {
return;
}
if (parent.getParent() instanceof HtmlTag && "input".equals(((HtmlTag)parent.getParent()).getName())) return;
XmlAttributeDescriptor descriptor = ((XmlAttribute)parent).getDescriptor();
if (descriptor == null) {
return;
}
String defaultValue = descriptor.getDefaultValue();
if (defaultValue != null && defaultValue.equals(value.getValue())) {
holder.registerProblem(value, "Redundant default attribute value assignment", ProblemHighlightType.LIKE_UNUSED_SYMBOL,
new LocalQuickFix() {
@Nls
@NotNull
@Override
public String getFamilyName() {
return XmlErrorMessages.message("remove.attribute.quickfix.family");
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
XmlAttribute attribute = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), XmlAttribute.class);
if (attribute != null) {
attribute.delete();
}
}
});
}
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
XmlAttribute attribute = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), XmlAttribute.class);
if (attribute != null) {
attribute.delete();
}
}
});
}
}
};