Skip to content

Commit 341c1d4

Browse files
committed
fix missing Yaml deprecation detection for colon in unquoted values Haehnchen#719
1 parent 9a0339b commit 341c1d4

File tree

5 files changed

+126
-2
lines changed

5 files changed

+126
-2
lines changed

META-INF/plugin.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,12 +539,18 @@
539539
language="Twig"
540540
implementationClass="fr.adrienbrault.idea.symfony2plugin.templating.inspection.TwigVariableDeprecatedInspection"/>
541541

542-
<localInspection groupPath="Symfony" shortName="YamlQuotedEscapedInspection" displayName="Yaml escaped \ in quoted inspection"
543-
groupName="Twig"
542+
<localInspection groupPath="Symfony" shortName="YamlQuotedEscapedInspection" displayName="Quoted issues"
543+
groupName="Yaml"
544544
enabledByDefault="true" level="WARNING"
545545
language="yaml"
546546
implementationClass="fr.adrienbrault.idea.symfony2plugin.intentions.yaml.YamlQuotedEscapedInspection"/>
547547

548+
<localInspection groupPath="Symfony" shortName="YamlUnquotedColon" displayName="Colon in the unquoted mapping"
549+
groupName="Yaml"
550+
enabledByDefault="true" level="WARNING"
551+
language="yaml"
552+
implementationClass="fr.adrienbrault.idea.symfony2plugin.intentions.yaml.YamlUnquotedColon"/>
553+
548554
<intentionAction>
549555
<className>fr.adrienbrault.idea.symfony2plugin.intentions.php.PhpServiceIntention</className>
550556
<category>PHP</category>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package fr.adrienbrault.idea.symfony2plugin.intentions.yaml;
2+
3+
import com.intellij.codeInspection.LocalInspectionTool;
4+
import com.intellij.codeInspection.ProblemHighlightType;
5+
import com.intellij.codeInspection.ProblemsHolder;
6+
import com.intellij.psi.PsiElement;
7+
import com.intellij.psi.PsiElementVisitor;
8+
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
9+
import org.jetbrains.annotations.NotNull;
10+
import org.jetbrains.yaml.YAMLTokenTypes;
11+
import org.jetbrains.yaml.psi.YAMLKeyValue;
12+
import org.jetbrains.yaml.psi.impl.YAMLPlainTextImpl;
13+
14+
/**
15+
* @author Daniel Espendiller <daniel@espendiller.net>
16+
*/
17+
public class YamlUnquotedColon extends LocalInspectionTool {
18+
19+
public static String MESSAGE = "Using a colon in the unquoted mapping value is deprecated since Symfony 2.8 and will throw a ParseException in 3.0";
20+
21+
@NotNull
22+
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
23+
if(!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
24+
return super.buildVisitor(holder, isOnTheFly);
25+
}
26+
27+
return new MyPsiElementVisitor(holder);
28+
}
29+
30+
private static class MyPsiElementVisitor extends PsiElementVisitor {
31+
private final ProblemsHolder holder;
32+
33+
MyPsiElementVisitor(ProblemsHolder holder) {
34+
this.holder = holder;
35+
}
36+
37+
@Override
38+
public void visitElement(PsiElement element) {
39+
if(element.getNode().getElementType() != YAMLTokenTypes.TEXT) {
40+
super.visitElement(element);
41+
return;
42+
}
43+
44+
PsiElement plainScalar = element.getParent();
45+
if(!(plainScalar instanceof YAMLPlainTextImpl)) {
46+
super.visitElement(element);
47+
return;
48+
}
49+
50+
PsiElement yamlKeyValue = plainScalar.getParent();
51+
if(!(yamlKeyValue instanceof YAMLKeyValue)) {
52+
super.visitElement(element);
53+
return;
54+
}
55+
56+
String text = ((YAMLPlainTextImpl) plainScalar).getTextValue();
57+
if(!text.contains(":")) {
58+
super.visitElement(element);
59+
return;
60+
}
61+
62+
holder.registerProblem(
63+
element,
64+
YamlUnquotedColon.MESSAGE,
65+
ProblemHighlightType.WEAK_WARNING
66+
);
67+
68+
super.visitElement(element);
69+
}
70+
}
71+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<html>
2+
<body>
3+
Deprecated quoted issues
4+
<!-- tooltip end -->
5+
</body>
6+
</html>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<html>
2+
<body>
3+
A colon cannot be used in an unquoted mapping value.
4+
<!-- tooltip end -->
5+
</body>
6+
</html>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package fr.adrienbrault.idea.symfony2plugin.tests.intentions.yaml;
2+
3+
import fr.adrienbrault.idea.symfony2plugin.intentions.yaml.YamlUnquotedColon;
4+
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;
5+
6+
/**
7+
* @author Daniel Espendiller <daniel@espendiller.net>
8+
* @see fr.adrienbrault.idea.symfony2plugin.intentions.yaml.YamlUnquotedColon
9+
*/
10+
public class YamlUnquotedColonTest extends SymfonyLightCodeInsightFixtureTestCase {
11+
12+
public void testColonInUnquotedMappingShouldDeprecated() {
13+
assertLocalInspectionContains("foo.yml",
14+
"class: fo<caret>obar:fff",
15+
YamlUnquotedColon.MESSAGE
16+
);
17+
}
18+
19+
public void testColonInUnquotedWithoutMappingScopeShouldNotDeprecated() {
20+
assertLocalInspectionContainsNotContains("foo.yml",
21+
"class: [fo<caret>obar:fff]",
22+
YamlUnquotedColon.MESSAGE
23+
);
24+
assertLocalInspectionContainsNotContains("foo.yml",
25+
"class: [foo, fo<caret>obar:fff]",
26+
YamlUnquotedColon.MESSAGE
27+
);
28+
29+
assertLocalInspectionContainsNotContains("foo.yml",
30+
"class: {fo<caret>obar:fff}",
31+
YamlUnquotedColon.MESSAGE
32+
);
33+
}
34+
35+
}

0 commit comments

Comments
 (0)