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 block-* rules #5426

Merged
merged 1 commit into from
Jul 27, 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
30 changes: 17 additions & 13 deletions lib/rules/block-closing-brace-empty-line-before/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const addEmptyLineAfter = require('../../utils/addEmptyLineAfter');
Expand All @@ -21,17 +19,18 @@ const messages = ruleMessages(ruleName, {
rejected: 'Unexpected empty line before closing brace',
});

function rule(expectation, options, context) {
/** @type {import('stylelint').StylelintRule} */
const rule = (primary, secondaryOptions, context) => {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{
actual: expectation,
actual: primary,
possible: ['always-multi-line', 'never'],
},
{
actual: options,
actual: secondaryOptions,
possible: {
except: ['after-closing-brace'],
},
Expand All @@ -47,6 +46,9 @@ function rule(expectation, options, context) {
root.walkRules(check);
root.walkAtRules(check);

/**
* @param {import('postcss').Rule | import('postcss').AtRule} statement
*/
function check(statement) {
// Return early if blockless or has empty block
if (!hasBlock(statement) || hasEmptyBlock(statement)) {
Expand All @@ -70,16 +72,14 @@ function rule(expectation, options, context) {

// Reverse the primary options if `after-closing-brace` is set
if (
optionsMatches(options, 'except', 'after-closing-brace') &&
optionsMatches(secondaryOptions, 'except', 'after-closing-brace') &&
statement.type === 'atrule' &&
!childNodeTypes.includes('decl')
) {
return expectation === 'never';
return primary === 'never';
}

return Boolean(
expectation === 'always-multi-line' && !isSingleLineString(blockString(statement)),
);
return primary === 'always-multi-line' && !isSingleLineString(blockString(statement));
})();

// Check for at least one empty line
Expand All @@ -91,10 +91,14 @@ function rule(expectation, options, context) {
}

if (context.fix) {
const { newline } = context;

if (typeof newline !== 'string') return;

if (expectEmptyLineBefore) {
addEmptyLineAfter(statement, context.newline);
addEmptyLineAfter(statement, newline);
} else {
removeEmptyLinesAfter(statement, context.newline);
removeEmptyLinesAfter(statement, newline);
}

return;
Expand All @@ -111,7 +115,7 @@ function rule(expectation, options, context) {
});
}
};
}
};

rule.ruleName = ruleName;
rule.messages = messages;
Expand Down
37 changes: 23 additions & 14 deletions lib/rules/block-closing-brace-newline-after/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const blockString = require('../../utils/blockString');
Expand All @@ -22,15 +20,16 @@ const messages = ruleMessages(ruleName, {
rejectedAfterMultiLine: () => 'Unexpected whitespace after "}" of a multi-line block',
});

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-single-line',
Expand All @@ -40,7 +39,7 @@ function rule(expectation, options, context) {
],
},
{
actual: options,
actual: secondaryOptions,
possible: {
ignoreAtRules: [isString],
},
Expand All @@ -56,12 +55,18 @@ function rule(expectation, options, context) {
root.walkRules(check);
root.walkAtRules(check);

/**
* @param {import('postcss').Rule | import('postcss').AtRule} statement
*/
function check(statement) {
if (!hasBlock(statement)) {
return;
}

if (optionsMatches(options, 'ignoreAtRules', statement.name)) {
if (
statement.type === 'atrule' &&
optionsMatches(secondaryOptions, 'ignoreAtRules', statement.name)
) {
return;
}

Expand Down Expand Up @@ -100,20 +105,24 @@ function rule(expectation, options, context) {
lineCheckStr: blockString(statement),
err: (msg) => {
if (context.fix) {
if (expectation.startsWith('always')) {
const index = nodeToCheck.raws.before.search(/\r?\n/);
const nodeToCheckRaws = nodeToCheck.raws;

if (typeof nodeToCheckRaws.before !== 'string') return;

if (primary.startsWith('always')) {
const index = nodeToCheckRaws.before.search(/\r?\n/);

if (index >= 0) {
nodeToCheck.raws.before = nodeToCheck.raws.before.slice(index);
nodeToCheckRaws.before = nodeToCheckRaws.before.slice(index);
} else {
nodeToCheck.raws.before = context.newline + nodeToCheck.raws.before;
nodeToCheckRaws.before = context.newline + nodeToCheckRaws.before;
}

return;
}

if (expectation.startsWith('never')) {
nodeToCheck.raws.before = '';
if (primary.startsWith('never')) {
nodeToCheckRaws.before = '';

return;
}
Expand All @@ -130,7 +139,7 @@ function rule(expectation, options, context) {
});
}
};
}
};

rule.ruleName = ruleName;
rule.messages = messages;
Expand Down
43 changes: 26 additions & 17 deletions lib/rules/block-closing-brace-newline-before/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const blockString = require('../../utils/blockString');
Expand All @@ -18,10 +16,11 @@ const messages = ruleMessages(ruleName, {
rejectedBeforeMultiLine: 'Unexpected whitespace before "}" of a multi-line block',
});

function rule(expectation, options, context) {
/** @type {import('stylelint').StylelintRule} */
const rule = (primary, _secondaryOptions, context) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
actual: primary,
possible: ['always', 'always-multi-line', 'never-multi-line'],
});

Expand All @@ -33,6 +32,9 @@ function rule(expectation, options, context) {
root.walkRules(check);
root.walkAtRules(check);

/**
* @param {import('postcss').Rule | import('postcss').AtRule} statement
*/
function check(statement) {
// Return early if blockless or has empty block
if (!hasBlock(statement) || hasEmptyBlock(statement)) {
Expand Down Expand Up @@ -61,40 +63,47 @@ function rule(expectation, options, context) {
// ignore any other whitespace between them, because that
// will be checked by the indentation rule.
if (!after.startsWith('\n') && !after.startsWith('\r\n')) {
if (expectation === 'always') {
if (primary === 'always') {
complain(messages.expectedBefore);
} else if (blockIsMultiLine && expectation === 'always-multi-line') {
} else if (blockIsMultiLine && primary === 'always-multi-line') {
complain(messages.expectedBeforeMultiLine);
}
}

if (after !== '' && blockIsMultiLine && expectation === 'never-multi-line') {
if (after !== '' && blockIsMultiLine && primary === 'never-multi-line') {
complain(messages.rejectedBeforeMultiLine);
}

/**
* @param {string} message
*/
function complain(message) {
if (context.fix) {
if (expectation.startsWith('always')) {
const firstWhitespaceIndex = statement.raws.after.search(/\s/);
const statementRaws = statement.raws;

if (typeof statementRaws.after !== 'string') return;

if (primary.startsWith('always')) {
const firstWhitespaceIndex = statementRaws.after.search(/\s/);
const newlineBefore =
firstWhitespaceIndex >= 0
? statement.raws.after.slice(0, firstWhitespaceIndex)
: statement.raws.after;
? statementRaws.after.slice(0, firstWhitespaceIndex)
: statementRaws.after;
const newlineAfter =
firstWhitespaceIndex >= 0 ? statement.raws.after.slice(firstWhitespaceIndex) : '';
firstWhitespaceIndex >= 0 ? statementRaws.after.slice(firstWhitespaceIndex) : '';
const newlineIndex = newlineAfter.search(/\r?\n/);

if (newlineIndex >= 0) {
statement.raws.after = newlineBefore + newlineAfter.slice(newlineIndex);
statementRaws.after = newlineBefore + newlineAfter.slice(newlineIndex);
} else {
statement.raws.after = newlineBefore + context.newline + newlineAfter;
statementRaws.after = newlineBefore + context.newline + newlineAfter;
}

return;
}

if (expectation === 'never-multi-line') {
statement.raws.after = statement.raws.after.replace(/\s/g, '');
if (primary === 'never-multi-line') {
statementRaws.after = statementRaws.after.replace(/\s/g, '');

return;
}
Expand All @@ -110,7 +119,7 @@ function rule(expectation, options, context) {
}
}
};
}
};

rule.ruleName = ruleName;
rule.messages = messages;
Expand Down
14 changes: 8 additions & 6 deletions lib/rules/block-closing-brace-space-after/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const blockString = require('../../utils/blockString');
Expand All @@ -21,12 +19,13 @@ const messages = ruleMessages(ruleName, {
rejectedAfterMultiLine: () => 'Unexpected whitespace after "}" of a multi-line block',
});

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

return function (root, result) {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
actual: primary,
possible: [
'always',
'never',
Expand All @@ -45,6 +44,9 @@ function rule(expectation) {
root.walkRules(check);
root.walkAtRules(check);

/**
* @param {import('postcss').Rule | import('postcss').AtRule} statement
*/
function check(statement) {
const nextNode = statement.next();

Expand Down Expand Up @@ -81,7 +83,7 @@ function rule(expectation) {
});
}
};
}
};

rule.ruleName = ruleName;
rule.messages = messages;
Expand Down
26 changes: 16 additions & 10 deletions lib/rules/block-closing-brace-space-before/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-nocheck

'use strict';

const blockString = require('../../utils/blockString');
Expand All @@ -21,12 +19,13 @@ const messages = ruleMessages(ruleName, {
rejectedBeforeMultiLine: () => 'Unexpected whitespace before "}" of a multi-line block',
});

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',
Expand All @@ -45,6 +44,9 @@ function rule(expectation, options, context) {
root.walkRules(check);
root.walkAtRules(check);

/**
* @param {import('postcss').Rule | import('postcss').AtRule} statement
*/
function check(statement) {
// Return early if blockless or has empty block
if (!hasBlock(statement) || hasEmptyBlock(statement)) {
Expand All @@ -65,14 +67,18 @@ function rule(expectation, options, context) {
index: source.length - 1,
err: (msg) => {
if (context.fix) {
if (expectation.startsWith('always')) {
statement.raws.after = statement.raws.after.replace(/\s*$/, ' ');
const statementRaws = statement.raws;

if (typeof statementRaws.after !== 'string') return;

if (primary.startsWith('always')) {
statementRaws.after = statementRaws.after.replace(/\s*$/, ' ');

return;
}

if (expectation.startsWith('never')) {
statement.raws.after = statement.raws.after.replace(/\s*$/, '');
if (primary.startsWith('never')) {
statementRaws.after = statementRaws.after.replace(/\s*$/, '');

return;
}
Expand All @@ -89,7 +95,7 @@ function rule(expectation, options, context) {
});
}
};
}
};

rule.ruleName = ruleName;
rule.messages = messages;
Expand Down