Files
react-native-devtools-frontend/node_modules/stylelint/lib/utils/blurFunctionArguments.js
T
Tim van der Lippe efb716af67 Update linter packages
- Updates stylelint to 13.8.0
- Updates ESLint to 7.14.0
- Updates @typescript-eslint to 4.9.0

DISABLE_THIRD_PARTY_CHECK=NPM update
R=jacktfranklin@chromium.org

Change-Id: I207e204607ede782710e042c17d6510c7f696905
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2566806
Reviewed-by: Jack Franklin <jacktfranklin@chromium.org>
Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org>
2020-12-01 16:04:40 +00:00

54 lines
1.6 KiB
JavaScript

'use strict';
const balancedMatch = require('balanced-match');
/**
* Replace all of the characters that are arguments to a certain
* CSS function with some innocuous character.
*
* This is useful if you need to use a RegExp to find a string
* but want to ignore matches in certain functions (e.g. `url()`,
* which might contain all kinds of false positives).
*
* For example:
* blurFunctionArguments("abc url(abc) abc", "url") === "abc url(```) abc"
*
* @param {string} source
* @param {string} functionName
* @return {string} - The result string, with the function arguments "blurred"
*/
module.exports = function (source, functionName, blurChar = '`') {
const nameWithParen = `${functionName.toLowerCase()}(`;
const lowerCaseSource = source.toLowerCase();
if (!lowerCaseSource.includes(nameWithParen)) {
return source;
}
const functionNameLength = functionName.length;
let result = source;
let searchStartIndex = 0;
while (lowerCaseSource.includes(nameWithParen, searchStartIndex)) {
const openingParenIndex =
lowerCaseSource.indexOf(nameWithParen, searchStartIndex) + functionNameLength;
const parensMatch = balancedMatch('(', ')', lowerCaseSource.slice(openingParenIndex));
if (!parensMatch) {
throw new Error(`No parens match: "${source}"`);
}
const closingParenIndex = parensMatch.end + openingParenIndex;
const argumentsLength = closingParenIndex - openingParenIndex - 1;
result =
result.slice(0, openingParenIndex + 1) +
blurChar.repeat(argumentsLength) +
result.slice(closingParenIndex);
searchStartIndex = closingParenIndex;
}
return result;
};