eslint-plugin-sonarjs
Version:
148 lines (147 loc) • 6.22 kB
JavaScript
;
/*
* 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/S1135/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;
exports.reportPatternInComment = reportPatternInComment;
const generate_meta_js_1 = require("../helpers/generate-meta.js");
const meta = __importStar(require("./generated-meta.js"));
const todoPattern = 'todo';
const letterPattern = /[\p{Letter}]/u;
const jiraIssueKeyPattern = /\b[A-Z][A-Z0-9]+-\d+\b/u;
const commentStartPattern = /^[\s*]*$/u;
const sentenceStartPattern = /[.!?][\s([{"'*]*$/u;
exports.rule = {
meta: (0, generate_meta_js_1.generateMeta)(meta, {
messages: {
completeTODO: 'Complete the task associated to this "TODO" comment.',
},
}),
create(context) {
return {
'Program:exit': () => {
reportPatternInComment(context, todoPattern, 'completeTODO', false, shouldIgnoreTodo);
},
};
},
};
function reportPatternInComment(context, pattern, messageId, caseSensitive = false, shouldIgnoreMatch = () => false) {
const normalizedPattern = caseSensitive ? pattern : pattern.toLowerCase();
for (const comment of context.sourceCode.getAllComments()) {
if (comment.value.trim().startsWith('eslint-disable')) {
continue;
}
for (const loc of findPatternPositions(comment, normalizedPattern, caseSensitive, shouldIgnoreMatch)) {
context.report({ messageId, loc });
}
}
}
function findPatternPositions(comment, pattern, caseSensitive, shouldIgnoreMatch) {
const rawText = caseSensitive ? comment.value : comment.value.toLowerCase();
if (!rawText.includes(pattern)) {
return [];
}
const originalLines = comment.value.split(/\r\n?|\n/);
const lines = rawText.split(/\r\n?|\n/);
return lines.flatMap((line, index) => findPatternPosition(line, originalLines[index], index, comment, pattern, shouldIgnoreMatch));
}
function findPatternPosition(line, originalLine, lineIdx, comment, pattern, shouldIgnoreMatch) {
let searchStart = 0;
while (searchStart < line.length) {
const index = line.indexOf(pattern, searchStart);
if (index < 0) {
return [];
}
if (!isLetterAround(line, index, pattern) && !shouldIgnoreMatch(originalLine, index, pattern)) {
return [getPatternPosition(lineIdx, index, comment, pattern)];
}
searchStart = index + pattern.length;
}
return [];
}
/**
* Checks whether a TODO is followed by a Jira issue key on the same line.
* @param line The original comment line.
* @param start The start index of the matched TODO marker.
* @param pattern The tracked marker.
* @return `true` when the TODO should be ignored.
*/
function isJiraAnchoredTodo(line, start, pattern) {
return jiraIssueKeyPattern.test(line.slice(start + pattern.length));
}
function shouldIgnoreTodo(line, start, pattern) {
return isJiraAnchoredTodo(line, start, pattern) || isProseTodo(line, start, pattern);
}
function isProseTodo(line, start, pattern) {
const matchedText = line.slice(start, start + pattern.length);
const startsSentenceOrComment = isSentenceOrCommentStart(line, start);
return ((matchedText === 'Todo' && startsSentenceOrComment) ||
(matchedText === 'todo' && !startsSentenceOrComment));
}
function isSentenceOrCommentStart(line, start) {
const prefix = line.slice(0, start);
return commentStartPattern.test(prefix) || sentenceStartPattern.test(prefix);
}
function isLetterAround(line, start, pattern) {
const end = start + pattern.length;
const pre = start > 0 && letterPattern.test(line.charAt(start - 1));
const post = end <= line.length - 1 && letterPattern.test(line.charAt(end));
return pre || post;
}
function getPatternPosition(lineIdx, index, comment, pattern) {
const line = comment.loc.start.line + lineIdx;
const columnStart = lineIdx === 0 ? comment.loc.start.column + 2 : 0;
const patternStart = columnStart + index;
return {
start: { line, column: patternStart },
end: { line, column: patternStart + pattern.length },
};
}