UNPKG

auto-cr-rules

Version:

Extensible static analysis rule set for the auto-cr automated code review toolkit

68 lines (67 loc) 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createRuleContext = void 0; var imports_1 = require("./imports"); var messages_1 = require("./messages"); var createRuleContext = function (_a) { var ast = _a.ast, filePath = _a.filePath, source = _a.source, reporter = _a.reporter, language = _a.language; var imports = (0, imports_1.collectImportReferences)(ast); var messages = (0, messages_1.createRuleMessages)(language); var helpers = Object.freeze(createRuleHelpers(reporter, imports)); return Object.freeze({ ast: ast, filePath: filePath, source: source, language: language, reporter: reporter, helpers: helpers, messages: messages, }); }; exports.createRuleContext = createRuleContext; var createRuleHelpers = function (reporter, imports) { var isRelativePath = function (value) { return value.startsWith('.'); }; var relativeDepth = function (value) { return (value.match(/\.\.\//g) || []).length; }; var reportViolation = function (input, spanArg) { var normalized = normalizeViolationInput(input, spanArg); if (typeof reporter.record === 'function') { reporter.record(normalized); return; } var targetSpan = normalized.span; if (targetSpan) { reporter.errorAtSpan(targetSpan, normalized.description); return; } if (typeof normalized.line === 'number') { reporter.errorAtLine(normalized.line, normalized.description); return; } reporter.error(normalized.description); }; return { imports: imports, isRelativePath: isRelativePath, relativeDepth: relativeDepth, reportViolation: reportViolation, }; }; function normalizeViolationInput(input, fallbackSpan) { var _a, _b; if (typeof input === 'string') { return { description: input, span: fallbackSpan, }; } var description = (_a = input.description) !== null && _a !== void 0 ? _a : input.message; return { description: description !== null && description !== void 0 ? description : 'Rule violation detected.', code: input.code, suggestions: input.suggestions, span: (_b = input.span) !== null && _b !== void 0 ? _b : fallbackSpan, line: input.line, }; }