Skip to content

Commit

Permalink
Merge pull request #178 from jenkinsci/idea-stapler-plugin-172
Browse files Browse the repository at this point in the history
Report custom tag attributes that are marked deprecated.
  • Loading branch information
duemir committed Jan 31, 2024
2 parents d6dd0a1 + f8723d5 commit 26cffda
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.jenkins.stapler.idea.jelly;

import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.XmlElementVisitor;
import com.intellij.psi.xml.XmlAttribute;
import org.jetbrains.annotations.NotNull;
import org.kohsuke.stapler.idea.descriptor.StaplerCustomJellyTagfileXmlAttributeDescriptor;

public class DeprecatedStaplerJellyCustomTagAttributeInspection extends LocalInspectionTool {
@Override
public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
return new XmlElementVisitor() {
@Override
public void visitXmlAttribute(@NotNull XmlAttribute attribute) {
if (attribute.getDescriptor() instanceof StaplerCustomJellyTagfileXmlAttributeDescriptor descriptor && descriptor.getModel().isDeprecated()) {
holder.registerProblem(attribute, String.format("Attribute '%s' is deprecated. Use \"Go to declaration\" to find the recommended solution.", attribute.getName()));
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public boolean isRequired() {
Optional<String> mayBeUseAttrValue = Optional.ofNullable(tag.getAttribute("use")).map(XmlAttribute::getValue);
return mayBeUseAttrValue.isPresent() && mayBeUseAttrValue.get().equals("required");
}

public boolean isDeprecated() {
Optional<String> mayBeUseAttrValue = Optional.ofNullable(tag.getAttribute("deprecated")).map(XmlAttribute::getValue);
return mayBeUseAttrValue.isPresent() && mayBeUseAttrValue.get().equals("true");
}
}
4 changes: 4 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
groupName="Stapler"
implementationClass="org.kohsuke.stapler.idea.I18nInspection" />
-->
<localInspection language="Jelly" enabledByDefault="true" level="WARNING"
displayName="Deprecated Jelly Tag library attribute usage"
groupName="Stapler"
implementationClass="io.jenkins.stapler.idea.jelly.DeprecatedStaplerJellyCustomTagAttributeInspection" />
<!-- jelly file definition parser -->
<lang.parserDefinition language="Jelly"
implementationClass="org.kohsuke.stapler.idea.language.JellyParserDefinition"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<body>
Reports usages of deprecated tag attributes in Jelly files.
<p>
It only analyzes custom tag libraries (a package of Jelly files with a marker file <code>taglib</code>). For attribute to be recognized and considered deprecated it must be explicitly documented and marked as deprecated. Use "Navigate to definition" to jump to the documentation to find the resolution.
</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.jenkins.stapler.idea.jelly;

import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.testFramework.fixtures.BasePlatformTestCase;

import java.util.List;

public class DeprecatedStaplerJellyCustomTagAttributeInspectionTest extends BasePlatformTestCase {
@Override
public void setUp() throws Exception {
super.setUp();
myFixture.enableInspections(new DeprecatedStaplerJellyCustomTagAttributeInspection());
}

@Override
protected String getTestDataPath() {
return "src/test/testData";
}

public void testDeprecateTagAttribute() {
myFixture.copyDirectoryToProject("testlib", "testlib");
myFixture.configureByFile(getTestName(true) + ".jelly");

List<HighlightInfo> highlightInfos = myFixture.doHighlighting(HighlightSeverity.WARNING);
assertNotEmpty(highlightInfos);
assertEquals("Attribute 'title' is deprecated. Use \"Go to declaration\" to find the recommended solution.", highlightInfos.get(0).getDescription());
}
}
5 changes: 5 additions & 0 deletions src/test/testData/deprecateTagAttribute.jelly
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:t="/testlib">
<t:test title="Hello, World!" />
</j:jelly>
Empty file.
7 changes: 7 additions & 0 deletions src/test/testData/testlib/test.jellytag
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler">
<st:documentation>
<st:attribute name="title" deprecated="true">title, deprecated use tooltip instead, or htmlTooltip if you intend to pass HTML.</st:attribute>
</st:documentation>
<p>Test</p>
</j:jelly>

0 comments on commit 26cffda

Please sign in to comment.