Files
react-native-devtools-frontend/node_modules/stylelint/lib/utils/whitespaceChecker.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

357 lines
9.3 KiB
JavaScript

'use strict';
const configurationError = require('./configurationError');
const isSingleLineString = require('./isSingleLineString');
const isWhitespace = require('./isWhitespace');
/**
* @typedef {(message: string) => string} MessageFunction
*/
/**
* @typedef {Object} Messages
* @property {MessageFunction} [expectedBefore]
* @property {MessageFunction} [rejectedBefore]
* @property {MessageFunction} [expectedAfter]
* @property {MessageFunction} [rejectedAfter]
* @property {MessageFunction} [expectedBeforeSingleLine]
* @property {MessageFunction} [rejectedBeforeSingleLine]
* @property {MessageFunction} [expectedBeforeMultiLine]
* @property {MessageFunction} [rejectedBeforeMultiLine]
* @property {MessageFunction} [expectedAfterSingleLine]
* @property {MessageFunction} [rejectedAfterSingleLine]
* @property {MessageFunction} [expectedAfterMultiLine]
* @property {MessageFunction} [rejectedAfterMultiLine]
*/
/**
* @typedef {Object} WhitespaceCheckerArgs
* @property {string} source - The source string
* @property {number} index - The index of the character to check before
* @property {(message: string) => void} err - If a problem is found, this callback
* will be invoked with the relevant warning message.
* Typically this callback will report() the problem.
* @property {string} [errTarget] - If a problem is found, this string
* will be sent to the relevant warning message.
* @property {string} [lineCheckStr] - Single- and multi-line checkers
* will use this string to determine whether they should proceed,
* i.e. if this string is one line only, single-line checkers will check,
* multi-line checkers will ignore.
* If none is passed, they will use `source`.
* @property {boolean} [onlyOneChar=false] - Only check *one* character before.
* By default, "always-*" checks will look for the `targetWhitespace` one
* before and then ensure there is no whitespace two before. This option
* bypasses that second check.
* @property {boolean} [allowIndentation=false] - Allow arbitrary indentation
* between the `targetWhitespace` (almost definitely a newline) and the `index`.
* With this option, the checker will see if a newline *begins* the whitespace before
* the `index`.
*/
/**
* @typedef {(args: WhitespaceCheckerArgs) => void} WhitespaceChecker
*/
/**
* @typedef {{
* before: WhitespaceChecker,
* beforeAllowingIndentation: WhitespaceChecker,
* after: WhitespaceChecker,
* afterOneOnly: WhitespaceChecker,
* }} WhitespaceCheckers
*/
/**
* Create a whitespaceChecker, which exposes the following functions:
* - `before()`
* - `beforeAllowingIndentation()`
* - `after()`
* - `afterOneOnly()`
*
* @param {"space" | "newline"} targetWhitespace - This is a keyword instead
* of the actual character (e.g. " ") in order to accommodate
* different styles of newline ("\n" vs "\r\n")
* @param {"always" | "never" | "always-single-line" | "always-multi-line" | "never-single-line" | "never-multi-line"} expectation
* @param {Messages} messages - An object of message functions;
* calling `before*()` or `after*()` and the `expectation` that is passed
* determines which message functions are required
*
* @returns {WhitespaceCheckers} The checker, with its exposed checking functions
*/
module.exports = function whitespaceChecker(targetWhitespace, expectation, messages) {
// Keep track of active arguments in order to avoid passing
// too much stuff around, making signatures long and confusing.
// This variable gets reset anytime a checking function is called.
/** @type {WhitespaceCheckerArgs} */
let activeArgs;
/**
* Check for whitespace *before* a character.
* @type {WhitespaceChecker}
*/
function before({
source,
index,
err,
errTarget,
lineCheckStr,
onlyOneChar = false,
allowIndentation = false,
}) {
activeArgs = {
source,
index,
err,
errTarget,
onlyOneChar,
allowIndentation,
};
switch (expectation) {
case 'always':
expectBefore();
break;
case 'never':
rejectBefore();
break;
case 'always-single-line':
if (!isSingleLineString(lineCheckStr || source)) {
return;
}
expectBefore(messages.expectedBeforeSingleLine);
break;
case 'never-single-line':
if (!isSingleLineString(lineCheckStr || source)) {
return;
}
rejectBefore(messages.rejectedBeforeSingleLine);
break;
case 'always-multi-line':
if (isSingleLineString(lineCheckStr || source)) {
return;
}
expectBefore(messages.expectedBeforeMultiLine);
break;
case 'never-multi-line':
if (isSingleLineString(lineCheckStr || source)) {
return;
}
rejectBefore(messages.rejectedBeforeMultiLine);
break;
default:
throw configurationError(`Unknown expectation "${expectation}"`);
}
}
/**
* Check for whitespace *after* a character.
* @type {WhitespaceChecker}
*/
function after({ source, index, err, errTarget, lineCheckStr, onlyOneChar = false }) {
activeArgs = { source, index, err, errTarget, onlyOneChar };
switch (expectation) {
case 'always':
expectAfter();
break;
case 'never':
rejectAfter();
break;
case 'always-single-line':
if (!isSingleLineString(lineCheckStr || source)) {
return;
}
expectAfter(messages.expectedAfterSingleLine);
break;
case 'never-single-line':
if (!isSingleLineString(lineCheckStr || source)) {
return;
}
rejectAfter(messages.rejectedAfterSingleLine);
break;
case 'always-multi-line':
if (isSingleLineString(lineCheckStr || source)) {
return;
}
expectAfter(messages.expectedAfterMultiLine);
break;
case 'never-multi-line':
if (isSingleLineString(lineCheckStr || source)) {
return;
}
rejectAfter(messages.rejectedAfterMultiLine);
break;
default:
throw configurationError(`Unknown expectation "${expectation}"`);
}
}
/**
* @type {WhitespaceChecker}
*/
function beforeAllowingIndentation(obj) {
before({ ...obj, allowIndentation: true });
}
function expectBefore(messageFunc = messages.expectedBefore) {
if (activeArgs.allowIndentation) {
expectBeforeAllowingIndentation(messageFunc);
return;
}
const _activeArgs = activeArgs;
const source = _activeArgs.source;
const index = _activeArgs.index;
const oneCharBefore = source[index - 1];
const twoCharsBefore = source[index - 2];
if (!isValue(oneCharBefore)) {
return;
}
if (
targetWhitespace === 'space' &&
oneCharBefore === ' ' &&
(activeArgs.onlyOneChar || !isWhitespace(twoCharsBefore))
) {
return;
}
assertFunction(messageFunc);
activeArgs.err(messageFunc(activeArgs.errTarget || source[index]));
}
function expectBeforeAllowingIndentation(messageFunc = messages.expectedBefore) {
const _activeArgs2 = activeArgs;
const source = _activeArgs2.source;
const index = _activeArgs2.index;
const err = _activeArgs2.err;
const expectedChar = (function () {
if (targetWhitespace === 'newline') {
return '\n';
}
})();
let i = index - 1;
while (source[i] !== expectedChar) {
if (source[i] === '\t' || source[i] === ' ') {
i--;
continue;
}
assertFunction(messageFunc);
err(messageFunc(activeArgs.errTarget || source[index]));
return;
}
}
function rejectBefore(messageFunc = messages.rejectedBefore) {
const _activeArgs3 = activeArgs;
const source = _activeArgs3.source;
const index = _activeArgs3.index;
const oneCharBefore = source[index - 1];
if (isValue(oneCharBefore) && isWhitespace(oneCharBefore)) {
assertFunction(messageFunc);
activeArgs.err(messageFunc(activeArgs.errTarget || source[index]));
}
}
/**
* @type {WhitespaceChecker}
*/
function afterOneOnly(obj) {
after({ ...obj, onlyOneChar: true });
}
function expectAfter(messageFunc = messages.expectedAfter) {
const _activeArgs4 = activeArgs;
const source = _activeArgs4.source;
const index = _activeArgs4.index;
const oneCharAfter = source[index + 1];
const twoCharsAfter = source[index + 2];
if (!isValue(oneCharAfter)) {
return;
}
if (targetWhitespace === 'newline') {
// If index is followed by a Windows CR-LF ...
if (
oneCharAfter === '\r' &&
twoCharsAfter === '\n' &&
(activeArgs.onlyOneChar || !isWhitespace(source[index + 3]))
) {
return;
}
// If index is followed by a Unix LF ...
if (oneCharAfter === '\n' && (activeArgs.onlyOneChar || !isWhitespace(twoCharsAfter))) {
return;
}
}
if (
targetWhitespace === 'space' &&
oneCharAfter === ' ' &&
(activeArgs.onlyOneChar || !isWhitespace(twoCharsAfter))
) {
return;
}
assertFunction(messageFunc);
activeArgs.err(messageFunc(activeArgs.errTarget || source[index]));
}
function rejectAfter(messageFunc = messages.rejectedAfter) {
const _activeArgs5 = activeArgs;
const source = _activeArgs5.source;
const index = _activeArgs5.index;
const oneCharAfter = source[index + 1];
if (isValue(oneCharAfter) && isWhitespace(oneCharAfter)) {
assertFunction(messageFunc);
activeArgs.err(messageFunc(activeArgs.errTarget || source[index]));
}
}
return {
before,
beforeAllowingIndentation,
after,
afterOneOnly,
};
};
/**
* @param {unknown} x
*/
function isValue(x) {
return x !== undefined && x !== null;
}
/**
* @param {unknown} x
* @returns {asserts x is Function}
*/
function assertFunction(x) {
if (typeof x !== 'function') {
throw new TypeError(`\`${x}\` must be a function`);
}
}