@elenajs/plugin-cem-define 0.0.3 → 0.2.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.
Files changed (2) hide show
  1. package/package.json +10 -3
  2. package/src/index.js +35 -11
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@elenajs/plugin-cem-define",
3
- "version": "0.0.3",
4
- "description": "CEM analyzer plugin that extracts tagName from Elena() mixin calls.",
3
+ "version": "0.2.0",
4
+ "description": "CEM analyzer plugin that registers the Elena components in the manifest.",
5
5
  "author": "Elena <hi@elenajs.com>",
6
6
  "homepage": "https://elenajs.com/",
7
7
  "license": "MIT",
@@ -15,11 +15,18 @@
15
15
  "files": [
16
16
  "src"
17
17
  ],
18
+ "scripts": {
19
+ "test": "vitest run"
20
+ },
18
21
  "engines": {
19
22
  "node": ">= 20"
20
23
  },
21
24
  "peerDependencies": {
22
25
  "@custom-elements-manifest/analyzer": ">=0.10.0"
23
26
  },
24
- "gitHead": "9bdf7174131361e71b5f0340cbfd58f3b5e424d1"
27
+ "devDependencies": {
28
+ "@custom-elements-manifest/analyzer": "0.11.0",
29
+ "vitest": "4.0.18"
30
+ },
31
+ "gitHead": "81fe2fc78111f605854c07df2001f746231d9dfc"
25
32
  }
package/src/index.js CHANGED
@@ -1,3 +1,17 @@
1
+ /**
2
+ * ██████████ ████
3
+ * ░░███░░░░░█░░███
4
+ * ░███ █ ░ ███ ██████ ████████ ██████
5
+ * ░██████ ███ ███░░███░░███░░███ ░░░░░███
6
+ * ░███░░█ ███ ░███████ ░███ ░███ ███████
7
+ * ░███ ░ █ ███ ░███░░░ ░███ ░███ ███░░███
8
+ * ██████████ █████░░██████ ████ █████░░████████
9
+ * ░░░░░░░░░░ ░░░░░ ░░░░░░ ░░░░ ░░░░░ ░░░░░░░░
10
+ *
11
+ * Elena CEM Define Plugin
12
+ * https://elenajs.com
13
+ */
14
+
1
15
  /**
2
16
  * CEM analyzer plugin that reads `tagName` from the options object passed to
3
17
  * `Elena(superClass, { tagName: "...", ... })` and registers the element in the manifest.
@@ -23,11 +37,9 @@ export function elenaDefinePlugin() {
23
37
  ts.isObjectLiteralExpression(node.initializer)
24
38
  ) {
25
39
  const varName = node.name.getText();
26
- const tagNameProp = node.initializer.properties.find(
27
- p => ts.isPropertyAssignment(p) && p.name.getText() === "tagName"
28
- );
29
- if (tagNameProp && ts.isStringLiteral(tagNameProp.initializer)) {
30
- optionsMap.set(varName, tagNameProp.initializer.text);
40
+ const tagName = getTagName(ts, node.initializer);
41
+ if (tagName) {
42
+ optionsMap.set(varName, tagName);
31
43
  }
32
44
  return;
33
45
  }
@@ -41,6 +53,7 @@ export function elenaDefinePlugin() {
41
53
  return;
42
54
  }
43
55
 
56
+ // Skip classes that don't extend anything (no `extends` clause)
44
57
  const heritageClause = node.heritageClauses?.[0];
45
58
  if (!heritageClause) {
46
59
  return;
@@ -61,12 +74,7 @@ export function elenaDefinePlugin() {
61
74
 
62
75
  if (ts.isObjectLiteralExpression(optionsArg)) {
63
76
  // Inline: Elena(HTMLElement, { tagName: "...", ... })
64
- const tagNameProp = optionsArg.properties.find(
65
- p => ts.isPropertyAssignment(p) && p.name.getText() === "tagName"
66
- );
67
- if (tagNameProp && ts.isStringLiteral(tagNameProp.initializer)) {
68
- tagName = tagNameProp.initializer.text;
69
- }
77
+ tagName = getTagName(ts, optionsArg);
70
78
  } else if (ts.isIdentifier(optionsArg)) {
71
79
  // Variable reference: Elena(HTMLElement, options)
72
80
  tagName = optionsMap.get(optionsArg.getText());
@@ -91,3 +99,19 @@ export function elenaDefinePlugin() {
91
99
  },
92
100
  };
93
101
  }
102
+
103
+ /**
104
+ * Extracts the `tagName` string value from an object literal expression.
105
+ * @param {import("typescript")} ts
106
+ * @param {import("typescript").ObjectLiteralExpression} objectLiteral
107
+ * @returns {string | undefined}
108
+ */
109
+ function getTagName(ts, objectLiteral) {
110
+ const prop = objectLiteral.properties.find(
111
+ p => ts.isPropertyAssignment(p) && p.name.getText() === "tagName"
112
+ );
113
+ if (prop && ts.isStringLiteral(prop.initializer)) {
114
+ return prop.initializer.text;
115
+ }
116
+ return undefined;
117
+ }