Files
react-native-devtools-frontend/node_modules/stylelint/lib/rules/function-whitespace-after/index.js
T
Tim van der Lippe 16b822804c Reland "Reland "Update stylelint to 14.0.1""
This reverts commit 2b4a9df2d9.

Reason for revert: subsequent presubmit uploads should be fixed now.

Original change's description:
> Revert "Reland "Update stylelint to 14.0.1""
>
> This reverts commit f2ea2c940d.
>
> Reason for revert: https://ci.chromium.org/ui/p/devtools-frontend/builders/try/dtf_presubmit_linux/b8831129368825517985/overview
>
> Original change's description:
> > Reland "Update stylelint to 14.0.1"
> >
> > This reverts commit 6c0f161c95.
> >
> > Reason for revert: the prerequisite CL (https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3259703) has landed
> >
> > Original change's description:
> > > Revert "Update stylelint to 14.0.1"
> > >
> > > This reverts commit 1e08ee816b.
> > >
> > > Reason for revert: tree is closed due to errors https://ci.chromium.org/ui/p/devtools-frontend/builders/ci/Stand-alone%20Linux/8169/overview
> > >
> > > Original change's description:
> > > > Update stylelint to 14.0.1
> > > >
> > > > This also upgrades PostCSS to 8.3.11.
> > > >
> > > > DISABLE_THIRD_PARTY_CHECK=Updating Stylelint configuration
> > > > R=​szuend@chromium.org
> > > >
> > > > Bug: none
> > > > Change-Id: I606540b03509d7c6e73f3d490327cd4174e03d31
> > > > Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3259541
> > > > Reviewed-by: Simon Zünd <szuend@chromium.org>
> > > > Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org>
> > >
> > > Bug: none
> > > Change-Id: Icb1c02b41dbccc3b4fc5760f5dd0b78eca078b61
> > > No-Presubmit: true
> > > No-Tree-Checks: true
> > > No-Try: true
> > > Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3263399
> > > Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
> > > Owners-Override: Alex Rudenko <alexrudenko@chromium.org>
> > > Commit-Queue: Alex Rudenko <alexrudenko@chromium.org>
> >
> > DISABLE_THIRD_PARTY_CHECK=Updating Stylelint configuration
> >
> > Bug: none
> > Change-Id: If132a67ee4253d27114caedd66c5ee61c774a6c7
> > Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3264206
> > Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org>
> > Reviewed-by: Simon Zünd <szuend@chromium.org>
> > Reviewed-by: Alex Rudenko <alexrudenko@chromium.org>
>
> Bug: none
> Change-Id: Idc6c9f5cc1e225c752799461eb0344e94b4ad1e5
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3264222
> Auto-Submit: Tim van der Lippe <tvanderlippe@chromium.org>
> Commit-Queue: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
> Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>

DISABLE_THIRD_PARTY_CHECK=Updating Stylelint configuration

Bug: none
Change-Id: I609941d48b46bfcf454b03dcc75a76d460fbe674
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3264223
Reviewed-by: Jack Franklin <jacktfranklin@chromium.org>
Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org>
2021-11-08 14:47:39 +00:00

183 lines
4.3 KiB
JavaScript

'use strict';
const atRuleParamIndex = require('../../utils/atRuleParamIndex');
const declarationValueIndex = require('../../utils/declarationValueIndex');
const getDeclarationValue = require('../../utils/getDeclarationValue');
const isWhitespace = require('../../utils/isWhitespace');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const setDeclarationValue = require('../../utils/setDeclarationValue');
const styleSearch = require('style-search');
const validateOptions = require('../../utils/validateOptions');
const ruleName = 'function-whitespace-after';
const messages = ruleMessages(ruleName, {
expected: 'Expected whitespace after ")"',
rejected: 'Unexpected whitespace after ")"',
});
const ACCEPTABLE_AFTER_CLOSING_PAREN = new Set([')', ',', '}', ':', '/', undefined]);
/** @type {import('stylelint').Rule} */
const rule = (primary, _secondaryOptions, context) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primary,
possible: ['always', 'never'],
});
if (!validOptions) {
return;
}
/**
* @param {import('postcss').Node} node
* @param {string} value
* @param {number} nodeIndex
* @param {((index: number) => void) | undefined} fix
*/
function check(node, value, nodeIndex, fix) {
styleSearch(
{
source: value,
target: ')',
functionArguments: 'only',
},
(match) => {
checkClosingParen(value, match.startIndex + 1, node, nodeIndex, fix);
},
);
}
/**
* @param {string} source
* @param {number} index
* @param {import('postcss').Node} node
* @param {number} nodeIndex
* @param {((index: number) => void) | undefined} fix
*/
function checkClosingParen(source, index, node, nodeIndex, fix) {
const nextChar = source[index];
if (primary === 'always') {
// Allow for the next character to be a single empty space,
// another closing parenthesis, a comma, or the end of the value
if (nextChar === ' ') {
return;
}
if (nextChar === '\n') {
return;
}
if (source.substr(index, 2) === '\r\n') {
return;
}
if (ACCEPTABLE_AFTER_CLOSING_PAREN.has(nextChar)) {
return;
}
if (fix) {
fix(index);
return;
}
report({
message: messages.expected,
node,
index: nodeIndex + index,
result,
ruleName,
});
} else if (primary === 'never' && isWhitespace(nextChar)) {
if (fix) {
fix(index);
return;
}
report({
message: messages.rejected,
node,
index: nodeIndex + index,
result,
ruleName,
});
}
}
/**
* @param {string} value
*/
function createFixer(value) {
let fixed = '';
let lastIndex = 0;
/** @type {(index: number) => void} */
let applyFix;
if (primary === 'always') {
applyFix = (index) => {
// eslint-disable-next-line prefer-template
fixed += value.slice(lastIndex, index) + ' ';
lastIndex = index;
};
} else if (primary === 'never') {
applyFix = (index) => {
let whitespaceEndIndex = index + 1;
while (whitespaceEndIndex < value.length && isWhitespace(value[whitespaceEndIndex])) {
whitespaceEndIndex++;
}
fixed += value.slice(lastIndex, index);
lastIndex = whitespaceEndIndex;
};
} else {
throw new Error(`Unexpected option: "${primary}"`);
}
return {
applyFix,
get hasFixed() {
return Boolean(lastIndex);
},
get fixed() {
return fixed + value.slice(lastIndex);
},
};
}
root.walkAtRules(/^import$/i, (atRule) => {
const param = (atRule.raws.params && atRule.raws.params.raw) || atRule.params;
const fixer = context.fix && createFixer(param);
check(atRule, param, atRuleParamIndex(atRule), fixer ? fixer.applyFix : undefined);
if (fixer && fixer.hasFixed) {
if (atRule.raws.params) {
atRule.raws.params.raw = fixer.fixed;
} else {
atRule.params = fixer.fixed;
}
}
});
root.walkDecls((decl) => {
const value = getDeclarationValue(decl);
const fixer = context.fix && createFixer(value);
check(decl, value, declarationValueIndex(decl), fixer ? fixer.applyFix : undefined);
if (fixer && fixer.hasFixed) {
setDeclarationValue(decl, fixer.fixed);
}
});
};
};
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;