3 Object.defineProperty(exports, "__esModule", {
6 exports.default = format;
8 var _path = require("path");
10 var _fs = require("fs");
12 var _utils = require("./utils");
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
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
33 * @param {Boolean} options.prettierLast - Run Prettier Last
34 * @return {String} - the formatted string
36 function format(options) {
37 var filePath = options.filePath;
40 var tslintFix = createTSLintFix(options.tslintConfig, options.tslintPath || (0, _utils.getModulePath)(filePath, "tslint"));
42 var prettify = createPrettify(options.prettierOptions || options.fallbackPrettierOptions || {}, options.prettierPath || (0, _utils.getModulePath)(filePath, "prettier"));
44 var text = options.text || (0, _fs.readFileSync)(filePath, "utf8");
45 return options.prettierLast ? prettify(tslintFix(text, filePath), filePath) : tslintFix(prettify(text, filePath), filePath);
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 }));
55 function createTSLintFix(defaultLintConfig, tslintPath) {
56 var tslint = (0, _utils.requireModule)(tslintPath);
57 var findConfiguration = tslint.Configuration.findConfiguration;
59 // Adapted from: https://github.com/palantir/tslint/blob/5.12.0/src/linter.ts
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.
69 var lintConfig = Object.assign({}, defaultLintConfig, findConfiguration(null, filePath).results);
71 linter.lint(filePath, text, lintConfig);
73 var _linter$getResult = linter.getResult(),
74 failures = _linter$getResult.failures;
76 if (!failures.length) {
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");
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;
89 var hasFix = function hasFix(f) {
90 return f.hasFix() && f.getRuleName() === ruleName;
92 if (failures.some(hasFix)) {
93 var sourceFile = tslint.getSourceFile(filePath, text);
94 var fixableFailures = tslint.removeDisabledFailures(sourceFile, rule.apply(sourceFile)).filter(function (f) {
98 if (fixableFailures.length) {
99 var fixes = fixableFailures.map(function (f) {
102 return tslint.Replacement.applyFixes(text, fixes);