Added inspection for invalid leading character

GitOrigin-RevId: 243d0c80a3c3bb848b3193d183861e7e9255485c
This commit is contained in:
marcelhillesheim
2021-10-20 15:24:31 +02:00
committed by intellij-monorepo-bot
parent e37179107e
commit 490aa2c815
6 changed files with 83 additions and 6 deletions
@@ -0,0 +1,49 @@
package ru.adelf.idea.dotenv.inspections;
import com.intellij.codeInspection.InspectionManager;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import ru.adelf.idea.dotenv.psi.DotEnvFile;
import ru.adelf.idea.dotenv.psi.DotEnvKey;
public class LeadingCharacterInspection extends LocalInspectionTool {
// Change the display name within the plugin.xml
// This needs to be here as otherwise the tests will throw errors.
@NotNull
@Override
public String getDisplayName() {
return "Invalid leading character";
}
@Nullable
@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
if (!(file instanceof DotEnvFile)) {
return null;
}
return analyzeFile(file, manager, isOnTheFly).getResultsArray();
}
@NotNull
private ProblemsHolder analyzeFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
ProblemsHolder problemsHolder = new ProblemsHolder(manager, file, isOnTheFly);
PsiTreeUtil.findChildrenOfType(file, DotEnvKey.class).forEach(dotEnvKey -> {
// Also accepts lower case chars as keys with lower case chars are handled by another inspection
// same for dash (-> IncorrectDelimiter
if (!dotEnvKey.getText().matches("[A-Za-z_-].*")){
problemsHolder.registerProblem(dotEnvKey,
"Invalid first char for a key. Only A-Z and '_' are allowed.");
}
});
return problemsHolder;
}
}
@@ -118,6 +118,14 @@
level="WARNING"
implementationClass="ru.adelf.idea.dotenv.inspections.IncorrectDelimiterInspection"/>
<localInspection language="DotEnv"
groupName="DotEnv"
shortName="DotEnvLeadingCharacterInspection"
displayName="Invalid leading character"
enabledByDefault="true"
level="WARNING"
implementationClass="ru.adelf.idea.dotenv.inspections.LeadingCharacterInspection"/>
<lang.syntaxHighlighterFactory language="DotEnv"
implementationClass="ru.adelf.idea.dotenv.DotEnvSyntaxHighlighterFactory"/>
@@ -0,0 +1,5 @@
<html>
<body>
Reports keys with invalid leading character in .env files. Only A-Z and '_' are allowed as first char of a key.
</body>
</html>
@@ -3,10 +3,10 @@ package ru.adelf.idea.dotenv.tests.dotenv;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.InspectionProfileEntry;
import ru.adelf.idea.dotenv.inspections.DuplicateKeyInspection;
import ru.adelf.idea.dotenv.inspections.ExtraBlankLineInspection;
import ru.adelf.idea.dotenv.inspections.IncorrectDelimiterInspection;
import ru.adelf.idea.dotenv.inspections.SpaceInsideNonQuotedInspection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import ru.adelf.idea.dotenv.inspections.*;
import ru.adelf.idea.dotenv.tests.DotEnvLightCodeInsightFixtureTestCase;
import java.util.ArrayList;
@@ -14,6 +14,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@RunWith(JUnit4.class)
public class InspectionsTest extends DotEnvLightCodeInsightFixtureTestCase {
@Override
@@ -28,23 +29,33 @@ public class InspectionsTest extends DotEnvLightCodeInsightFixtureTestCase {
// Test for each Inspection
@Test
public void testDuplicateKey() {
doInspectionTest(new DuplicateKeyInspection(), Arrays.asList("DUPLICATE_KEY=test", "DUPLICATE_KEY=test2"));
}
@Test
public void testSpaceInsideNonQuoted() {
doInspectionTest(new SpaceInsideNonQuotedInspection(), Collections.singletonList("spaces without quotes"));
}
@Test
public void testExtraBlankLine() {
doInspectionTest(new ExtraBlankLineInspection(), Collections.singletonList("\n\n\n"));
}
@Test
public void testIncorrectDelimiterInspection() {
doInspectionTest(new IncorrectDelimiterInspection(), Collections.singletonList("INCORRECT-DELIMITER"));
}
@Test
public void testLeadingCharacterInspection() {
doInspectionTest(new LeadingCharacterInspection(), Collections.singletonList("*LEADING_CHARACTER"));
}
// Every available quickfix from every inspection is getting applied
@Test
public void testQuickFixes() {
myFixture.enableInspections(new SpaceInsideNonQuotedInspection());
myFixture.enableInspections(new ExtraBlankLineInspection());
@@ -8,4 +8,6 @@ SPACE_INSIDE_NON_QUOTED=spaces without quotes
# extra blank lines test END
INCORRECT-DELIMITER=test-test
INCORRECT-DELIMITER=test-test
*LEADING_CHARACTER=test
@@ -7,4 +7,6 @@ SPACE_INSIDE_NON_QUOTED="spaces without quotes"
# extra blank lines test END
INCORRECT_DELIMITER=test-test
INCORRECT_DELIMITER=test-test
*LEADING_CHARACTER=test