UNPKG

@sasjs/lint

Version:

Linting and formatting for SAS code

147 lines 7.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LintConfig = void 0; const file_1 = require("../rules/file"); const line_1 = require("../rules/line"); const path_1 = require("../rules/path"); const LineEndings_1 = require("./LineEndings"); const utils_1 = require("../utils"); const Severity_1 = require("./Severity"); /** * LintConfig is the logical representation of the .sasjslint file. * It exposes two sets of rules - one to be run against each line in a file, * and one to be run once per file. * * More types of rules, when available, will be added here. */ class LintConfig { constructor(json) { this.ignoreList = []; this.allowedGremlins = []; this.lineLintRules = []; this.fileLintRules = []; this.pathLintRules = []; this.maxLineLength = 80; this.maxHeaderLineLength = 80; this.maxDataLineLength = 80; this.indentationMultiple = 2; this.lineEndings = LineEndings_1.LineEndings.LF; this.defaultHeader = (0, utils_1.getDefaultHeader)(); this.severityLevel = {}; this.requiredMacroOptions = []; if (json === null || json === void 0 ? void 0 : json.ignoreList) { if (Array.isArray(json.ignoreList)) { json.ignoreList.forEach((item) => { if (typeof item === 'string') this.ignoreList.push(item); else throw new Error(`Property "ignoreList" has invalid type of values. It can contain only strings.`); }); } else { throw new Error(`Property "ignoreList" can only be an array of strings`); } } if ((json === null || json === void 0 ? void 0 : json.noTrailingSpaces) !== false) { this.lineLintRules.push(line_1.noTrailingSpaces); } if ((json === null || json === void 0 ? void 0 : json.noEncodedPasswords) !== false) { this.lineLintRules.push(line_1.noEncodedPasswords); } this.lineLintRules.push(line_1.noTabs); if ((json === null || json === void 0 ? void 0 : json.noTabs) === false || (json === null || json === void 0 ? void 0 : json.noTabIndentation) === false) { this.lineLintRules.pop(); } if ((json === null || json === void 0 ? void 0 : json.maxLineLength) > 0) { this.lineLintRules.push(line_1.maxLineLength); this.maxLineLength = json.maxLineLength; if (!isNaN(json === null || json === void 0 ? void 0 : json.maxHeaderLineLength)) { this.maxHeaderLineLength = json.maxHeaderLineLength; } if (!isNaN(json === null || json === void 0 ? void 0 : json.maxDataLineLength)) { this.maxDataLineLength = json.maxDataLineLength; } } if ((json === null || json === void 0 ? void 0 : json.lineEndings) && json.lineEndings !== LineEndings_1.LineEndings.OFF) { if (json.lineEndings !== LineEndings_1.LineEndings.LF && json.lineEndings !== LineEndings_1.LineEndings.CRLF) { throw new Error(`Invalid value for lineEndings: can be ${LineEndings_1.LineEndings.LF} or ${LineEndings_1.LineEndings.CRLF}`); } this.fileLintRules.push(file_1.lineEndings); this.lineEndings = json.lineEndings; } this.lineLintRules.push(line_1.indentationMultiple); if (!isNaN(json === null || json === void 0 ? void 0 : json.indentationMultiple)) { this.indentationMultiple = json.indentationMultiple; } if ((json === null || json === void 0 ? void 0 : json.hasDoxygenHeader) !== false) { this.fileLintRules.push(file_1.hasDoxygenHeader); } if (json === null || json === void 0 ? void 0 : json.defaultHeader) { this.defaultHeader = json.defaultHeader; } if ((json === null || json === void 0 ? void 0 : json.noSpacesInFileNames) !== false) { this.pathLintRules.push(path_1.noSpacesInFileNames); } if ((json === null || json === void 0 ? void 0 : json.lowerCaseFileNames) !== false) { this.pathLintRules.push(path_1.lowerCaseFileNames); } if (json === null || json === void 0 ? void 0 : json.hasMacroNameInMend) { this.fileLintRules.push(file_1.hasMacroNameInMend); } if ((json === null || json === void 0 ? void 0 : json.noNestedMacros) !== false) { this.fileLintRules.push(file_1.noNestedMacros); } if ((json === null || json === void 0 ? void 0 : json.hasMacroParentheses) !== false) { this.fileLintRules.push(file_1.hasMacroParentheses); } if ((json === null || json === void 0 ? void 0 : json.strictMacroDefinition) !== false) { this.fileLintRules.push(file_1.strictMacroDefinition); } if (json === null || json === void 0 ? void 0 : json.hasRequiredMacroOptions) { this.fileLintRules.push(file_1.hasRequiredMacroOptions); if (json === null || json === void 0 ? void 0 : json.requiredMacroOptions) { if (Array.isArray(json.requiredMacroOptions) && json.requiredMacroOptions.length > 0) { json.requiredMacroOptions.forEach((item) => { if (typeof item === 'string') { this.requiredMacroOptions.push(item); } else { throw new Error(`Property "requiredMacroOptions" has invalid type of values. It can only contain strings.`); } }); } else { throw new Error(`Property "requiredMacroOptions" can only be an array of strings.`); } } } if ((json === null || json === void 0 ? void 0 : json.noGremlins) !== false) { this.lineLintRules.push(line_1.noGremlins); if (json === null || json === void 0 ? void 0 : json.allowedGremlins) { if (Array.isArray(json.allowedGremlins)) { json.allowedGremlins.forEach((item) => { if (typeof item === 'string' && /^0x[0-9a-f]{4}$/i.test(item)) this.allowedGremlins.push(item); else throw new Error(`Property "allowedGremlins" has invalid type of values. It can contain only strings of form hexcode like '["0x0080", "0x3000"]'`); }); } else { throw new Error(`Property "allowedGremlins" can only be an array of strings of form hexcode like '["0x0080", "0x3000"]'`); } } } if (json === null || json === void 0 ? void 0 : json.severityLevel) { for (const [rule, severity] of Object.entries(json.severityLevel)) { if (severity === 'warn') this.severityLevel[rule] = Severity_1.Severity.Warning; if (severity === 'error') this.severityLevel[rule] = Severity_1.Severity.Error; } } } } exports.LintConfig = LintConfig; //# sourceMappingURL=LintConfig.js.map