eslint-plugin-sonarjs
Version:
63 lines (62 loc) • 2.56 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isRequiredParserServices = isRequiredParserServices;
exports.getTypeScriptDiagnosticLocation = getTypeScriptDiagnosticLocation;
/*
* 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/
*/
const typescript_1 = __importDefault(require("typescript"));
function isRequiredParserServices(services) {
// see https://github.com/typescript-eslint/typescript-eslint/issues/7124
return !!services?.program;
}
/** Map compiler diagnostics through ESTree, whose locations Vue restores to the SFC. */
function getTypeScriptDiagnosticLocation(sourceCode, services, diagnostic) {
const { file, start, length } = diagnostic;
let originalStart;
if (file.text === sourceCode.text) {
originalStart = start;
}
else {
const expected = file.text.slice(start, start + length);
const visit = (node) => {
const nodeStart = node.getStart(file);
if (nodeStart > start || node.end < start + length) {
return;
}
const original = services.tsNodeToESTreeNodeMap.get(node);
if (original) {
const candidate = original.range[0] + start - nodeStart;
if (sourceCode.text.slice(candidate, candidate + length) === expected) {
originalStart = candidate;
}
}
typescript_1.default.forEachChild(node, visit);
};
visit(file);
}
// Synthetic setup/generic nodes may have no corresponding source location.
if (originalStart === undefined) {
return undefined;
}
return {
start: sourceCode.getLocFromIndex(originalStart),
end: sourceCode.getLocFromIndex(originalStart + length),
};
}