@eagleoutice/flowr
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
68 lines • 3.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEAD_CODE = void 0;
const linter_format_1 = require("../linter-format");
const range_1 = require("../../util/range");
const flowr_search_builder_1 = require("../../search/flowr-search-builder");
const linter_tags_1 = require("../linter-tags");
const search_enrichers_1 = require("../../search/search-executor/search-enrichers");
const assert_1 = require("../../util/assert");
const cfg_simplification_1 = require("../../control-flow/cfg-simplification");
exports.DEAD_CODE = {
createSearch: (config) => flowr_search_builder_1.Q.all().with(search_enrichers_1.Enrichment.CfgInformation, {
checkReachable: true,
simplificationPasses: config.simplificationPasses ?? [...cfg_simplification_1.DefaultCfgSimplificationOrder, 'analyze-dead-code']
}),
processSearchResult: (elements, _config, _data) => {
const meta = {
consideredNodes: 0
};
return {
results: combineResults(elements.getElements()
.filter(element => {
meta.consideredNodes++;
const cfgInformation = (0, search_enrichers_1.enrichmentContent)(element, search_enrichers_1.Enrichment.CfgInformation);
return element.node.info.role !== "el-g" /* RoleInParent.ExpressionListGrouping */ && !cfgInformation.isReachable;
})
.map(element => ({
certainty: linter_format_1.LintingResultCertainty.Certain,
involvedId: element.node.info.id,
loc: range_1.SourceLocation.fromNode(element.node)
}))
.filter(element => (0, assert_1.isNotUndefined)(element.loc))),
'.meta': meta
};
},
prettyPrint: {
[linter_format_1.LintingPrettyPrintContext.Query]: result => `Code at ${range_1.SourceLocation.format(result.loc)}`,
[linter_format_1.LintingPrettyPrintContext.Full]: result => `Code at ${range_1.SourceLocation.format(result.loc)} can never be executed`,
},
info: {
name: 'Dead Code',
tags: [linter_tags_1.LintingRuleTag.Smell, linter_tags_1.LintingRuleTag.Usability, linter_tags_1.LintingRuleTag.Reproducibility],
// our limited dead code analysis causes complex cases of dead code not to be included in the linting result, but deadness is properly investigated for returned results
certainty: linter_format_1.LintingRuleCertainty.BestEffort,
description: 'Marks areas of code that are never reached during execution.',
defaultConfig: {}
}
};
function combineResults(results) {
for (let i = results.length - 1; i >= 0; i--) {
const result = results[i];
const other = results.find(other => result !== other && range_1.SourceLocation.isSubsetOf(result.loc, other.loc));
if (other !== undefined) {
if (!Array.isArray(other.involvedId)) {
other.involvedId = other.involvedId !== undefined ? [other.involvedId] : [];
}
if (Array.isArray(result.involvedId)) {
other.involvedId.push(...result.involvedId);
}
else if (result.involvedId !== undefined) {
other.involvedId.push(result.involvedId);
}
results.splice(i, 1);
}
}
return results;
}
//# sourceMappingURL=dead-code.js.map