typescript-formatter
Version:
Formatter of TypeScript code
78 lines • 3.29 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var ts = require("typescript");
var fs = require("fs");
var path = require("path");
var TSCONFIG_CACHE = {};
function createDefaultFormatCodeSettings() {
return {
baseIndentSize: 0,
indentSize: 4,
tabSize: 4,
indentStyle: ts.IndentStyle.Smart,
newLineCharacter: "\r\n",
convertTabsToSpaces: true,
insertSpaceAfterCommaDelimiter: true,
insertSpaceAfterSemicolonInForStatements: true,
insertSpaceBeforeAndAfterBinaryOperators: true,
insertSpaceAfterConstructor: false,
insertSpaceAfterKeywordsInControlFlowStatements: true,
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
insertSpaceAfterTypeAssertion: false,
insertSpaceBeforeFunctionParenthesis: false,
placeOpenBraceOnNewLineForFunctions: false,
placeOpenBraceOnNewLineForControlBlocks: false,
insertSpaceBeforeTypeAnnotation: false,
};
}
exports.createDefaultFormatCodeSettings = createDefaultFormatCodeSettings;
function getConfigFileName(baseDir, configFileName) {
var configFilePath = path.resolve(baseDir, configFileName);
if (fs.existsSync(configFilePath)) {
return configFilePath;
}
if (baseDir.length === path.dirname(baseDir).length) {
return null;
}
return getConfigFileName(path.resolve(baseDir, "../"), configFileName);
}
exports.getConfigFileName = getConfigFileName;
function readFilesFromTsconfig(configPath) {
return readTsconfig(configPath).fileNames;
}
exports.readFilesFromTsconfig = readFilesFromTsconfig;
function readTsconfig(configPath) {
if (TSCONFIG_CACHE[configPath]) {
return TSCONFIG_CACHE[configPath];
}
// for `extends` support. It supported from TypeScript 2.1.1.
// `& { readFile(path: string): string; }` is backword compat for TypeScript compiler 2.0.3 support.
var host = {
useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
readDirectory: ts.sys.readDirectory,
fileExists: function (path) { return fs.existsSync(path); },
readFile: function (path) { return fs.readFileSync(path, "utf-8"); },
};
var rootConfig = parseJSON(fs.readFileSync(configPath, "utf-8"));
var parsed = ts.parseJsonConfigFileContent(rootConfig, host, path.dirname(configPath));
if (parsed.errors && parsed.errors.length !== 0) {
throw new Error(parsed.errors.map(function (e) { return e.messageText; }).join("\n"));
}
TSCONFIG_CACHE[configPath] = parsed;
return parsed;
}
exports.readTsconfig = readTsconfig;
function parseJSON(jsonText) {
var result = ts.parseConfigFileTextToJson("tmp.json", jsonText);
if (result.error) {
throw new Error("JSON parse error");
}
return result.config;
}
exports.parseJSON = parseJSON;
//# sourceMappingURL=utils.js.map
;