Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to improve types for function-* rules #5487

Merged
merged 3 commits into from Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions lib/rules/function-allowed-list/index.js
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const declarationValueIndex = require('../../utils/declarationValueIndex');
Expand All @@ -18,8 +16,9 @@ const messages = ruleMessages(ruleName, {
rejected: (name) => `Unexpected function "${name}"`,
});

function rule(listInput) {
const list = [].concat(listInput);
/** @type {import('stylelint').StylelintRule} */
const rule = (primary) => {
const list = [primary].flat();

return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
Expand Down Expand Up @@ -57,7 +56,7 @@ function rule(listInput) {
});
});
};
}
};

rule.primaryOptionArray = true;

Expand Down
56 changes: 49 additions & 7 deletions lib/rules/function-calc-no-unspaced-operator/index.js
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const balancedMatch = require('balanced-match');
Expand All @@ -18,19 +16,28 @@ const messages = ruleMessages(ruleName, {
expectedOperatorBeforeSign: (operator) => `Expected an operator before sign "${operator}"`,
});

function rule(actual, secondary, context) {
/** @typedef {{ index: number, insert: boolean }} SymbolToFix */

/** @type {import('stylelint').StylelintRule} */
const rule = (primary, _secondaryOptions, context) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, { actual });
const validOptions = validateOptions(result, ruleName, { actual: primary });

if (!validOptions) {
return;
}

/**
* @param {string} message
* @param {import('postcss').Node} node
* @param {number} index
*/
function complain(message, node, index) {
report({ message, node, index, result, ruleName });
}

root.walkDecls((decl) => {
/** @type {SymbolToFix[]} */
const symbolsToFix = [];

valueParser(decl.value).walk((node) => {
Expand All @@ -45,6 +52,10 @@ function rule(actual, secondary, context) {
throw new Error(`No parens match: "${nodeText}"`);
}

if (decl.source == null || decl.source.start == null) {
throw new Error('Declaration source must be present');
}

const rawExpression = parensMatch.body;
const expressionIndex =
decl.source.start.column +
Expand All @@ -53,12 +64,18 @@ function rule(actual, secondary, context) {
node.sourceIndex;
const expression = blurVariables(rawExpression);

const parensMatchStart = parensMatch.start;

checkSymbol('+');
checkSymbol('-');
checkSymbol('*');
checkSymbol('/');

/**
* @param {string} symbol
*/
function checkSymbol(symbol) {
/** @type {import('style-search').Options} */
const styleSearchOptions = {
source: expression,
target: symbol,
Expand All @@ -67,7 +84,7 @@ function rule(actual, secondary, context) {

styleSearch(styleSearchOptions, (match) => {
const index = match.startIndex;
const symbolIndex = node.sourceIndex + parensMatch.start + index + 1;
const symbolIndex = node.sourceIndex + parensMatchStart + index + 1;

// Deal with signs.
// (@ and $ are considered "digits" here to allow for variable syntaxes
Expand Down Expand Up @@ -170,7 +187,7 @@ function rule(actual, secondary, context) {
});

if (context.fix) {
decl.value = symbolsToFix.reduce((fixedValue, { insert, index }) => {
decl.value = symbolsToFix.reduce((/** @type {string} */ fixedValue, { insert, index }) => {
shiftIndexes(symbolsToFix, index, insert);

return insert
Expand All @@ -180,12 +197,21 @@ function rule(actual, secondary, context) {
}
});
};
}
};

/**
* @param {string} str
* @param {number} index
*/
function isNewlineAtIndex(str, index) {
return str[index] === '\n' || str.slice(index, index + 2) === '\r\n';
}

/**
* @param {SymbolToFix[]} symbolsToFix
* @param {number} index
* @param {boolean} insert
*/
function shiftIndexes(symbolsToFix, index, insert) {
symbolsToFix.forEach((symbol) => {
if (symbol.index > index) {
Expand All @@ -194,18 +220,34 @@ function shiftIndexes(symbolsToFix, index, insert) {
});
}

/**
* @param {string} str
* @param {number} index
*/
function removeCharAtIndex(str, index) {
return str.slice(0, index) + str.slice(index + 1, str.length);
}

/**
* @param {string} str
* @param {number} index
* @param {string} char
*/
function insertCharAtIndex(str, index, char) {
return str.slice(0, index) + char + str.slice(index, str.length);
}

/**
* @param {string} source
*/
function blurVariables(source) {
return source.replace(/[$@][^)\s]+|#{.+?}/g, '0');
}

/**
* @param {string} str
* @param {number} startIndex
*/
function newlineBefore(str, startIndex) {
let index = startIndex;

Expand Down
15 changes: 7 additions & 8 deletions lib/rules/function-comma-newline-after/index.js
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const fixer = require('../functionCommaSpaceFix');
Expand All @@ -16,12 +14,13 @@ const messages = ruleMessages(ruleName, {
rejectedAfterMultiLine: () => 'Unexpected whitespace after "," in a multi-line function',
});

function rule(expectation, options, context) {
const checker = whitespaceChecker('newline', expectation, messages);
/** @type {import('stylelint').StylelintRule} */
const rule = (primary, _secondaryOptions, context) => {
const checker = whitespaceChecker('newline', primary, messages);

return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
actual: primary,
possible: ['always', 'always-multi-line', 'never-multi-line'],
});

Expand All @@ -40,15 +39,15 @@ function rule(expectation, options, context) {
div,
index,
nodes,
expectation,
expectation: primary,
position: 'after',
symb: context.newline,
symb: context.newline || '',
});
}
: null,
});
};
}
};

rule.ruleName = ruleName;
rule.messages = messages;
Expand Down
15 changes: 7 additions & 8 deletions lib/rules/function-comma-newline-before/index.js
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const fixer = require('../functionCommaSpaceFix');
Expand All @@ -16,12 +14,13 @@ const messages = ruleMessages(ruleName, {
rejectedBeforeMultiLine: () => 'Unexpected whitespace before "," in a multi-line function',
});

function rule(expectation, options, context) {
const checker = whitespaceChecker('newline', expectation, messages);
/** @type {import('stylelint').StylelintRule} */
const rule = (primary, _secondaryOptions, context) => {
const checker = whitespaceChecker('newline', primary, messages);

return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
actual: primary,
possible: ['always', 'always-multi-line', 'never-multi-line'],
});

Expand All @@ -40,15 +39,15 @@ function rule(expectation, options, context) {
div,
index,
nodes,
expectation,
expectation: primary,
position: 'before',
symb: context.newline,
symb: context.newline || '',
});
}
: null,
});
};
}
};

rule.ruleName = ruleName;
rule.messages = messages;
Expand Down
13 changes: 6 additions & 7 deletions lib/rules/function-comma-space-after/index.js
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const fixer = require('../functionCommaSpaceFix');
Expand All @@ -17,12 +15,13 @@ const messages = ruleMessages(ruleName, {
rejectedAfterSingleLine: () => 'Unexpected whitespace after "," in a single-line function',
});

function rule(expectation, options, context) {
const checker = whitespaceChecker('space', expectation, messages);
/** @type {import('stylelint').StylelintRule} */
const rule = (primary, _secondaryOptions, context) => {
const checker = whitespaceChecker('space', primary, messages);

return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
actual: primary,
possible: ['always', 'never', 'always-single-line', 'never-single-line'],
});

Expand All @@ -41,15 +40,15 @@ function rule(expectation, options, context) {
div,
index,
nodes,
expectation,
expectation: primary,
position: 'after',
symb: ' ',
});
}
: null,
});
};
}
};

rule.ruleName = ruleName;
rule.messages = messages;
Expand Down
13 changes: 6 additions & 7 deletions lib/rules/function-comma-space-before/index.js
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const fixer = require('../functionCommaSpaceFix');
Expand All @@ -17,12 +15,13 @@ const messages = ruleMessages(ruleName, {
rejectedBeforeSingleLine: () => 'Unexpected whitespace before "," in a single-line function',
});

function rule(expectation, options, context) {
const checker = whitespaceChecker('space', expectation, messages);
/** @type {import('stylelint').StylelintRule} */
const rule = (primary, _secondaryOptions, context) => {
const checker = whitespaceChecker('space', primary, messages);

return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
actual: primary,
possible: ['always', 'never', 'always-single-line', 'never-single-line'],
});

Expand All @@ -41,15 +40,15 @@ function rule(expectation, options, context) {
div,
index,
nodes,
expectation,
expectation: primary,
position: 'before',
symb: ' ',
});
}
: null,
});
};
}
};

rule.ruleName = ruleName;
rule.messages = messages;
Expand Down
11 changes: 5 additions & 6 deletions lib/rules/function-disallowed-list/index.js
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const declarationValueIndex = require('../../utils/declarationValueIndex');
Expand All @@ -18,10 +16,11 @@ const messages = ruleMessages(ruleName, {
rejected: (name) => `Unexpected function "${name}"`,
});

function rule(list) {
/** @type {import('stylelint').StylelintRule} */
const rule = (primary) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: list,
actual: primary,
possible: [isString, isRegExp],
});

Expand All @@ -41,7 +40,7 @@ function rule(list) {
return;
}

if (!matchesStringOrRegExp(vendor.unprefixed(node.value), list)) {
if (!matchesStringOrRegExp(vendor.unprefixed(node.value), primary)) {
return;
}

Expand All @@ -55,7 +54,7 @@ function rule(list) {
});
});
};
}
};

rule.primaryOptionArray = true;

Expand Down