11import type { types as t , Visitor } from "@babel/core"
22
3- import { componentPathAttributeName , formatComponentPathValue } from "./component-path.js"
3+ import { formatComponentPathValue } from "./component-path.js"
44
55/**
66 * Context required for JSX tagging.
@@ -12,6 +12,10 @@ export type JsxTaggerContext = {
1212 * Relative file path from the project root.
1313 */
1414 readonly relativeFilename : string
15+ /**
16+ * Name of the attribute to add (defaults to "data-path").
17+ */
18+ readonly attributeName : string
1519}
1620
1721/**
@@ -41,32 +45,34 @@ export const attrExists = (node: t.JSXOpeningElement, attrName: string, types: t
4145/**
4246 * Creates a JSX attribute with the component path value.
4347 *
48+ * @param attributeName - Name of the attribute to create.
4449 * @param relativeFilename - Relative path to the file.
4550 * @param line - 1-based line number.
4651 * @param column - 0-based column number.
4752 * @param types - Babel types module.
4853 * @returns JSX attribute node with the path value.
4954 *
5055 * @pure true
51- * @invariant attribute name is always componentPathAttributeName
56+ * @invariant attribute name matches the provided attributeName parameter
5257 * @complexity O(1)
5358 */
54- // CHANGE: extract attribute creation as a pure factory .
55- // WHY: single point for attribute creation ensures consistency .
56- // REF: issue-12 (unified interface request )
57- // FORMAT THEOREM: ∀ f, l, c: createPathAttribute(f, l, c) = JSXAttribute(path , f:l:c)
59+ // CHANGE: add attributeName parameter for configurable attribute names .
60+ // WHY: support customizable attribute names while maintaining default "data-path" .
61+ // REF: issue-14 (add attributeName option )
62+ // FORMAT THEOREM: ∀ n, f, l, c: createPathAttribute(n, f, l, c) = JSXAttribute(n , f:l:c)
5863// PURITY: CORE
5964// EFFECT: n/a
60- // INVARIANT: output format is always path:line:column
65+ // INVARIANT: output format is always path:line:column with configurable attribute name
6166// COMPLEXITY: O(1)/O(1)
6267export const createPathAttribute = (
68+ attributeName : string ,
6369 relativeFilename : string ,
6470 line : number ,
6571 column : number ,
6672 types : typeof t
6773) : t . JSXAttribute => {
6874 const value = formatComponentPathValue ( relativeFilename , line , column )
69- return types . jsxAttribute ( types . jsxIdentifier ( componentPathAttributeName ) , types . stringLiteral ( value ) )
75+ return types . jsxAttribute ( types . jsxIdentifier ( attributeName ) , types . stringLiteral ( value ) )
7076}
7177
7278/**
@@ -76,12 +82,12 @@ export const createPathAttribute = (
7682 * Both the Vite plugin and standalone Babel plugin use this function.
7783 *
7884 * @param node - JSX opening element to process.
79- * @param context - Tagging context with relative filename.
85+ * @param context - Tagging context with relative filename and attribute name .
8086 * @param types - Babel types module.
8187 * @returns true if attribute was added, false if skipped.
8288 *
8389 * @pure false (mutates node)
84- * @invariant each JSX element has at most one path attribute after processing
90+ * @invariant each JSX element has at most one instance of the specified attribute after processing
8591 * @complexity O(n) where n = number of existing attributes
8692 */
8793// CHANGE: extract unified JSX element processing logic.
@@ -103,13 +109,13 @@ export const processJsxElement = (
103109 return false
104110 }
105111
106- // Skip if already has path attribute (idempotency)
107- if ( attrExists ( node , componentPathAttributeName , types ) ) {
112+ // Skip if already has the specified attribute (idempotency)
113+ if ( attrExists ( node , context . attributeName , types ) ) {
108114 return false
109115 }
110116
111117 const { column, line } = node . loc . start
112- const attr = createPathAttribute ( context . relativeFilename , line , column , types )
118+ const attr = createPathAttribute ( context . attributeName , context . relativeFilename , line , column , types )
113119
114120 node . attributes . push ( attr )
115121 return true
0 commit comments