@litsx/babel-preset-litsx 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -0
- package/dist/index.cjs.map +1 -1
- package/dist/internal/transform-litsx-components.cjs +316 -40
- package/dist/internal/transform-litsx-components.cjs.map +1 -1
- package/dist/internal/transform-litsx-renderer-props.cjs +383 -0
- package/dist/internal/transform-litsx-renderer-props.cjs.map +1 -0
- package/dist/pipeline.cjs +2 -0
- package/dist/pipeline.cjs.map +1 -1
- package/package.json +7 -2
- package/src/internal/transform-litsx-components.js +186 -38
- package/src/internal/transform-litsx-param-rewrites.js +72 -1
- package/src/internal/transform-litsx-program.js +32 -0
- package/src/internal/transform-litsx-renderer-props.js +360 -0
- package/src/internal/transform-litsx-wrapper-utils.js +30 -1
- package/src/pipeline.js +2 -0
package/dist/index.cjs
CHANGED
|
@@ -10,6 +10,7 @@ require('@litsx/babel-plugin-transform-litsx-scoped-elements');
|
|
|
10
10
|
require('./internal/transform-litsx-dom-refs.cjs');
|
|
11
11
|
require('@litsx/babel-plugin-shared-hooks');
|
|
12
12
|
require('./internal/transform-litsx-hooks.cjs');
|
|
13
|
+
require('./internal/transform-litsx-renderer-props.cjs');
|
|
13
14
|
require('@babel/plugin-syntax-jsx');
|
|
14
15
|
require('module');
|
|
15
16
|
require('@litsx/typescript-session');
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/index.js"],"sourcesContent":["import { createLitsxPresetPlugins } from \"./pipeline.js\";\n\nexport { createLitsxPresetPlugins, detectLitsxSourceFeatures } from \"./pipeline.js\";\nexport {\n createTransformLitsxComponentsPlugin,\n setTypescriptModule,\n} from \"./pipeline.js\";\n\nexport default function litsxPreset(api, options = {}) {\n api.assertVersion?.(7);\n\n return {\n plugins: createLitsxPresetPlugins(options),\n };\n}\n"],"names":["createLitsxPresetPlugins"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/index.js"],"sourcesContent":["import { createLitsxPresetPlugins } from \"./pipeline.js\";\n\nexport { createLitsxPresetPlugins, detectLitsxSourceFeatures } from \"./pipeline.js\";\nexport {\n createTransformLitsxComponentsPlugin,\n setTypescriptModule,\n} from \"./pipeline.js\";\n\nexport default function litsxPreset(api, options = {}) {\n api.assertVersion?.(7);\n\n return {\n plugins: createLitsxPresetPlugins(options),\n };\n}\n"],"names":["createLitsxPresetPlugins"],"mappings":";;;;;;;;;;;;;;;;;AAQe,SAAS,WAAW,CAAC,GAAG,EAAE,OAAO,GAAG,EAAE,EAAE;AACvD,EAAE,GAAG,CAAC,aAAa,GAAG,CAAC,CAAC;;AAExB,EAAE,OAAO;AACT,IAAI,OAAO,EAAEA,iCAAwB,CAAC,OAAO,CAAC;AAC9C,GAAG;AACH;;;;;;;;"}
|
|
@@ -979,6 +979,15 @@ function createHandlerClassMember({ name, params, body, async, generator }) {
|
|
|
979
979
|
|
|
980
980
|
let t$5;
|
|
981
981
|
|
|
982
|
+
function isCapitalizedComponentName$1(name) {
|
|
983
|
+
if (typeof name !== "string" || name.length === 0) {
|
|
984
|
+
return false;
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
const first = name[0];
|
|
988
|
+
return first === first.toUpperCase() && first !== first.toLowerCase();
|
|
989
|
+
}
|
|
990
|
+
|
|
982
991
|
function setWrapperUtilsBabelTypes(nextTypes) {
|
|
983
992
|
t$5 = nextTypes;
|
|
984
993
|
}
|
|
@@ -1046,6 +1055,7 @@ function maybeTransformWrappedVariableDeclarator({
|
|
|
1046
1055
|
{
|
|
1047
1056
|
...resolvedPluginOptions,
|
|
1048
1057
|
...wrapperMeta.options,
|
|
1058
|
+
state,
|
|
1049
1059
|
typeResolver: state?.__litsxTypeResolver,
|
|
1050
1060
|
warn: (warning) => {
|
|
1051
1061
|
state?.__litsxWarnings?.push(warning);
|
|
@@ -1108,12 +1118,17 @@ function handlePotentialComponentExport({
|
|
|
1108
1118
|
exportName = declaration.declarations[0].id.name;
|
|
1109
1119
|
}
|
|
1110
1120
|
|
|
1121
|
+
if (exportName && !isCapitalizedComponentName$1(exportName)) {
|
|
1122
|
+
return false;
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1111
1125
|
const classNode = transformFunction(
|
|
1112
1126
|
declarationPath,
|
|
1113
1127
|
exportPath.findParent((p) => p.isProgram()),
|
|
1114
1128
|
exportName,
|
|
1115
1129
|
{
|
|
1116
1130
|
...state?.__litsxResolvedPluginOptions,
|
|
1131
|
+
state,
|
|
1117
1132
|
typeResolver,
|
|
1118
1133
|
warn: (warning) => {
|
|
1119
1134
|
state?.__litsxWarnings?.push(warning);
|
|
@@ -1149,6 +1164,10 @@ function handlePotentialComponentExport({
|
|
|
1149
1164
|
: undefined;
|
|
1150
1165
|
const programPath = exportPath.findParent((p) => p.isProgram());
|
|
1151
1166
|
|
|
1167
|
+
if (exportName && !isCapitalizedComponentName$1(exportName)) {
|
|
1168
|
+
return false;
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1152
1171
|
if (initPath.isCallExpression()) {
|
|
1153
1172
|
const wrapperMeta = getWrapperMetadata(initPath);
|
|
1154
1173
|
if (!wrapperMeta) return false;
|
|
@@ -1163,6 +1182,7 @@ function handlePotentialComponentExport({
|
|
|
1163
1182
|
{
|
|
1164
1183
|
...state?.__litsxResolvedPluginOptions,
|
|
1165
1184
|
...wrapperMeta.options,
|
|
1185
|
+
state,
|
|
1166
1186
|
typeResolver,
|
|
1167
1187
|
warn: (warning) => {
|
|
1168
1188
|
state?.__litsxWarnings?.push(warning);
|
|
@@ -1175,7 +1195,11 @@ function handlePotentialComponentExport({
|
|
|
1175
1195
|
exportPath.scope.removeBinding(exportName);
|
|
1176
1196
|
}
|
|
1177
1197
|
|
|
1178
|
-
exportPath.replaceWith(
|
|
1198
|
+
exportPath.replaceWith(
|
|
1199
|
+
isDefault
|
|
1200
|
+
? t$5.exportDefaultDeclaration(classNode)
|
|
1201
|
+
: t$5.exportNamedDeclaration(classNode, [])
|
|
1202
|
+
);
|
|
1179
1203
|
exportPath.requeue();
|
|
1180
1204
|
pruneWrapperImports(wrapperMeta);
|
|
1181
1205
|
updateTransformState?.(state, classNode);
|
|
@@ -1196,6 +1220,10 @@ function handlePotentialComponentExport({
|
|
|
1196
1220
|
? wrapperMeta.functionPath.node.id.name
|
|
1197
1221
|
: undefined;
|
|
1198
1222
|
|
|
1223
|
+
if (!inferredName || !isCapitalizedComponentName$1(inferredName)) {
|
|
1224
|
+
return false;
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1199
1227
|
const classNode = transformFunction(
|
|
1200
1228
|
wrapperMeta.functionPath,
|
|
1201
1229
|
programPath,
|
|
@@ -1203,6 +1231,7 @@ function handlePotentialComponentExport({
|
|
|
1203
1231
|
{
|
|
1204
1232
|
...state?.__litsxResolvedPluginOptions,
|
|
1205
1233
|
...wrapperMeta.options,
|
|
1234
|
+
state,
|
|
1206
1235
|
typeResolver,
|
|
1207
1236
|
warn: (warning) => {
|
|
1208
1237
|
state?.__litsxWarnings?.push(warning);
|
|
@@ -1573,11 +1602,82 @@ function createThisMemberExpression$1(propName) {
|
|
|
1573
1602
|
return t$2.memberExpression(t$2.thisExpression(), t$2.identifier(propName));
|
|
1574
1603
|
}
|
|
1575
1604
|
|
|
1576
|
-
function
|
|
1605
|
+
function getBoundPropName(bindingInfo) {
|
|
1606
|
+
if (typeof bindingInfo === "string") {
|
|
1607
|
+
return bindingInfo;
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
if (bindingInfo && typeof bindingInfo === "object") {
|
|
1611
|
+
return bindingInfo.bindKey ?? null;
|
|
1612
|
+
}
|
|
1613
|
+
|
|
1614
|
+
return null;
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1617
|
+
function isPropBackedCallee(node, localNames) {
|
|
1618
|
+
if (t$2.isIdentifier(node)) {
|
|
1619
|
+
return localNames.includes(node.name);
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
if (
|
|
1623
|
+
t$2.isMemberExpression(node) &&
|
|
1624
|
+
!node.computed &&
|
|
1625
|
+
t$2.isIdentifier(node.object)
|
|
1626
|
+
) {
|
|
1627
|
+
return localNames.includes(node.object.name) && node.object.name === "props";
|
|
1628
|
+
}
|
|
1629
|
+
|
|
1630
|
+
return false;
|
|
1631
|
+
}
|
|
1632
|
+
|
|
1633
|
+
function getPropBackedCalleeReplacement(node, bindings) {
|
|
1634
|
+
if (t$2.isIdentifier(node)) {
|
|
1635
|
+
const propName = getBoundPropName(bindings.get(node.name));
|
|
1636
|
+
return propName ? createThisMemberExpression$1(propName) : node;
|
|
1637
|
+
}
|
|
1638
|
+
|
|
1639
|
+
if (
|
|
1640
|
+
t$2.isMemberExpression(node) &&
|
|
1641
|
+
!node.computed &&
|
|
1642
|
+
t$2.isIdentifier(node.object)
|
|
1643
|
+
) {
|
|
1644
|
+
const propName = getBoundPropName(bindings.get(node.object.name));
|
|
1645
|
+
if (!propName) {
|
|
1646
|
+
return node;
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
return t$2.memberExpression(
|
|
1650
|
+
createThisMemberExpression$1(propName),
|
|
1651
|
+
t$2.cloneNode(node.property),
|
|
1652
|
+
false
|
|
1653
|
+
);
|
|
1654
|
+
}
|
|
1655
|
+
|
|
1656
|
+
return node;
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
function transformJSXExpressions(jsxPath, bindings, state = null) {
|
|
1577
1660
|
const localNames = Array.from(bindings.keys());
|
|
1578
1661
|
|
|
1579
1662
|
jsxPath.traverse({
|
|
1580
1663
|
JSXExpressionContainer(expressionPath) {
|
|
1664
|
+
if (t$2.isCallExpression(expressionPath.node.expression)) {
|
|
1665
|
+
const { callee, arguments: args } = expressionPath.node.expression;
|
|
1666
|
+
if (isPropBackedCallee(callee, localNames)) {
|
|
1667
|
+
if (state) {
|
|
1668
|
+
state.__litsxNeedsRendererCallImport = true;
|
|
1669
|
+
}
|
|
1670
|
+
expressionPath.node.expression = t$2.callExpression(
|
|
1671
|
+
t$2.identifier("renderRendererCall"),
|
|
1672
|
+
[
|
|
1673
|
+
getPropBackedCalleeReplacement(callee, bindings),
|
|
1674
|
+
...args,
|
|
1675
|
+
]
|
|
1676
|
+
);
|
|
1677
|
+
return;
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
|
|
1581
1681
|
if (t$2.isIdentifier(expressionPath.node.expression)) {
|
|
1582
1682
|
const name = expressionPath.node.expression.name;
|
|
1583
1683
|
if (localNames.includes(name)) {
|
|
@@ -1815,6 +1915,15 @@ function createLitsxInfrastructureImport(importedName) {
|
|
|
1815
1915
|
);
|
|
1816
1916
|
}
|
|
1817
1917
|
|
|
1918
|
+
function createLitsxInternalRuntimeImport(importedName) {
|
|
1919
|
+
return t$1.importDeclaration(
|
|
1920
|
+
[
|
|
1921
|
+
t$1.importSpecifier(t$1.identifier(importedName), t$1.identifier(importedName)),
|
|
1922
|
+
],
|
|
1923
|
+
t$1.stringLiteral("@litsx/litsx/internal/runtime-render-context")
|
|
1924
|
+
);
|
|
1925
|
+
}
|
|
1926
|
+
|
|
1818
1927
|
function createLitsxImport(importedName) {
|
|
1819
1928
|
return t$1.importDeclaration(
|
|
1820
1929
|
[
|
|
@@ -2018,11 +2127,43 @@ function finalizeProgram(programPath, state) {
|
|
|
2018
2127
|
}
|
|
2019
2128
|
}
|
|
2020
2129
|
|
|
2130
|
+
if (state.__litsxNeedsRendererCallImport) {
|
|
2131
|
+
const bodyPathsWithInternalRuntime = programPath.get("body");
|
|
2132
|
+
const internalRuntimeImports = bodyPathsWithInternalRuntime.filter(
|
|
2133
|
+
(n) =>
|
|
2134
|
+
n.isImportDeclaration() &&
|
|
2135
|
+
n.node.source.value === "@litsx/litsx/internal/runtime-render-context"
|
|
2136
|
+
);
|
|
2137
|
+
|
|
2138
|
+
let internalRuntimeImported = false;
|
|
2139
|
+
internalRuntimeImports.some((importPath) => {
|
|
2140
|
+
if (ensureNamedImport(importPath, "renderRendererCall")) {
|
|
2141
|
+
internalRuntimeImported = true;
|
|
2142
|
+
return true;
|
|
2143
|
+
}
|
|
2144
|
+
|
|
2145
|
+
return false;
|
|
2146
|
+
});
|
|
2147
|
+
|
|
2148
|
+
if (!internalRuntimeImported) {
|
|
2149
|
+
programPath.unshiftContainer("body", createLitsxInternalRuntimeImport("renderRendererCall"));
|
|
2150
|
+
}
|
|
2151
|
+
}
|
|
2152
|
+
|
|
2021
2153
|
pruneUnusedLitsxStaticImports(programPath);
|
|
2022
2154
|
}
|
|
2023
2155
|
|
|
2024
2156
|
let t;
|
|
2025
2157
|
|
|
2158
|
+
function isCapitalizedComponentName(name) {
|
|
2159
|
+
if (typeof name !== "string" || name.length === 0) {
|
|
2160
|
+
return false;
|
|
2161
|
+
}
|
|
2162
|
+
|
|
2163
|
+
const first = name[0];
|
|
2164
|
+
return first === first.toUpperCase() && first !== first.toLowerCase();
|
|
2165
|
+
}
|
|
2166
|
+
|
|
2026
2167
|
function createTransformFunctionToClassPlugin(defaultPluginOptions = {}) {
|
|
2027
2168
|
return function transformFunctionToClassPlugin(_api, pluginOptions = {}) {
|
|
2028
2169
|
internal_transformLitsxProperties.ensureTypescriptModule();
|
|
@@ -2055,6 +2196,7 @@ function createTransformFunctionToClassPlugin(defaultPluginOptions = {}) {
|
|
|
2055
2196
|
this.__litsxNeedsStaticHoistsMixin = false;
|
|
2056
2197
|
this.__litsxNeedsLightDomMixin = false;
|
|
2057
2198
|
this.__litsxNeedsCallbackRef = false;
|
|
2199
|
+
this.__litsxNeedsRendererCallImport = false;
|
|
2058
2200
|
this.__litsxWarnings = [];
|
|
2059
2201
|
this.__litsxResolvedPluginOptions = resolvedPluginOptions;
|
|
2060
2202
|
this.__litsxTypeResolver = fileLikelyNeedsTypeResolver(this)
|
|
@@ -2121,15 +2263,21 @@ function createTransformFunctionToClassPlugin(defaultPluginOptions = {}) {
|
|
|
2121
2263
|
return;
|
|
2122
2264
|
}
|
|
2123
2265
|
|
|
2124
|
-
if (
|
|
2266
|
+
if (
|
|
2267
|
+
initPath &&
|
|
2268
|
+
initPath.isArrowFunctionExpression() &&
|
|
2269
|
+
!isInsideFunctionOrClass(varPath) &&
|
|
2270
|
+
t.isIdentifier(varPath.node.id) &&
|
|
2271
|
+
isCapitalizedComponentName(varPath.node.id.name)
|
|
2272
|
+
) {
|
|
2125
2273
|
const programPath = varPath.findParent((p) => p.isProgram());
|
|
2126
|
-
const elementCandidates = collectElementCandidates(initPath, programPath);
|
|
2127
2274
|
const classNode = transformFunction(
|
|
2128
2275
|
initPath,
|
|
2129
2276
|
programPath,
|
|
2130
2277
|
varPath.node.id.name,
|
|
2131
2278
|
{
|
|
2132
2279
|
...resolvedPluginOptions,
|
|
2280
|
+
state: this,
|
|
2133
2281
|
typeResolver: getTypeResolverForFunction(initPath, this),
|
|
2134
2282
|
warn: (warning) => {
|
|
2135
2283
|
this.__litsxWarnings.push(warning);
|
|
@@ -2139,12 +2287,6 @@ function createTransformFunctionToClassPlugin(defaultPluginOptions = {}) {
|
|
|
2139
2287
|
|
|
2140
2288
|
if (!classNode) return;
|
|
2141
2289
|
|
|
2142
|
-
if (elementCandidates.size) {
|
|
2143
|
-
classNode._litsxElementCandidates &&= new Set(classNode._litsxElementCandidates);
|
|
2144
|
-
const elementSet = classNode._litsxElementCandidates ||= new Set();
|
|
2145
|
-
elementCandidates.forEach((candidate) => elementSet.add(candidate));
|
|
2146
|
-
}
|
|
2147
|
-
|
|
2148
2290
|
const declarationPath = varPath.parentPath;
|
|
2149
2291
|
if (!declarationPath.isVariableDeclaration()) return;
|
|
2150
2292
|
|
|
@@ -2155,15 +2297,21 @@ function createTransformFunctionToClassPlugin(defaultPluginOptions = {}) {
|
|
|
2155
2297
|
}
|
|
2156
2298
|
},
|
|
2157
2299
|
FunctionDeclaration(funcPath) {
|
|
2158
|
-
if (
|
|
2300
|
+
if (
|
|
2301
|
+
!funcPath.parentPath?.isExportNamedDeclaration?.() &&
|
|
2302
|
+
!funcPath.parentPath?.isExportDefaultDeclaration?.() &&
|
|
2303
|
+
!isInsideFunctionOrClass(funcPath) &&
|
|
2304
|
+
funcPath.node.id &&
|
|
2305
|
+
isCapitalizedComponentName(funcPath.node.id.name)
|
|
2306
|
+
) {
|
|
2159
2307
|
const programPath = funcPath.findParent((p) => p.isProgram());
|
|
2160
|
-
const elementCandidates = collectElementCandidates(funcPath, programPath);
|
|
2161
2308
|
const classNode = transformFunction(
|
|
2162
2309
|
funcPath,
|
|
2163
2310
|
programPath,
|
|
2164
2311
|
undefined,
|
|
2165
2312
|
{
|
|
2166
2313
|
...resolvedPluginOptions,
|
|
2314
|
+
state: this,
|
|
2167
2315
|
typeResolver: getTypeResolverForFunction(funcPath, this),
|
|
2168
2316
|
warn: (warning) => {
|
|
2169
2317
|
this.__litsxWarnings.push(warning);
|
|
@@ -2176,11 +2324,6 @@ function createTransformFunctionToClassPlugin(defaultPluginOptions = {}) {
|
|
|
2176
2324
|
if (funcPath.node.id) {
|
|
2177
2325
|
funcPath.scope.removeBinding(funcPath.node.id.name);
|
|
2178
2326
|
}
|
|
2179
|
-
if (elementCandidates.size) {
|
|
2180
|
-
classNode._litsxElementCandidates &&= new Set(classNode._litsxElementCandidates);
|
|
2181
|
-
const elementSet = classNode._litsxElementCandidates ||= new Set();
|
|
2182
|
-
elementCandidates.forEach((candidate) => elementSet.add(candidate));
|
|
2183
|
-
}
|
|
2184
2327
|
funcPath.replaceWith(classNode);
|
|
2185
2328
|
funcPath.requeue();
|
|
2186
2329
|
updateTransformState(this, classNode);
|
|
@@ -2332,6 +2475,7 @@ function getTypeResolverForFunction(functionPath, state) {
|
|
|
2332
2475
|
|
|
2333
2476
|
function transformFunction(functionPath, programPath, className, options = {}) {
|
|
2334
2477
|
const { node } = functionPath;
|
|
2478
|
+
const elementCandidates = collectElementCandidates(functionPath, programPath, options);
|
|
2335
2479
|
const forwardRefOptions = options.forwardRef || null;
|
|
2336
2480
|
let resolvedName = className;
|
|
2337
2481
|
if (!resolvedName && node && node.id && t.isIdentifier(node.id)) {
|
|
@@ -2363,7 +2507,7 @@ function transformFunction(functionPath, programPath, className, options = {}) {
|
|
|
2363
2507
|
ReturnStatement(returnPath) {
|
|
2364
2508
|
if (t.isJSXElement(returnPath.node.argument)) {
|
|
2365
2509
|
returnStatement = returnPath.node;
|
|
2366
|
-
transformJSXExpressions(returnPath, bindings);
|
|
2510
|
+
transformJSXExpressions(returnPath, bindings, options.state ?? null);
|
|
2367
2511
|
}
|
|
2368
2512
|
},
|
|
2369
2513
|
});
|
|
@@ -2445,7 +2589,7 @@ function transformFunction(functionPath, programPath, className, options = {}) {
|
|
|
2445
2589
|
createHandlerClassMember,
|
|
2446
2590
|
});
|
|
2447
2591
|
|
|
2448
|
-
|
|
2592
|
+
const classNode = createComponentClass({
|
|
2449
2593
|
className,
|
|
2450
2594
|
classMembers,
|
|
2451
2595
|
hoistMembers,
|
|
@@ -2456,6 +2600,14 @@ function transformFunction(functionPath, programPath, className, options = {}) {
|
|
|
2456
2600
|
needsUnsafeCss,
|
|
2457
2601
|
needsCallbackRef,
|
|
2458
2602
|
});
|
|
2603
|
+
|
|
2604
|
+
if (classNode && elementCandidates.size) {
|
|
2605
|
+
classNode._litsxElementCandidates &&= new Set(classNode._litsxElementCandidates);
|
|
2606
|
+
const elementSet = classNode._litsxElementCandidates ||= new Set();
|
|
2607
|
+
elementCandidates.forEach((candidate) => elementSet.add(candidate));
|
|
2608
|
+
}
|
|
2609
|
+
|
|
2610
|
+
return classNode;
|
|
2459
2611
|
}
|
|
2460
2612
|
|
|
2461
2613
|
function createThisMemberExpression(propName) {
|
|
@@ -2479,40 +2631,164 @@ function createNestedInitializerStatement(pattern, root, defaultValue, t) {
|
|
|
2479
2631
|
]);
|
|
2480
2632
|
}
|
|
2481
2633
|
|
|
2482
|
-
function collectElementCandidates(functionPath, programPath) {
|
|
2634
|
+
function collectElementCandidates(functionPath, programPath, options = {}) {
|
|
2483
2635
|
const candidates = new Set();
|
|
2484
2636
|
if (!programPath) return candidates;
|
|
2637
|
+
programPath.scope.crawl();
|
|
2638
|
+
const compatPascalNames =
|
|
2639
|
+
programPath.getData("__litsxCompatPascalNames") || new Set();
|
|
2485
2640
|
|
|
2486
|
-
const
|
|
2641
|
+
const availableNames = new Set();
|
|
2642
|
+
const helperPaths = new Map();
|
|
2487
2643
|
programPath.get("body").forEach((nodePath) => {
|
|
2488
|
-
if (
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2644
|
+
if (nodePath.isImportDeclaration()) {
|
|
2645
|
+
nodePath.node.specifiers.forEach((specifier) => {
|
|
2646
|
+
if (specifier.local?.name) {
|
|
2647
|
+
availableNames.add(specifier.local.name);
|
|
2648
|
+
}
|
|
2649
|
+
});
|
|
2650
|
+
return;
|
|
2651
|
+
}
|
|
2652
|
+
|
|
2653
|
+
if (nodePath.isClassDeclaration() && nodePath.node.id?.name) {
|
|
2654
|
+
availableNames.add(nodePath.node.id.name);
|
|
2655
|
+
return;
|
|
2656
|
+
}
|
|
2657
|
+
|
|
2658
|
+
if (
|
|
2659
|
+
(nodePath.isExportNamedDeclaration() || nodePath.isExportDefaultDeclaration()) &&
|
|
2660
|
+
nodePath.get("declaration")?.isClassDeclaration?.() &&
|
|
2661
|
+
nodePath.node.declaration?.id?.name
|
|
2662
|
+
) {
|
|
2663
|
+
availableNames.add(nodePath.node.declaration.id.name);
|
|
2664
|
+
return;
|
|
2665
|
+
}
|
|
2666
|
+
|
|
2667
|
+
if (nodePath.isFunctionDeclaration() && nodePath.node.id?.name) {
|
|
2668
|
+
availableNames.add(nodePath.node.id.name);
|
|
2669
|
+
helperPaths.set(nodePath.node.id.name, nodePath);
|
|
2670
|
+
return;
|
|
2671
|
+
}
|
|
2672
|
+
|
|
2673
|
+
if (!nodePath.isVariableDeclaration()) return;
|
|
2674
|
+
nodePath.get("declarations").forEach((declaratorPath) => {
|
|
2675
|
+
const declarator = declaratorPath.node;
|
|
2676
|
+
if (!t.isIdentifier(declarator.id)) {
|
|
2677
|
+
return;
|
|
2678
|
+
}
|
|
2679
|
+
|
|
2680
|
+
availableNames.add(declarator.id.name);
|
|
2681
|
+
|
|
2682
|
+
const initPath = declaratorPath.get("init");
|
|
2683
|
+
if (
|
|
2684
|
+
initPath?.isArrowFunctionExpression?.() ||
|
|
2685
|
+
initPath?.isFunctionExpression?.()
|
|
2686
|
+
) {
|
|
2687
|
+
helperPaths.set(declarator.id.name, initPath);
|
|
2492
2688
|
}
|
|
2493
2689
|
});
|
|
2494
2690
|
});
|
|
2495
2691
|
|
|
2496
|
-
|
|
2497
|
-
JSXOpeningElement(path) {
|
|
2498
|
-
if (!path.node.name || path.node.name.type !== "JSXIdentifier") return;
|
|
2499
|
-
const originalName = path.node.name.name;
|
|
2500
|
-
if (!importNames.has(originalName)) return;
|
|
2692
|
+
const helperCandidateCache = new Map();
|
|
2501
2693
|
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2694
|
+
function isCapitalizedName(name) {
|
|
2695
|
+
if (typeof name !== "string" || name.length === 0) {
|
|
2696
|
+
return false;
|
|
2697
|
+
}
|
|
2698
|
+
|
|
2699
|
+
const first = name[0];
|
|
2700
|
+
return first === first.toUpperCase() && first !== first.toLowerCase();
|
|
2701
|
+
}
|
|
2702
|
+
|
|
2703
|
+
function isProgramLevelBinding(binding) {
|
|
2704
|
+
return binding?.scope?.path?.isProgram?.() === true;
|
|
2705
|
+
}
|
|
2706
|
+
|
|
2707
|
+
function maybeRewriteComponentName(nameNode, pathForErrors = null) {
|
|
2708
|
+
if (!nameNode || nameNode.type !== "JSXIdentifier") return null;
|
|
2709
|
+
const originalName = nameNode.__scopedOriginal || nameNode.name;
|
|
2710
|
+
if (!isCapitalizedName(originalName)) return null;
|
|
2711
|
+
const binding = pathForErrors?.scope?.getBinding?.(originalName) || null;
|
|
2712
|
+
if (!binding) {
|
|
2713
|
+
if (availableNames.has(originalName)) {
|
|
2714
|
+
return originalName;
|
|
2715
|
+
}
|
|
2716
|
+
if (compatPascalNames.has(originalName)) {
|
|
2717
|
+
return null;
|
|
2718
|
+
}
|
|
2719
|
+
if (options?.allowUnknownPascalCase === true) {
|
|
2720
|
+
return null;
|
|
2721
|
+
}
|
|
2722
|
+
throw (pathForErrors?.buildCodeFrameError?.(
|
|
2723
|
+
`Unknown LitSX component "${originalName}". Add an import or declare it in this module before using it in JSX.`
|
|
2724
|
+
) || new Error(
|
|
2725
|
+
`Unknown LitSX component "${originalName}". Add an import or declare it in this module before using it in JSX.`
|
|
2726
|
+
));
|
|
2727
|
+
}
|
|
2728
|
+
|
|
2729
|
+
if (!isProgramLevelBinding(binding)) {
|
|
2730
|
+
return null;
|
|
2731
|
+
}
|
|
2732
|
+
|
|
2733
|
+
return originalName;
|
|
2734
|
+
}
|
|
2735
|
+
|
|
2736
|
+
function scanFunction(path, seen = new Set()) {
|
|
2737
|
+
if (!path?.node) {
|
|
2738
|
+
return new Set();
|
|
2739
|
+
}
|
|
2740
|
+
|
|
2741
|
+
if (helperCandidateCache.has(path.node)) {
|
|
2742
|
+
return new Set(helperCandidateCache.get(path.node));
|
|
2743
|
+
}
|
|
2744
|
+
|
|
2745
|
+
if (seen.has(path.node)) {
|
|
2746
|
+
return new Set();
|
|
2747
|
+
}
|
|
2748
|
+
|
|
2749
|
+
const nextSeen = new Set(seen);
|
|
2750
|
+
nextSeen.add(path.node);
|
|
2751
|
+
const localCandidates = new Set();
|
|
2752
|
+
const referencedHelpers = new Set();
|
|
2753
|
+
|
|
2754
|
+
path.traverse({
|
|
2755
|
+
JSXOpeningElement(jsxPath) {
|
|
2756
|
+
const candidate = maybeRewriteComponentName(jsxPath.node.name, jsxPath);
|
|
2757
|
+
if (candidate) {
|
|
2758
|
+
localCandidates.add(candidate);
|
|
2759
|
+
}
|
|
2760
|
+
},
|
|
2761
|
+
JSXClosingElement(jsxPath) {
|
|
2762
|
+
maybeRewriteComponentName(jsxPath.node.name, jsxPath);
|
|
2763
|
+
},
|
|
2764
|
+
Identifier(identifierPath) {
|
|
2765
|
+
if (!identifierPath.isReferencedIdentifier()) {
|
|
2766
|
+
return;
|
|
2767
|
+
}
|
|
2768
|
+
|
|
2769
|
+
if (!helperPaths.has(identifierPath.node.name)) {
|
|
2770
|
+
return;
|
|
2771
|
+
}
|
|
2772
|
+
|
|
2773
|
+
referencedHelpers.add(identifierPath.node.name);
|
|
2774
|
+
},
|
|
2775
|
+
});
|
|
2776
|
+
|
|
2777
|
+
referencedHelpers.forEach((helperName) => {
|
|
2778
|
+
const helperCandidates = scanFunction(helperPaths.get(helperName), nextSeen);
|
|
2779
|
+
helperCandidates.forEach((candidate) => localCandidates.add(candidate));
|
|
2780
|
+
});
|
|
2781
|
+
|
|
2782
|
+
helperCandidateCache.set(path.node, new Set(localCandidates));
|
|
2783
|
+
return localCandidates;
|
|
2784
|
+
}
|
|
2785
|
+
|
|
2786
|
+
scanFunction(functionPath).forEach((candidate) => candidates.add(candidate));
|
|
2512
2787
|
|
|
2513
2788
|
return candidates;
|
|
2514
2789
|
}
|
|
2515
2790
|
|
|
2516
2791
|
exports.createTransformFunctionToClassPlugin = createTransformFunctionToClassPlugin;
|
|
2517
2792
|
exports.default = transformLitsxComponents;
|
|
2793
|
+
exports.isCapitalizedComponentName = isCapitalizedComponentName;
|
|
2518
2794
|
//# sourceMappingURL=transform-litsx-components.cjs.map
|