|
11 | 11 | * "plugins": ["@prover-coder-ai/component-tagger/babel"] |
12 | 12 | * } |
13 | 13 | */ |
14 | | -// CHANGE: provide CommonJS entry point for Babel plugin. |
15 | | -// WHY: Babel configuration often requires CommonJS modules. |
16 | | -// REF: issue-12 |
| 14 | +// CHANGE: provide CommonJS entry point for Babel plugin with configurable attributeName. |
| 15 | +// WHY: Babel configuration often requires CommonJS modules; support custom attribute names. |
| 16 | +// REF: issue-12, issue-14 |
17 | 17 | // FORMAT THEOREM: forall require: require(babel.cjs) -> PluginFactory |
18 | 18 | // PURITY: SHELL |
19 | 19 | // EFFECT: n/a |
|
22 | 22 |
|
23 | 23 | const path = require("node:path") |
24 | 24 |
|
25 | | -const componentPathAttributeName = "path" |
| 25 | +const componentPathAttributeName = "data-path" |
26 | 26 | const jsxFilePattern = /\.(tsx|jsx)(\?.*)?$/u |
27 | 27 |
|
28 | 28 | const isJsxFile = (id) => jsxFilePattern.test(id) |
@@ -58,21 +58,22 @@ module.exports = function componentTaggerBabelPlugin({ types: t }) { |
58 | 58 | return |
59 | 59 | } |
60 | 60 |
|
61 | | - // Skip if already has path attribute |
62 | | - if (attrExists(node, componentPathAttributeName, t)) { |
63 | | - return |
64 | | - } |
65 | | - |
66 | | - // Compute relative path from root |
| 61 | + // Compute relative path from root and get attribute name |
67 | 62 | const opts = state.opts || {} |
68 | 63 | const rootDir = opts.rootDir || state.cwd || process.cwd() |
| 64 | + const attributeName = opts.attributeName || componentPathAttributeName |
69 | 65 | const relativeFilename = path.relative(rootDir, filename) |
70 | 66 |
|
| 67 | + // Skip if already has the specified attribute (idempotency) |
| 68 | + if (attrExists(node, attributeName, t)) { |
| 69 | + return |
| 70 | + } |
| 71 | + |
71 | 72 | const { column, line } = node.loc.start |
72 | 73 | const value = formatComponentPathValue(relativeFilename, line, column) |
73 | 74 |
|
74 | 75 | node.attributes.push( |
75 | | - t.jsxAttribute(t.jsxIdentifier(componentPathAttributeName), t.stringLiteral(value)) |
| 76 | + t.jsxAttribute(t.jsxIdentifier(attributeName), t.stringLiteral(value)) |
76 | 77 | ) |
77 | 78 | } |
78 | 79 | } |
|
0 commit comments