Skip to content

Commit

Permalink
A small refactor (#4837)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-allanson committed Jun 22, 2020
1 parent 32abe3b commit 56a0108
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 39 deletions.
56 changes: 34 additions & 22 deletions lib/getPostcssResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ const LazyResult = require('postcss/lib/lazy-result');
const postcss = require('postcss');
const syntaxes = require('./syntaxes');

/** @typedef {import('postcss').Result} Result */
/** @typedef {import('postcss').Syntax} Syntax */
/** @typedef {import('stylelint').CustomSyntax} CustomSyntax */
/** @typedef {import('stylelint').GetPostcssOptions} GetPostcssOptions */
/** @typedef {import('stylelint').StylelintInternalApi} StylelintInternalApi */
/** @typedef {{parse: any, stringify: any}} Syntax */

const postcssProcessor = postcss();

/**
* @param {StylelintInternalApi} stylelint
* @param {import('stylelint').GetPostcssOptions} options
* @param {GetPostcssOptions} options
*
* @returns {Promise<import('postcss').Result>}
* @returns {Promise<Result>}
*/
module.exports = function (stylelint, options = {}) {
const cached = options.filePath ? stylelint._postcssResultCache.get(options.filePath) : undefined;
Expand All @@ -40,25 +43,7 @@ module.exports = function (stylelint, options = {}) {
let syntax = null;

if (stylelint._options.customSyntax) {
try {
// TODO TYPES determine which type has customSyntax
const customSyntax = /** @type {any} */ require(stylelint._options.customSyntax);

/*
* PostCSS allows for syntaxes that only contain a parser, however,
* it then expects the syntax to be set as the `parser` option rather than `syntax`.
*/
if (!customSyntax.parse) {
syntax = {
parse: customSyntax,
stringify: postcss.stringify,
};
} else {
syntax = customSyntax;
}
} catch (e) {
throw new Error(`Cannot resolve custom syntax module ${stylelint._options.customSyntax}`);
}
syntax = getCustomSyntax(stylelint._options.customSyntax);
} else if (stylelint._options.syntax) {
if (stylelint._options.syntax === 'css') {
syntax = cssSyntax(stylelint);
Expand Down Expand Up @@ -125,6 +110,33 @@ module.exports = function (stylelint, options = {}) {
});
};

/**
* @param {CustomSyntax} customSyntax
* @returns {Syntax}
*/
function getCustomSyntax(customSyntax) {
let resolved;

try {
resolved = require(customSyntax);
} catch (error) {
throw new Error(`Cannot resolve custom syntax module ${customSyntax}`);
}

/*
* PostCSS allows for syntaxes that only contain a parser, however,
* it then expects the syntax to be set as the `parse` option.
*/
if (!resolved.parse) {
resolved = {
parse: resolved,
stringify: postcss.stringify,
};
}

return resolved;
}

/**
* @param {string} filePath
* @returns {Promise<string>}
Expand Down
44 changes: 30 additions & 14 deletions lib/standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,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 @@ -79,20 +80,10 @@ module.exports = function (options) {
/** @type {Formatter} */
let formatterFunction;

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

if (formatterFunction === undefined) {
return Promise.reject(
new Error(
`You must use a valid formatter option: ${getFormatterOptionsText()} or a function`,
),
);
}
} else if (typeof formatter === 'function') {
formatterFunction = formatter;
} else {
formatterFunction = formatters.json;
try {
formatterFunction = getFormatterFunction(formatter);
} catch (error) {
return Promise.reject(error);
}

const stylelint = createStylelint({
Expand Down Expand Up @@ -288,6 +279,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
8 changes: 5 additions & 3 deletions types/stylelint/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ declare module 'stylelint' {

export type FormatterIdentifier = 'compact' | 'json' | 'string' | 'unix' | 'verbose' | Formatter;

export type CustomSyntax = string;

export type StylelintOptions = {
config?: StylelintConfig;
configFile?: string;
Expand All @@ -100,7 +102,7 @@ declare module 'stylelint' {
reportInvalidScopeDisables?: boolean;
reportNeedlessDisables?: boolean;
syntax?: string;
customSyntax?: string;
customSyntax?: CustomSyntax;
fix?: boolean;
};

Expand All @@ -110,7 +112,7 @@ declare module 'stylelint' {
filePath?: string;
codeProcessors?: Array<Function>;
syntax?: string;
customSyntax?: string;
customSyntax?: CustomSyntax;
};

export type GetLintSourceOptions = GetPostcssOptions & { existingPostcssResult?: Result };
Expand Down Expand Up @@ -158,7 +160,7 @@ declare module 'stylelint' {
reportInvalidScopeDisables?: boolean;
maxWarnings?: number;
syntax?: string;
customSyntax?: string;
customSyntax?: CustomSyntax;
formatter?: FormatterIdentifier;
disableDefaultIgnores?: boolean;
fix?: boolean;
Expand Down

0 comments on commit 56a0108

Please sign in to comment.