Skip to content

Commit

Permalink
Move getFormatterFunction back into standalone
Browse files Browse the repository at this point in the history
  • Loading branch information
m-allanson committed May 25, 2020
1 parent 8577328 commit 0664f3f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 35 deletions.
34 changes: 0 additions & 34 deletions lib/getFormatterFunction.js

This file was deleted.

29 changes: 28 additions & 1 deletion lib/standalone.js
Expand Up @@ -6,8 +6,9 @@ const createStylelintResult = require('./createStylelintResult');
const debug = require('debug')('stylelint:standalone');
const FileCache = require('./utils/FileCache');
const filterFilePaths = require('./utils/filterFilePaths');
const formatters = require('./formatters');
const fs = require('fs');
const getFormatterFunction = require('./getFormatterFunction');
const getFormatterOptionsText = require('./utils/getFormatterOptionsText');
const globby = require('globby');
const hash = require('./utils/hash');
const NoFilesFoundError = require('./utils/noFilesFoundError');
Expand All @@ -24,6 +25,7 @@ const writeFileAtomic = require('write-file-atomic');
/** @typedef {import('stylelint').StylelintStandaloneReturnValue} StylelintStandaloneReturnValue */
/** @typedef {import('stylelint').StylelintResult} StylelintResult */
/** @typedef {import('stylelint').Formatter} Formatter */
/** @typedef {import('stylelint').FormatterIdentifier} FormatterIdentifier */

/**
* @param {StylelintStandaloneOptions} options
Expand Down Expand Up @@ -284,6 +286,31 @@ module.exports = function (options) {
});
};

/**
* @param {FormatterIdentifier | undefined} selected
* @returns {Formatter}
*/
function getFormatterFunction(selected) {
/** @type {Formatter} */
let formatterFunction;

if (typeof selected === 'string') {
formatterFunction = formatters[selected];

if (formatterFunction === undefined) {
throw new Error(
`You must use a valid formatter option: ${getFormatterOptionsText()} or a function`,
);
}
} else if (typeof selected === 'function') {
formatterFunction = selected;
} else {
formatterFunction = formatters.json;
}

return formatterFunction;
}

/**
* @param {import('stylelint').StylelintInternalApi} stylelint
* @param {any} error
Expand Down

0 comments on commit 0664f3f

Please sign in to comment.