|
| 1 | +import * as ts from "typescript"; |
| 2 | + |
| 3 | +import * as path from "path"; |
| 4 | +import * as fs from "fs"; |
| 5 | + |
| 6 | +import { Options } from "../"; |
| 7 | +import { getConfigFileName, parseJSON } from "../utils"; |
| 8 | + |
| 9 | +// https://code.visualstudio.com/Docs/customization/userandworkspace |
| 10 | +interface VSCodeSettings { |
| 11 | + "typescript.format.insertSpaceAfterCommaDelimiter": boolean; |
| 12 | + "typescript.format.insertSpaceAfterSemicolonInForStatements": boolean; |
| 13 | + "typescript.format.insertSpaceBeforeAndAfterBinaryOperators": boolean; |
| 14 | + "typescript.format.insertSpaceAfterKeywordsInControlFlowStatements": boolean; |
| 15 | + "typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": boolean; |
| 16 | + "typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": boolean; |
| 17 | + "typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": boolean; |
| 18 | + "typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": boolean; |
| 19 | + "typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": boolean; |
| 20 | + "typescript.format.placeOpenBraceOnNewLineForFunctions": boolean; |
| 21 | + "typescript.format.placeOpenBraceOnNewLineForControlBlocks": boolean; |
| 22 | +} |
| 23 | + |
| 24 | +export default function makeFormatCodeOptions(fileName: string, opts: Options, formatSettings: ts.FormatCodeSettings): ts.FormatCodeSettings { |
| 25 | + |
| 26 | + let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName)); |
| 27 | + let configFileName = getConfigFileName(baseDir, "./.vscode/settings.json"); |
| 28 | + if (!configFileName) { |
| 29 | + return formatSettings; |
| 30 | + } |
| 31 | + if (opts.verbose) { |
| 32 | + console.log(`read ${configFileName} for ${fileName}`); |
| 33 | + } |
| 34 | + |
| 35 | + let config: VSCodeSettings = parseJSON(fs.readFileSync(configFileName, "utf-8")); |
| 36 | + if (config["typescript.format.insertSpaceAfterCommaDelimiter"] != null) { |
| 37 | + formatSettings.insertSpaceAfterCommaDelimiter = config["typescript.format.insertSpaceAfterCommaDelimiter"]; |
| 38 | + } |
| 39 | + if (config["typescript.format.insertSpaceAfterSemicolonInForStatements"] != null) { |
| 40 | + formatSettings.insertSpaceAfterSemicolonInForStatements = config["typescript.format.insertSpaceAfterSemicolonInForStatements"]; |
| 41 | + } |
| 42 | + if (config["typescript.format.insertSpaceBeforeAndAfterBinaryOperators"] != null) { |
| 43 | + formatSettings.insertSpaceBeforeAndAfterBinaryOperators = config["typescript.format.insertSpaceBeforeAndAfterBinaryOperators"]; |
| 44 | + } |
| 45 | + if (config["typescript.format.insertSpaceAfterKeywordsInControlFlowStatements"] != null) { |
| 46 | + formatSettings.insertSpaceAfterKeywordsInControlFlowStatements = config["typescript.format.insertSpaceAfterKeywordsInControlFlowStatements"]; |
| 47 | + } |
| 48 | + if (config["typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions"] != null) { |
| 49 | + formatSettings.insertSpaceAfterFunctionKeywordForAnonymousFunctions = config["typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions"]; |
| 50 | + } |
| 51 | + if (config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis"] != null) { |
| 52 | + formatSettings.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis = config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis"]; |
| 53 | + } |
| 54 | + if (config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets"] != null) { |
| 55 | + formatSettings.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets = config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets"]; |
| 56 | + } |
| 57 | + if (config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"] != null) { |
| 58 | + formatSettings.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces = config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"]; |
| 59 | + } |
| 60 | + if (config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces"] != null) { |
| 61 | + formatSettings.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces = config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces"]; |
| 62 | + } |
| 63 | + if (config["typescript.format.placeOpenBraceOnNewLineForFunctions"] != null) { |
| 64 | + formatSettings.placeOpenBraceOnNewLineForFunctions = config["typescript.format.placeOpenBraceOnNewLineForFunctions"]; |
| 65 | + } |
| 66 | + if (config["typescript.format.placeOpenBraceOnNewLineForControlBlocks"] != null) { |
| 67 | + formatSettings.placeOpenBraceOnNewLineForControlBlocks = config["typescript.format.placeOpenBraceOnNewLineForControlBlocks"]; |
| 68 | + } |
| 69 | + |
| 70 | + return formatSettings; |
| 71 | +} |
0 commit comments