UNPKG

@eagleoutice/flowr

Version:

Static Dataflow Analyzer and Program Slicer for the R Programming Language

143 lines 6.88 kB
"use strict"; 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.comments = exports.initialCommentInfo = void 0; const xpath = __importStar(require("xpath-ts2")); const post_process_1 = require("./post-process"); const assert_1 = require("../../../../util/assert"); const statistics_file_1 = require("../../../output/statistics-file"); exports.initialCommentInfo = { totalAmount: 0, roxygenComments: 0, import: 0, importFrom: 0, importMethodsFrom: 0, importClassesFrom: 0, useDynLib: 0, export: 0, exportClass: 0, exportMethod: 0, exportS3Method: 0, exportPattern: 0 }; const commentQuery = xpath.parse('//COMMENT'); const importRegex = /^'\s*@import\s+(?<package>\S+)/; const importFromRegex = /^'\s*@importFrom\s+(?<package>\S+)(?<fn>( +\S+)+)$/; const useDynLibRegex = /^'\s*@useDynLib\s+(?<package>\S+)(?<fn>( +\S+)+)?$/; /** we still name the classes fn, so we can reuse processing code */ const importClassesFromRegex = /^'\s*@importClassesFrom\s+(?<package>\S+)(?<fn>( +\S+)+)$/; const importMethodsFrom = /^'\s*@importMethodsFrom\s+(?<package>\S+)(?<fn>( +\S+)+)$/; /** deliberately includes the others to get a "total" overview */ const exportRegex = /^'\s*@export/; const exportS3MethodRegex = /^'\s*@exportS3Method/; const exportClassRegex = /^'\s*@exportClass/; const exportMethodRegex = /^'\s*@exportMethod/; const exportPatternRegex = /^'\s*@exportPattern/; function processRoxygenImport(existing, commentsText, filepath) { const packages = commentsText.map(text => importRegex.exec(text)?.groups?.package).filter(assert_1.isNotUndefined); existing.import += packages.length; (0, statistics_file_1.appendStatisticsFile)(exports.comments.name, 'import', packages, filepath, true); } function processWithRegex(commentsText, existing, regex) { return commentsText.map(text => regex.exec(text)).filter(assert_1.isNotNull) .flatMap(match => { const packageName = match.groups?.package ?? '<unknown>'; return (match.groups?.fn.trim().split(/\s+/) ?? []).map(fn => `${JSON.stringify(packageName)},${fn}`); }); } function processRoxygenImportFrom(existing, commentsText, filepath) { const result = processWithRegex(commentsText, existing, importFromRegex); existing.importFrom += result.length; (0, statistics_file_1.appendStatisticsFile)(exports.comments.name, 'importFrom', result, filepath, true); } function processRoxygenImportClassesFrom(existing, commentsText, filepath) { const result = processWithRegex(commentsText, existing, importClassesFromRegex); existing.importClassesFrom += result.length; (0, statistics_file_1.appendStatisticsFile)(exports.comments.name, 'importClassesFrom', result, filepath, true); } function processRoxygenImportMethodsFrom(existing, commentsText, filepath) { const result = processWithRegex(commentsText, existing, importMethodsFrom); existing.importMethodsFrom += result.length; (0, statistics_file_1.appendStatisticsFile)(exports.comments.name, 'importMethodsFrom', result, filepath, true); } function processExports(existing, comments) { existing.export += comments.filter(text => exportRegex.test(text)).length; existing.exportClass += comments.filter(text => exportClassRegex.test(text)).length; existing.exportMethod += comments.filter(text => exportMethodRegex.test(text)).length; existing.exportS3Method += comments.filter(text => exportS3MethodRegex.test(text)).length; existing.exportPattern += comments.filter(text => exportPatternRegex.test(text)).length; } function processMatchForDynLib(match) { const packageName = match.groups?.package ?? '<unknown>'; const functions = match.groups?.fn?.trim().split(/\s+/) ?? []; if (functions.length === 0) { return [packageName]; } else { return functions.map(fn => `${JSON.stringify(packageName)},${fn}`); } } function processRoxygenUseDynLib(existing, commentsText, filepath) { const result = commentsText.map(text => useDynLibRegex.exec(text)) .filter(assert_1.isNotNull) .flatMap(processMatchForDynLib); existing.useDynLib += result.length; (0, statistics_file_1.appendStatisticsFile)(exports.comments.name, 'useDynLib', result, filepath, true); } exports.comments = { name: 'Comments', description: 'All comments that appear within the document', process(existing, input) { const comments = commentQuery.select({ node: input.parsedRAst }).map(node => node.textContent ?? '#') .map(text => { (0, assert_1.guard)(text.startsWith('#'), `unexpected comment ${text}`); return text.slice(1); }) .map(text => text.trim()); existing.totalAmount += comments.length; const roxygenComments = comments.filter(text => text.startsWith("'")); existing.roxygenComments += roxygenComments.length; processRoxygenImport(existing, roxygenComments, input.filepath); processRoxygenImportFrom(existing, roxygenComments, input.filepath); processRoxygenUseDynLib(existing, roxygenComments, input.filepath); processRoxygenImportClassesFrom(existing, roxygenComments, input.filepath); processRoxygenImportMethodsFrom(existing, roxygenComments, input.filepath); processExports(existing, roxygenComments); return existing; }, initialValue: exports.initialCommentInfo, postProcess: post_process_1.postProcess }; //# sourceMappingURL=comments.js.map