@css-panda/ast
Advanced tools
Comparing version 0.0.0-dev-20220913060050 to 0.0.0-dev-20220913093624
@@ -31,4 +31,6 @@ import * as swc from '@swc/core'; | ||
declare function dynamicPlugin(data: Map<string, Set<PluginResult>>, moduleName: string, fileName?: string): (program: swc.Program) => swc.Program; | ||
declare function jsxPlugin(data: Map<string, Set<PluginResult>>, moduleName: string, fileName?: string): (program: swc.Program) => swc.Program; | ||
declare function createCollector(): { | ||
sx: Set<PluginResult>; | ||
jsx: Set<PluginResult>; | ||
css: Set<PluginResult>; | ||
@@ -51,2 +53,2 @@ globalStyle: Set<PluginResult>; | ||
export { PurgeVisitor, createCollector, createPlugins, cssMapPlugin, cssPlugin, dynamicPlugin, fontFacePlugin, globalStylePlugin, minifyConfig, sxPlugin, transform, transformFile, transformFileSync, transformSync }; | ||
export { PurgeVisitor, createCollector, createPlugins, cssMapPlugin, cssPlugin, dynamicPlugin, fontFacePlugin, globalStylePlugin, jsxPlugin, minifyConfig, sxPlugin, transform, transformFile, transformFileSync, transformSync }; |
@@ -37,2 +37,3 @@ "use strict"; | ||
globalStylePlugin: () => globalStylePlugin, | ||
jsxPlugin: () => jsxPlugin, | ||
minifyConfig: () => minifyConfig, | ||
@@ -74,7 +75,8 @@ sxPlugin: () => sxPlugin, | ||
// src/plugins.ts | ||
var import_logger2 = require("@css-panda/logger"); | ||
var import_is_valid_prop = require("@css-panda/is-valid-prop"); | ||
var import_logger3 = require("@css-panda/logger"); | ||
// src/visitor.ts | ||
var import_logger = require("@css-panda/logger"); | ||
// src/jsx-visitor.ts | ||
var import_Visitor = __toESM(require("@swc/core/Visitor")); | ||
var import_lodash2 = __toESM(require("lodash.merge")); | ||
@@ -139,2 +141,43 @@ // src/ast.ts | ||
} | ||
function jsxAttribute(node, result = {}) { | ||
const key = (0, import_ts_pattern.match)(node.name).with({ type: "Identifier", value: import_ts_pattern.P.select() }, (node2) => node2).otherwise(() => void 0); | ||
if (!key) | ||
return result; | ||
(0, import_ts_pattern.match)(node.value).with({ type: import_ts_pattern.P.union("StringLiteral", "NumericLiteral") }, (node2) => { | ||
result[key] = node2.value; | ||
}).with( | ||
{ | ||
type: "JSXExpressionContainer", | ||
expression: { type: "ObjectExpression" } | ||
}, | ||
(node2) => { | ||
(0, import_lodash.default)(result, { [key]: objectExpression(node2.expression) }); | ||
} | ||
).with( | ||
{ | ||
type: "JSXExpressionContainer", | ||
expression: { type: "ArrayExpression" } | ||
}, | ||
(node2) => { | ||
(0, import_lodash.default)(result, { [key]: arrayExpression(node2.expression) }); | ||
} | ||
).with( | ||
{ | ||
type: "JSXExpressionContainer", | ||
expression: { type: "ConditionalExpression" } | ||
}, | ||
(node2) => { | ||
result.conditions || (result.conditions = []); | ||
const consequent = expression(node2.expression.consequent); | ||
const alternate = expression(node2.expression.alternate); | ||
if (consequent.length) { | ||
result.conditions.push({ [key]: consequent[0] }); | ||
} | ||
if (alternate.length) { | ||
result.conditions.push({ [key]: alternate[0] }); | ||
} | ||
} | ||
).run(); | ||
return result; | ||
} | ||
function callExpression(node, scope) { | ||
@@ -218,4 +261,6 @@ const result = []; | ||
// src/visitor.ts | ||
var CallVisitor = class extends import_Visitor.default { | ||
// src/jsx-visitor.ts | ||
var import_ts_pattern2 = require("ts-pattern"); | ||
var import_logger = require("@css-panda/logger"); | ||
var JSXPropVisitor = class extends import_Visitor.default { | ||
constructor(ctx) { | ||
@@ -225,2 +270,5 @@ super(); | ||
} | ||
visitTsType(t) { | ||
return t; | ||
} | ||
import; | ||
@@ -238,2 +286,52 @@ visitImportDeclaration(node) { | ||
} | ||
visitJSXOpeningElement(node) { | ||
var _a, _b; | ||
if (!this.import) | ||
return node; | ||
const isValidType = (0, import_ts_pattern2.match)(node.name).with({ object: { type: "Identifier", value: this.import.alias } }, () => true).otherwise(() => false); | ||
if (!isValidType) | ||
return node; | ||
const attrs = node.attributes.filter((attr) => { | ||
return (0, import_ts_pattern2.match)(attr).with( | ||
{ | ||
type: "JSXAttribute", | ||
name: { type: "Identifier", name: { value: import_ts_pattern2.P.when(this.ctx.isValidProp) } } | ||
}, | ||
() => true | ||
).otherwise(() => false); | ||
}); | ||
const result = {}; | ||
for (const attr of attrs) { | ||
if (attr.type === "SpreadElement") | ||
continue; | ||
(0, import_lodash2.default)(result, jsxAttribute(attr)); | ||
} | ||
(_b = (_a = this.ctx).onData) == null ? void 0 : _b.call(_a, { | ||
type: "object", | ||
data: result | ||
}); | ||
return node; | ||
} | ||
}; | ||
// src/visitor.ts | ||
var import_logger2 = require("@css-panda/logger"); | ||
var import_Visitor2 = __toESM(require("@swc/core/Visitor")); | ||
var CallVisitor = class extends import_Visitor2.default { | ||
constructor(ctx) { | ||
super(); | ||
this.ctx = ctx; | ||
} | ||
import; | ||
visitImportDeclaration(node) { | ||
const result = importDeclaration(node, this.ctx.import); | ||
if (result) { | ||
import_logger2.logger.debug({ | ||
type: "ast:import", | ||
msg: `Found import { ${result.identifer} } in ${this.ctx.import.filename}` | ||
}); | ||
this.import = result; | ||
} | ||
return node; | ||
} | ||
visitCallExpression(node) { | ||
@@ -270,3 +368,3 @@ var _a, _b, _c, _d; | ||
}; | ||
var DynamicCallVisitor = class extends import_Visitor.default { | ||
var DynamicCallVisitor = class extends import_Visitor2.default { | ||
constructor(ctx) { | ||
@@ -280,3 +378,3 @@ super(); | ||
if (result) { | ||
import_logger.logger.debug({ | ||
import_logger2.logger.debug({ | ||
type: "ast:import", | ||
@@ -326,3 +424,3 @@ msg: `Found import { ${result.map((t) => t.alias).join(", ")} } in ${this.ctx.import.filename}` | ||
onData(result) { | ||
import_logger2.logger.debug({ type: `ast:${name}`, fileName, result }); | ||
import_logger3.logger.debug({ type: `ast:${name}`, fileName, result }); | ||
data.add(result); | ||
@@ -345,3 +443,3 @@ } | ||
onDynamicData(name, result) { | ||
import_logger2.logger.debug({ type: `ast:${name}`, fileName, result }); | ||
import_logger3.logger.debug({ type: `ast:${name}`, fileName, result }); | ||
data.set(name, data.get(name) || /* @__PURE__ */ new Set()); | ||
@@ -354,5 +452,22 @@ data.get(name).add(result); | ||
} | ||
function jsxPlugin(data, moduleName, fileName) { | ||
return function(program) { | ||
const visitor = new JSXPropVisitor({ | ||
import: { name: "panda", module: moduleName, filename: fileName }, | ||
onDynamicData(name, result) { | ||
import_logger3.logger.debug({ type: `ast:${name}`, fileName, result }); | ||
data.set(name, data.get(name) || /* @__PURE__ */ new Set()); | ||
data.get(name).add(result); | ||
}, | ||
isValidProp(prop) { | ||
return (0, import_is_valid_prop.isValidCSSProp)(prop); | ||
} | ||
}); | ||
return visitor.visitProgram(program); | ||
}; | ||
} | ||
function createCollector() { | ||
return { | ||
sx: /* @__PURE__ */ new Set(), | ||
jsx: /* @__PURE__ */ new Set(), | ||
css: /* @__PURE__ */ new Set(), | ||
@@ -376,4 +491,3 @@ globalStyle: /* @__PURE__ */ new Set(), | ||
cssMapPlugin(data.cssMap, importMap.css, fileName), | ||
dynamicPlugin(data.recipe, importMap.recipe, fileName), | ||
dynamicPlugin(data.pattern, importMap.pattern, fileName) | ||
dynamicPlugin(data.recipe, importMap.recipe, fileName) | ||
]; | ||
@@ -384,5 +498,5 @@ } | ||
var import_core = require("@swc/core"); | ||
var import_Visitor2 = __toESM(require("@swc/core/Visitor")); | ||
var import_ts_pattern2 = require("ts-pattern"); | ||
var isTransformMethod = (0, import_ts_pattern2.isMatching)({ | ||
var import_Visitor3 = __toESM(require("@swc/core/Visitor")); | ||
var import_ts_pattern3 = require("ts-pattern"); | ||
var isTransformMethod = (0, import_ts_pattern3.isMatching)({ | ||
type: "MethodProperty", | ||
@@ -394,4 +508,4 @@ key: { | ||
}); | ||
var isValuesProperty = (0, import_ts_pattern2.isMatching)({ | ||
type: import_ts_pattern2.P.union("MethodProperty", "KeyValueProperty", "ArrayExpression"), | ||
var isValuesProperty = (0, import_ts_pattern3.isMatching)({ | ||
type: import_ts_pattern3.P.union("MethodProperty", "KeyValueProperty", "ArrayExpression"), | ||
key: { | ||
@@ -406,3 +520,3 @@ type: "Identifier", | ||
} | ||
var PurgeVisitor = class extends import_Visitor2.default { | ||
var PurgeVisitor = class extends import_Visitor3.default { | ||
visitObjectExpression(node) { | ||
@@ -430,3 +544,3 @@ const inner = (node2) => { | ||
node2.properties.forEach((property) => { | ||
(0, import_ts_pattern2.match)(property).with({ type: "KeyValueProperty", value: { type: "ObjectExpression" } }, (property2) => { | ||
(0, import_ts_pattern3.match)(property).with({ type: "KeyValueProperty", value: { type: "ObjectExpression" } }, (property2) => { | ||
inner(property2.value); | ||
@@ -474,2 +588,3 @@ }).with({ type: "KeyValueProperty", value: { type: "ArrayExpression" } }, (property2) => { | ||
globalStylePlugin, | ||
jsxPlugin, | ||
minifyConfig, | ||
@@ -476,0 +591,0 @@ sxPlugin, |
{ | ||
"name": "@css-panda/ast", | ||
"version": "0.0.0-dev-20220913060050", | ||
"version": "0.0.0-dev-20220913093624", | ||
"description": "The static parser for css panda", | ||
@@ -15,4 +15,5 @@ "main": "dist/index.js", | ||
"@swc/core": "1.2.248", | ||
"@css-panda/logger": "0.0.0-dev-20220913060050", | ||
"@css-panda/types": "0.0.0-dev-20220913060050", | ||
"@css-panda/logger": "0.0.0-dev-20220913093624", | ||
"@css-panda/is-valid-prop": "0.0.0-dev-20220913093624", | ||
"@css-panda/types": "0.0.0-dev-20220913093624", | ||
"ts-pattern": "4.0.5", | ||
@@ -22,3 +23,3 @@ "lodash.merge": "4.6.2" | ||
"devDependencies": { | ||
"@css-panda/fixture": "0.0.0-dev-20220913060050", | ||
"@css-panda/fixture": "0.0.0-dev-20220913093624", | ||
"@types/lodash.merge": "4.6.7" | ||
@@ -25,0 +26,0 @@ }, |
Sorry, the diff of this file is not supported yet
39062
1142
6
+ Added@css-panda/logger@0.0.0-dev-20220913093624(transitive)
+ Added@css-panda/types@0.0.0-dev-20220913093624(transitive)
- Removed@css-panda/logger@0.0.0-dev-20220913060050(transitive)
- Removed@css-panda/types@0.0.0-dev-20220913060050(transitive)