UNPKG

eslint-plugin-sonarjs

Version:
239 lines (238 loc) • 9.52 kB
"use strict"; /* * SonarQube JavaScript Plugin * Copyright (C) SonarSource Sàrl * mailto:info AT sonarsource DOT com * * You can redistribute and/or modify this program under the terms of * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the Sonar Source-Available License for more details. * * You should have received a copy of the Sonar Source-Available License * along with this program; if not, see https://sonarsource.com/license/ssal/ */ // https://sonarsource.github.io/rspec/#/rspec/S6324/javascript var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.rule = void 0; const generate_meta_js_1 = require("../helpers/generate-meta.js"); const meta = __importStar(require("./generated-meta.js")); const rule_template_js_1 = require("../helpers/regex/rule-template.js"); const EXCEPTIONS = new Set(['\t', '\n']); const MAX_CONTROL_CHAR_CODE = 0x1f; const DELETE = 0x7f; const MAX_C1_CONTROL_CHAR_CODE = 0x9f; // ANSI escape sequence control characters const ESC = 0x1b; const BEL = 0x07; const LEFT_BRACKET = 0x5b; // [ const RIGHT_BRACKET = 0x5d; // ] const BACKSLASH = 0x5c; const STRING_TERMINATOR = 0x9c; const OSC = 0x9d; const OSC_DELIMITERS = [BEL, ESC, STRING_TERMINATOR, OSC]; /** * Control characters used as range boundaries (e.g., [\x00-\x1f]) indicate intentional usage. */ function isCharacterClassRangeBoundary(character) { return character.parent.type === 'CharacterClassRange'; } /** * Standalone control characters are exempt only when the character class contains * a range that starts in the control-character block (e.g., [\x00-\x08\x0b\x0c]). */ function isInCharacterClassWithControlCharRange(character) { const parent = character.parent; if (parent.type !== 'CharacterClass') { return false; } return parent.elements.some(element => element.type === 'CharacterClassRange' && element.min.value <= MAX_CONTROL_CHAR_CODE); } /** * Checks if ESC (0x1b) is followed by [ or ] to form ANSI CSI/OSC sequence start. * Per xterm spec, ESC + [ starts a CSI sequence, ESC + ] starts an OSC sequence. */ function isAnsiSequenceStart(character) { if (character.value !== ESC) { return false; } const parent = character.parent; if (parent.type !== 'Alternative') { return false; } const elements = parent.elements; const index = elements.indexOf(character); if (index === -1 || index >= elements.length - 1) { return false; } const next = elements[index + 1]; return isFixedBracket(next, LEFT_BRACKET) || isFixedBracket(next, RIGHT_BRACKET); } /** Groups and exact-one repetitions must preserve one fixed bracket. */ function isFixedBracket(element, bracket) { if (isCharacter(element, bracket)) { return true; } if (isGroup(element)) { return element.alternatives.every(alternative => alternative.elements.length === 1 && isFixedBracket(alternative.elements[0], bracket)); } return (element?.type === 'Quantifier' && element.min === 1 && element.max === 1 && isFixedBracket(element.element, bracket)); } /** Recognizes deliberate OSC delimiter matching, not terminal-protocol validity. */ function findOscMatchers(alternative) { for (let ancestor = alternative.parent; ancestor; ancestor = ancestor.parent) { if (ancestor.type === 'Assertion') { return []; } } const matches = []; const { elements } = alternative; for (let i = 0; i < elements.length; i++) { const introducerLength = oscIntroducerLength(elements, i); if (introducerLength === 0) { continue; } let endIndex = i + introducerLength; while (endIndex < elements.length && isOscPayload(elements[endIndex])) { endIndex++; } const terminatorLength = oscTerminatorLength(elements, endIndex); if (terminatorLength > 0) { matches.push({ start: elements[i].start, end: elements[endIndex + terminatorLength - 1].end, }); } } return matches; } function oscIntroducerLength(elements, index) { const element = elements[index]; if (isCharacter(element, OSC)) { return 1; } if (isCharacter(element, ESC) && isFixedBracket(elements[index + 1], RIGHT_BRACKET)) { return 2; } return isGroup(element) && element.alternatives.every(isOscIntroducerAlternative) ? 1 : 0; } function isOscIntroducerAlternative(alternative) { const [first, second] = alternative.elements; return ((alternative.elements.length === 1 && isCharacter(first, OSC)) || (alternative.elements.length === 2 && isCharacter(first, ESC) && isFixedBracket(second, RIGHT_BRACKET))); } function oscTerminatorLength(elements, index) { const element = elements[index]; if (isCharacter(element, BEL) || isCharacter(element, STRING_TERMINATOR)) { return 1; } if (isCharacter(element, ESC) && isCharacter(elements[index + 1], BACKSLASH)) { return 2; } return isGroup(element) && element.alternatives.every(isOscTerminatorAlternative) ? 1 : 0; } function isOscTerminatorAlternative(alternative) { const [first, second] = alternative.elements; return ((alternative.elements.length === 1 && (isCharacter(first, BEL) || isCharacter(first, STRING_TERMINATOR))) || (alternative.elements.length === 2 && isCharacter(first, ESC) && isCharacter(second, BACKSLASH))); } function isCharacter(element, value) { return element?.type === 'Character' && element.value === value; } function isGroup(element) { return element?.type === 'Group' || element?.type === 'CapturingGroup'; } function isOscPayload(element) { if (element.type === 'Character') { return (element.value >= 0x20 && (element.value < DELETE || element.value > MAX_C1_CONTROL_CHAR_CODE)); } if (element.type === 'Quantifier') { return isOscPayload(element.element); } if (element.type !== 'CharacterClass' || !element.negate) { return false; } return (element.elements.every(item => item.type === 'Character' || item.type === 'CharacterClassRange') && OSC_DELIMITERS.every(value => element.elements.some(item => item.type === 'Character' ? item.value === value : item.type === 'CharacterClassRange' && item.min.value <= value && value <= item.max.value))); } exports.rule = (0, rule_template_js_1.createRegExpRule)(context => { const oscMatchers = []; return { onAlternativeEnter: alternative => { oscMatchers.push(...findOscMatchers(alternative)); }, onCharacterEnter: (character) => { const { value, raw } = character; if (value >= 0x00 && value <= MAX_CONTROL_CHAR_CODE && (isSameInterpreted(raw, value) || raw.startsWith(String.raw `\x`) || raw.startsWith(String.raw `\u`)) && !EXCEPTIONS.has(raw) && !isCharacterClassRangeBoundary(character) && !isInCharacterClassWithControlCharRange(character) && !isAnsiSequenceStart(character) && !((value === BEL || value === ESC) && oscMatchers.some(matcher => matcher.start <= character.start && character.end <= matcher.end))) { context.reportRegExpNode({ message: 'Remove this control character.', node: context.node, regexpNode: character, }); } }, }; }, (0, generate_meta_js_1.generateMeta)(meta)); /** * When the character has been interpreted, we need to compare its * code point value. */ function isSameInterpreted(raw, value) { return raw.codePointAt(0) === value; }