.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / prettier-tslint / dist / format.js
1 "use strict";
2
3 Object.defineProperty(exports, "__esModule", {
4   value: true
5 });
6 exports.default = format;
7
8 var _path = require("path");
9
10 var _fs = require("fs");
11
12 var _utils = require("./utils");
13
14 /**
15  * Formats the text with prettier and then eslint based on the given options
16  * @param {String} options.filePath - the path of the file being formatted
17  *  can be used in leu of `eslintConfig` (eslint will be used to find the
18  *  relevant config for the file). Will also be used to load the `text` if
19  *  `text` is not provided.
20  * @param {String} options.text - the text (TypeScript code) to format
21  * @param {String} options.tslintPath - the path to the tslint module to use.
22  *   Will default to require.resolve('tslint')
23  * @param {String} options.prettierPath - the path to the prettier module.
24  *   Will default to require.resovlve('prettier')
25  * @param {Object} options.tslintConfig - the config to use for formatting
26  *  with TSLint.
27  * @param {Object} options.prettierOptions - the options to pass for
28  *  formatting with `prettier`. If not provided, prettier-eslint will attempt
29  *  to create the options based on the eslintConfig
30  * @param {Object} options.fallbackPrettierOptions - the options to pass for
31  *  formatting with `prettier` if the given option is not inferrable from the
32  *  eslintConfig.
33  * @param {Boolean} options.prettierLast - Run Prettier Last
34  * @return {String} - the formatted string
35  */
36 function format(options) {
37   var filePath = options.filePath;
38
39
40   var tslintFix = createTSLintFix(options.tslintConfig, options.tslintPath || (0, _utils.getModulePath)(filePath, "tslint"));
41
42   var prettify = createPrettify(options.prettierOptions || options.fallbackPrettierOptions || {}, options.prettierPath || (0, _utils.getModulePath)(filePath, "prettier"));
43
44   var text = options.text || (0, _fs.readFileSync)(filePath, "utf8");
45   return options.prettierLast ? prettify(tslintFix(text, filePath), filePath) : tslintFix(prettify(text, filePath), filePath);
46 }
47
48 function createPrettify(formatOptions, prettierPath) {
49   var prettier = (0, _utils.requireModule)(prettierPath);
50   return function prettify(text, filePath) {
51     return prettier.format(text, Object.assign({}, formatOptions, (0, _utils.getPrettierConfig)(filePath), filePath && { filepath: filePath }));
52   };
53 }
54
55 function createTSLintFix(defaultLintConfig, tslintPath) {
56   var tslint = (0, _utils.requireModule)(tslintPath);
57   var findConfiguration = tslint.Configuration.findConfiguration;
58
59   // Adapted from: https://github.com/palantir/tslint/blob/5.12.0/src/linter.ts
60
61   return function tslintFix(text, filePath) {
62     // TODO: Use the "fix" option of `new tslint.Linter()` once the following
63     // issue is triaged: https://github.com/palantir/tslint/issues/4411
64     var linter = new tslint.Linter({
65       fix: false, // Disabled to avoid file operations.
66       formatter: "json"
67     });
68
69     var lintConfig = Object.assign({}, defaultLintConfig, findConfiguration(null, filePath).results);
70
71     linter.lint(filePath, text, lintConfig);
72
73     var _linter$getResult = linter.getResult(),
74         failures = _linter$getResult.failures;
75
76     if (!failures.length) {
77       return text;
78     }
79
80     // This is a private method, but we're using it as a workaround.
81     var enabledRules = linter.getEnabledRules(lintConfig, (0, _path.extname)(filePath) === ".js");
82
83     // To keep rules from interfering with one another, we apply their fixes one
84     // rule at a time. More info: https://github.com/azz/prettier-tslint/issues/26
85     return enabledRules.reduce(function (text, rule) {
86       var _rule$getOptions = rule.getOptions(),
87           ruleName = _rule$getOptions.ruleName;
88
89       var hasFix = function hasFix(f) {
90         return f.hasFix() && f.getRuleName() === ruleName;
91       };
92       if (failures.some(hasFix)) {
93         var sourceFile = tslint.getSourceFile(filePath, text);
94         var fixableFailures = tslint.removeDisabledFailures(sourceFile, rule.apply(sourceFile)).filter(function (f) {
95           return f.hasFix();
96         });
97
98         if (fixableFailures.length) {
99           var fixes = fixableFailures.map(function (f) {
100             return f.getFix();
101           });
102           return tslint.Replacement.applyFixes(text, fixes);
103         }
104       }
105       return text;
106     }, text);
107   };
108 }