getAttributes.js
2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"use strict";
exports.__esModule = true;
exports.default = void 0;
var t = _interopRequireWildcard(require("@babel/types"));
var _util = require("./util");
var _stringToObjectStyle = _interopRequireDefault(require("./stringToObjectStyle"));
var _mappings = require("./mappings");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function convertAriaAttribute(kebabKey) {
const [aria, ...parts] = kebabKey.split('-');
return `${aria}-${parts.join('').toLowerCase()}`;
}
function getKey(key, value, node) {
const lowerCaseKey = key.toLowerCase();
const mappedElementAttribute = _mappings.ELEMENT_ATTRIBUTE_MAPPING[node.name] && _mappings.ELEMENT_ATTRIBUTE_MAPPING[node.name][lowerCaseKey];
const mappedAttribute = _mappings.ATTRIBUTE_MAPPING[lowerCaseKey];
if (mappedElementAttribute || mappedAttribute) {
return t.jsxIdentifier(mappedElementAttribute || mappedAttribute);
}
const kebabKey = (0, _util.kebabCase)(key);
if (kebabKey.startsWith('aria-')) {
return t.jsxIdentifier(convertAriaAttribute(kebabKey));
}
if (kebabKey.startsWith('data-')) {
return t.jsxIdentifier(kebabKey);
}
return t.jsxIdentifier(key);
}
function getValue(key, value) {
// Handle className
if (Array.isArray(value)) {
return t.stringLiteral((0, _util.replaceSpaces)(value.join(' ')));
}
if (key === 'style') {
return t.jsxExpressionContainer((0, _stringToObjectStyle.default)(value));
}
if ((0, _util.isNumeric)(value)) {
return t.jsxExpressionContainer(t.numericLiteral(Number(value)));
}
return t.stringLiteral((0, _util.replaceSpaces)(value));
}
const getAttributes = node => {
const keys = Object.keys(node.properties);
const attributes = [];
let index = -1;
while (++index < keys.length) {
const key = keys[index];
const value = node.properties[key];
const attribute = t.jsxAttribute(getKey(key, value, node), getValue(key, value, node));
attributes.push(attribute);
}
return attributes;
};
var _default = getAttributes;
exports.default = _default;