123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.getFirstToken = exports.getFirstTokenAfter = exports.toSecondaryLocation = exports.issueLocation = exports.report = exports.getMainFunctionTokenLocation = void 0;
- function getMainFunctionTokenLocation(fn, parent, context) {
- let location;
- if (fn.type === 'FunctionDeclaration') {
-
- if (fn.id) {
- location = fn.id.loc;
- }
- else {
- const token = getTokenByValue(fn, 'function', context);
- location = token && token.loc;
- }
- }
- else if (fn.type === 'FunctionExpression') {
- if (parent && (parent.type === 'MethodDefinition' || parent.type === 'Property')) {
- location = parent.key.loc;
- }
- else {
- const token = getTokenByValue(fn, 'function', context);
- location = token && token.loc;
- }
- }
- else if (fn.type === 'ArrowFunctionExpression') {
- const token = context
- .getSourceCode()
- .getTokensBefore(fn.body)
- .reverse()
- .find(token => token.value === '=>');
- location = token && token.loc;
- }
- return location;
- }
- exports.getMainFunctionTokenLocation = getMainFunctionTokenLocation;
- function report(context, reportDescriptor, secondaryLocations, message, cost) {
- if (context.options[context.options.length - 1] !== 'sonar-runtime') {
- context.report(reportDescriptor);
- return;
- }
- const encodedMessage = {
- secondaryLocations,
- message: expandMessage(message, reportDescriptor.data),
- cost,
- };
- reportDescriptor.messageId = 'sonarRuntime';
- if (reportDescriptor.data === undefined) {
- reportDescriptor.data = {};
- }
- reportDescriptor.data.sonarRuntimeData =
- JSON.stringify(encodedMessage);
- context.report(reportDescriptor);
- }
- exports.report = report;
- function expandMessage(message, reportDescriptorData) {
- let expandedMessage = message;
- if (reportDescriptorData !== undefined) {
- for (const [key, value] of Object.entries(reportDescriptorData)) {
- expandedMessage = replaceAll(expandedMessage, `{{${key}}}`, value.toString());
- }
- }
- return expandedMessage;
- }
- function replaceAll(target, search, replacement) {
- return target.split(search).join(replacement);
- }
- function issueLocation(startLoc, endLoc = startLoc, message = '', data = {}) {
- const issueLocation = {
- line: startLoc.start.line,
- column: startLoc.start.column,
- endLine: endLoc.end.line,
- endColumn: endLoc.end.column,
- message,
- };
- if (data !== undefined && Object.keys(data).length > 0) {
- issueLocation.data = data;
- }
- return issueLocation;
- }
- exports.issueLocation = issueLocation;
- function toSecondaryLocation(locationHolder, message) {
- const { loc } = locationHolder;
- return {
- message,
- column: loc.start.column,
- line: loc.start.line,
- endColumn: loc.end.column,
- endLine: loc.end.line,
- };
- }
- exports.toSecondaryLocation = toSecondaryLocation;
- function getTokenByValue(node, value, context) {
- return context
- .getSourceCode()
- .getTokens(node)
- .find(token => token.value === value);
- }
- function getFirstTokenAfter(node, context) {
- return context.getSourceCode().getTokenAfter(node);
- }
- exports.getFirstTokenAfter = getFirstTokenAfter;
- function getFirstToken(node, context) {
- return context.getSourceCode().getTokens(node)[0];
- }
- exports.getFirstToken = getFirstToken;
|