@sasjs/lint
Version:
Linting and formatting for SAS code
44 lines • 2.4 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatFile = void 0;
const file_1 = require("@sasjs/utils/file");
const lint_1 = require("../lint");
const getLintConfig_1 = require("../utils/getLintConfig");
const shared_1 = require("./shared");
/**
* Applies automatic formatting to the file at the given path.
* @param {string} filePath - the path to the file to be formatted.
* @param {LintConfig} configuration - an optional configuration. When not passed in, this is read from the .sasjslint file.
* @returns {Promise<FormatResult>} Resolves successfully when the file has been formatted.
*/
const formatFile = (filePath, configuration) => __awaiter(void 0, void 0, void 0, function* () {
const config = configuration || (yield (0, getLintConfig_1.getLintConfig)());
const diagnosticsBeforeFormat = yield (0, lint_1.lintFile)(filePath, config);
const diagnosticsCountBeforeFormat = diagnosticsBeforeFormat.length;
const text = yield (0, file_1.readFile)(filePath);
const formattedText = (0, shared_1.processText)(text, config);
yield (0, file_1.createFile)(filePath, formattedText);
const diagnosticsAfterFormat = yield (0, lint_1.lintFile)(filePath, config);
const diagnosticsCountAfterFormat = diagnosticsAfterFormat.length;
const fixedDiagnosticsCount = diagnosticsCountBeforeFormat - diagnosticsCountAfterFormat;
const updatedFilePaths = [];
if (fixedDiagnosticsCount) {
updatedFilePaths.push(filePath);
}
return {
updatedFilePaths,
fixedDiagnosticsCount,
unfixedDiagnostics: diagnosticsAfterFormat
};
});
exports.formatFile = formatFile;
//# sourceMappingURL=formatFile.js.map