mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
PY-20776 Report empty expression fragments inside f-strings
This commit is contained in:
@@ -644,6 +644,7 @@
|
||||
<dumbAnnotator implementation="com.jetbrains.python.validation.GeneratorInArgumentListAnnotator"/>
|
||||
<dumbAnnotator implementation="com.jetbrains.python.validation.StarAnnotator"/>
|
||||
<dumbAnnotator implementation="com.jetbrains.python.validation.StringLiteralQuotesAnnotator"/>
|
||||
<dumbAnnotator implementation="com.jetbrains.python.validation.FStringsAnnotator"/>
|
||||
<dumbAnnotator implementation="com.jetbrains.python.validation.DumbAwareHighlightingAnnotator"/>
|
||||
|
||||
<customTargetExpressionStubType implementation="com.jetbrains.python.psi.impl.stubs.PropertyStubType"/>
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.jetbrains.python.validation;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.jetbrains.python.codeInsight.fstrings.FStringParser;
|
||||
import com.jetbrains.python.codeInsight.fstrings.FStringParser.FragmentOffsets;
|
||||
import com.jetbrains.python.psi.PyStringLiteralExpression;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.jetbrains.python.psi.PyUtil.StringNodeInfo;
|
||||
|
||||
/**
|
||||
* @author Mikhail Golubev
|
||||
*/
|
||||
public class FStringsAnnotator extends PyAnnotator {
|
||||
@Override
|
||||
public void visitPyStringLiteralExpression(PyStringLiteralExpression pyString) {
|
||||
for (ASTNode node : pyString.getStringNodes()) {
|
||||
if (new StringNodeInfo(node).isFormatted()) {
|
||||
final int nodeOffset = node.getTextRange().getStartOffset();
|
||||
final List<FragmentOffsets> fragments = FStringParser.parse(node.getText());
|
||||
for (FragmentOffsets fragment : fragments) {
|
||||
if (fragment.getLeftBraceOffset() + 1 >= fragment.getContentEndOffset()) {
|
||||
report(fragment.getContentRange().shiftRight(nodeOffset), "Empty expressions are not allowed inside f-strings");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void report(@NotNull TextRange range, @NotNull String message) {
|
||||
getHolder().createErrorAnnotation(range, message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
f'{<error descr="Empty expressions are not allowed inside f-strings"></error>}'
|
||||
f'{<error descr="Empty expressions are not allowed inside f-strings"></error>'
|
||||
<error descr="Missing closing quote [']">f'{<error descr="Empty expressions are not allowed inside f-strings"></error></error>
|
||||
f'{<error descr="Empty expressions are not allowed inside f-strings"></error>!r}'
|
||||
f'{<error descr="Empty expressions are not allowed inside f-strings"></error>:2.3}'
|
||||
f'{42:2.{<error descr="Empty expressions are not allowed inside f-strings"></error>}}'
|
||||
@@ -305,6 +305,11 @@ public class PythonHighlightingTest extends PyTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
// PY-20776
|
||||
public void testFStringEmptyExpressions() {
|
||||
runWithLanguageLevel(LanguageLevel.PYTHON36, () -> doTest(true, false));
|
||||
}
|
||||
|
||||
// ---
|
||||
private void doTest(final LanguageLevel languageLevel, final boolean checkWarnings, final boolean checkInfos) {
|
||||
PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), languageLevel);
|
||||
|
||||
Reference in New Issue
Block a user