|
| 1 | +import { readFileSync } from "fs"; |
| 2 | +import { applyTSLintAllFixes } from "./apply-fixes"; |
| 3 | +import { requireModule, getModulePath, getPrettierConfig } from "./utils"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Formats the text with prettier and then eslint based on the given options |
| 7 | + * @param {String} options.filePath - the path of the file being formatted |
| 8 | + * can be used in leu of `eslintConfig` (eslint will be used to find the |
| 9 | + * relevant config for the file). Will also be used to load the `text` if |
| 10 | + * `text` is not provided. |
| 11 | + * @param {String} options.text - the text (TypeScript code) to format |
| 12 | + * @param {String} options.tslintPath - the path to the tslint module to use. |
| 13 | + * Will default to require.resolve('tslint') |
| 14 | + * @param {String} options.prettierPath - the path to the prettier module. |
| 15 | + * Will default to require.resovlve('prettier') |
| 16 | + * @param {Object} options.tslintConfig - the config to use for formatting |
| 17 | + * with TSLint. |
| 18 | + * @param {Object} options.prettierOptions - the options to pass for |
| 19 | + * formatting with `prettier`. If not provided, prettier-eslint will attempt |
| 20 | + * to create the options based on the eslintConfig |
| 21 | + * @param {Object} options.fallbackPrettierOptions - the options to pass for |
| 22 | + * formatting with `prettier` if the given option is not inferrable from the |
| 23 | + * eslintConfig. |
| 24 | + * @param {Boolean} options.prettierLast - Run Prettier Last |
| 25 | + * @return {String} - the formatted string |
| 26 | + */ |
| 27 | +export default function format(options) { |
| 28 | + const { |
| 29 | + filePath, |
| 30 | + text = readFileSync(filePath, "utf8"), |
| 31 | + tslintPath = getModulePath(filePath, "tslint"), |
| 32 | + prettierPath = getModulePath(filePath, "prettier"), |
| 33 | + prettierLast, |
| 34 | + fallbackPrettierOptions, |
| 35 | + } = options; |
| 36 | + |
| 37 | + const tslintConfig = Object.assign( |
| 38 | + {}, |
| 39 | + options.tslintConfig, |
| 40 | + getTSLintConfig(filePath, tslintPath) |
| 41 | + ); |
| 42 | + |
| 43 | + const prettierOptions = Object.assign( |
| 44 | + {}, |
| 45 | + filePath && { filepath: filePath }, |
| 46 | + getPrettierConfig(filePath), |
| 47 | + options.prettierOptions |
| 48 | + ); |
| 49 | + |
| 50 | + const prettify = createPrettify( |
| 51 | + prettierOptions || fallbackPrettierOptions || {}, |
| 52 | + prettierPath |
| 53 | + ); |
| 54 | + const tslintFix = createTSLintFix(tslintConfig, tslintPath); |
| 55 | + |
| 56 | + if (prettierLast) { |
| 57 | + return prettify(tslintFix(text, filePath)); |
| 58 | + } |
| 59 | + return tslintFix(prettify(text), filePath); |
| 60 | +} |
| 61 | + |
| 62 | +function createPrettify(formatOptions, prettierPath) { |
| 63 | + return function prettify(text) { |
| 64 | + const prettier = requireModule(prettierPath); |
| 65 | + try { |
| 66 | + const output = prettier.format(text, formatOptions); |
| 67 | + return output; |
| 68 | + } catch (error) { |
| 69 | + throw error; |
| 70 | + } |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +function createTSLintFix(tslintConfig, tslintPath) { |
| 75 | + return function tslintFix(text, filePath) { |
| 76 | + const tslint = requireModule(tslintPath); |
| 77 | + try { |
| 78 | + const linter = new tslint.Linter({ |
| 79 | + fix: false, |
| 80 | + formatter: "json", |
| 81 | + }); |
| 82 | + |
| 83 | + linter.lint(filePath, text, tslintConfig); |
| 84 | + return applyTSLintAllFixes(linter.getResult(), text, tslint); |
| 85 | + } catch (error) { |
| 86 | + throw error; |
| 87 | + } |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +function getTSLintConfig(filePath, tslintPath) { |
| 92 | + const tslint = requireModule(tslintPath); |
| 93 | + try { |
| 94 | + return tslint.Configuration.findConfiguration(null, filePath).results; |
| 95 | + } catch (error) { |
| 96 | + return { rules: {} }; |
| 97 | + } |
| 98 | +} |
0 commit comments