@eagleoutice/flowr
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
46 lines • 2.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.USELESS_LOOP = void 0;
const useless_loop_1 = require("../../control-flow/useless-loop");
const vertex_1 = require("../../dataflow/graph/vertex");
const flowr_search_builder_1 = require("../../search/flowr-search-builder");
const range_1 = require("../../util/range");
const linter_format_1 = require("../linter-format");
const linter_tags_1 = require("../linter-tags");
exports.USELESS_LOOP = {
createSearch: () => flowr_search_builder_1.Q.all().filter(vertex_1.VertexType.FunctionCall),
processSearchResult: (elements, useLessLoopConfig, { analyzer, dataflow, normalize, cfg }) => {
const results = elements.getElements().filter(e => {
const vertex = dataflow.graph.getVertex(e.node.info.id);
return vertex
&& (0, vertex_1.isFunctionCallVertex)(vertex)
&& vertex.origin !== 'unnamed'
&& useLessLoopConfig.loopyFunctions.has(vertex.origin[0]);
}).filter(loop => (0, useless_loop_1.onlyLoopsOnce)(loop.node.info.id, dataflow.graph, cfg, normalize, analyzer.inspectContext())).map(res => ({
certainty: linter_format_1.LintingResultCertainty.Certain,
name: res.node.lexeme,
loc: range_1.SourceLocation.fromNode(res.node) ?? range_1.SourceLocation.invalid(),
involvedId: res.node.info.id
}));
return {
results: results,
'.meta': {
numOfUselessLoops: results.length
}
};
},
prettyPrint: {
[linter_format_1.LintingPrettyPrintContext.Query]: result => `${result.name}-loop at ${range_1.SourceLocation.format(result.loc)} only loops once`,
[linter_format_1.LintingPrettyPrintContext.Full]: result => `${result.name}-loop at ${range_1.SourceLocation.format(result.loc)} only loops once`
},
info: {
name: 'Useless Loops',
description: 'Detect loops which only iterate once',
certainty: linter_format_1.LintingRuleCertainty.BestEffort,
tags: [linter_tags_1.LintingRuleTag.Smell, linter_tags_1.LintingRuleTag.Readability],
defaultConfig: {
loopyFunctions: useless_loop_1.loopyFunctions
}
}
};
//# sourceMappingURL=useless-loop.js.map