Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21106x 21106x 21106x 21106x 123700x 123700x 123700x 123700x 123700x 123700x 123700x 123700x 123700x 123700x 21106x 21106x 20205x 20205x 21106x 21106x 374x 374x 21106x 21106x 446x 446x 446x 446x 2532x 2532x 34x 34x 2532x 446x 446x 446x 21106x 21106x 21106x 2532x 2532x 8652x 8652x 2532x 2532x 21106x 21106x 36x 34x 34x 36x 21106x 21106x 21106x 44221x 44221x 22107x 22107x 22107x 22114x 22114x 44221x 33x 33x 44221x 44221x 44221x 5385x 5385x 22114x 44221x 35569x 35569x 22114x 21106x 21106x 21106x 5385x 5385x 5385x 15570x 15570x 10403x 10403x 5167x 5167x 15570x 9x 9x 9x 9x 9x 9x 15570x 21x 21x 21x 21x 5158x 50x 50x 15570x 5385x 21106x 21106x 22107x 22107x 22107x 19575x 19575x 3x 3x 19575x 19575x 19575x 19575x 19575x 19575x 19575x 19575x 3x 3x 19575x 22107x 22107x 22107x 22107x 22107x 21106x 21106x 14x 14x 14x 21106x 21106x | import * as Expressions from "../abap/2_statements/expressions";
import * as Tokens from "../abap/1_lexer/tokens";
import {Issue} from "../issue";
import {Position} from "../position";
import {ABAPRule} from "./_abap_rule";
import {ExpressionNode, StatementNode, TokenNode} from "../abap/nodes";
import {BasicRuleConfig} from "./_basic_rule_config";
import {RuleTag, IRuleMetadata} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {AbstractToken} from "../abap/1_lexer/tokens/abstract_token";
export class ParserMissingSpaceConf extends BasicRuleConfig {
}
/** state carried through the walk of one statement */
interface IWalkState {
prev: AbstractToken | undefined;
/** outermost Cond or ComponentCond the previous token is part of */
prevCond: ExpressionNode | undefined;
prevInDynamic: boolean;
/** leftmost missing space found so far */
first: Position | undefined;
}
/*
* The lexer records whitespace around parentheses in the token type, eg. "WParenLeft" is a "(" with whitespace
* before but not after. In the expressions below the grammar also accepts the variants without inner whitespace,
* this rule reports them. Elsewhere the grammar is strict, and missing whitespace gives a parser error instead.
* Matched on the constructor, none of these expressions is subclassed.
*/
const PAREN_EXPRESSIONS = new Set<unknown>([
Expressions.MethodCallParam, Expressions.CondSub, Expressions.ComponentCondSub, Expressions.SQLCond,
Expressions.ValueBodyLine, Expressions.NewObject, Expressions.Source]);
export class ParserMissingSpace extends ABAPRule {
private conf = new ParserMissingSpaceConf();
public getMetadata(): IRuleMetadata {
return {
key: "parser_missing_space",
title: "Parser Error, missing space",
shortDescription: `In special cases the ABAP language allows for not having spaces before or after string literals.
This rule makes sure the spaces are consistently required across the language.`,
tags: [RuleTag.Syntax, RuleTag.Whitespace, RuleTag.SingleFile],
badExample: `IF ( foo = 'bar').`,
goodExample: `IF ( foo = 'bar' ).`,
};
}
public getConfig() {
return this.conf;
}
public setConfig(conf: ParserMissingSpaceConf) {
this.conf = conf;
}
public runParsed(file: ABAPFile) {
const issues: Issue[] = [];
const message = "Missing space between string or character literal and parentheses";
for (const statement of file.getStatements()) {
const missing = this.missingSpace(statement);
if (missing) {
issues.push(Issue.atPosition(file, missing, message, this.getMetadata().key, this.conf.severity));
}
}
return issues;
}
/** returns the position of the first missing space in the statement, the rule reports one issue per statement */
private missingSpace(statement: StatementNode): Position | undefined {
const state: IWalkState = {prev: undefined, prevCond: undefined, prevInDynamic: false, first: undefined};
for (const child of statement.getChildren()) {
this.walk(child, undefined, false, state);
}
return state.first;
}
private report(state: IWalkState, pos: Position): void {
if (state.first === undefined || pos.isBefore(state.first)) {
state.first = pos;
}
}
/** visits each node once in source order, checking the parentheses of expressions and each pair of tokens */
private walk(node: ExpressionNode | TokenNode, cond: ExpressionNode | undefined, inDynamic: boolean,
state: IWalkState): void {
if (node instanceof TokenNode) {
this.checkToken(node.get(), cond, inDynamic, state);
return;
}
const type = node.get();
if (cond === undefined && (type instanceof Expressions.Cond || type instanceof Expressions.ComponentCond)) {
cond = node;
}
inDynamic = inDynamic || type instanceof Expressions.Dynamic;
if (PAREN_EXPRESSIONS.has(type.constructor)) {
this.checkParens(node, state);
}
for (const child of node.getChildren()) {
this.walk(child, cond, inDynamic, state);
}
}
/** "foo( 'bar')" and "IF (foo = bar )", parentheses directly part of the expression need inner whitespace */
private checkParens(node: ExpressionNode, state: IWalkState): void {
const children = node.getChildren();
let dynamicAccess = false;
for (let i = 0; i < children.length; i++) {
const child = children[i];
if (!(child instanceof TokenNode)) {
continue;
}
const token = child.get();
if (token instanceof Tokens.ParenLeft || token instanceof Tokens.WParenLeft) {
// dynamic component and attribute access, "foo-(name)" and "foo->(name)"
const prev = children[i - 1]?.get();
if (prev instanceof Tokens.Dash || prev instanceof Tokens.InstanceArrow) {
dynamicAccess = true;
} else {
this.report(state, token.getEnd());
}
} else if (token instanceof Tokens.ParenRight || token instanceof Tokens.ParenRightW) {
if (dynamicAccess) {
dynamicAccess = false;
} else {
this.report(state, token.getStart());
}
} else if (token instanceof Tokens.WParenRight || token instanceof Tokens.WParenRightW) {
dynamicAccess = false;
}
}
}
private checkToken(current: AbstractToken, cond: ExpressionNode | undefined, inDynamic: boolean,
state: IWalkState): void {
const prev = state.prev;
if (prev !== undefined) {
// "foo( )= bar"
if ((prev instanceof Tokens.ParenRight || prev instanceof Tokens.WParenRight) && current.getStr() === "=") {
this.report(state, current.getStart());
}
// "IF foo ='bar'", dynamic parts like "('NAME')" are allowed without spaces
if (current.getStr().startsWith("'")
&& cond !== undefined
&& cond === state.prevCond
&& state.prevInDynamic === false
&& inDynamic === false
&& this.isAdjacent(prev, current)) {
this.report(state, prev.getEnd());
}
}
state.prev = current;
state.prevCond = cond;
state.prevInDynamic = inDynamic;
}
private isAdjacent(first: AbstractToken, second: AbstractToken): boolean {
return first.getRow() === second.getRow()
&& first.getEnd().getCol() === second.getStart().getCol();
}
}
|