@sumup-oss/eslint-plugin-circuit-ui
Version:
ESLint rules to lint Circuit UI.
68 lines (67 loc) • 2.49 kB
JavaScript
;
/**
* Copyright 2023, SumUp Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.findAttribute = findAttribute;
exports.getAttributeValue = getAttributeValue;
exports.filterWhitespaceChildren = filterWhitespaceChildren;
exports.transformAttributeValueToChildren = transformAttributeValueToChildren;
/* eslint-disable */
function findAttribute(node, attributeName) {
return node.openingElement.attributes.find((attribute) => attribute.type === 'JSXAttribute' &&
attribute.name.type === 'JSXIdentifier' &&
attribute.name.name === attributeName);
}
function getAttributeValue(attribute) {
if (attribute.value?.type === 'Literal' &&
typeof attribute.value.value === 'string') {
return attribute.value.value;
}
if (attribute.value?.type === 'JSXExpressionContainer' &&
attribute.value.expression.type === 'Literal') {
return attribute.value.expression.value;
}
return null;
}
function filterWhitespaceChildren(children) {
return children.filter((child) => {
if (child.type !== 'JSXText') {
return true;
}
const nonWhiteSpaceText = child.value.trim();
return Boolean(nonWhiteSpaceText);
});
}
function transformAttributeValueToChildren(sourceCode, attribute) {
if (!attribute.value) {
return null;
}
switch (attribute.value.type) {
case 'Literal':
return sourceCode
.getText(attribute.value)
.replace(/^"/, '')
.replace(/"$/, '');
case 'JSXExpressionContainer':
switch (attribute.value.expression.type) {
case 'JSXElement':
return sourceCode.getText(attribute.value.expression);
default:
return sourceCode.getText(attribute.value);
}
default:
return null;
}
}