@herb-tools/rewriter 0.9.5 → 0.9.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@herb-tools/rewriter",
3
- "version": "0.9.5",
3
+ "version": "0.9.6",
4
4
  "description": "Rewriter system for transforming HTML+ERB AST nodes and formatted strings",
5
5
  "license": "MIT",
6
6
  "homepage": "https://herb-tools.dev",
@@ -38,12 +38,12 @@
38
38
  }
39
39
  },
40
40
  "dependencies": {
41
- "@herb-tools/core": "0.9.5",
42
- "@herb-tools/tailwind-class-sorter": "0.9.5",
41
+ "@herb-tools/core": "0.9.6",
42
+ "@herb-tools/tailwind-class-sorter": "0.9.6",
43
43
  "tinyglobby": "^0.2.15"
44
44
  },
45
45
  "devDependencies": {
46
- "@herb-tools/printer": "0.9.5"
46
+ "@herb-tools/printer": "0.9.6"
47
47
  },
48
48
  "files": [
49
49
  "package.json",
@@ -1,4 +1,4 @@
1
- import { Visitor, Location, ERBOpenTagNode, ERBEndNode, HTMLElementNode, HTMLVirtualCloseTagNode, createSyntheticToken } from "@herb-tools/core"
1
+ import { Visitor, Location, ERBOpenTagNode, ERBEndNode, HTMLElementNode, HTMLVirtualCloseTagNode, createSyntheticToken, findPreferredHelperForTag, HELPER_REGISTRY } from "@herb-tools/core"
2
2
  import { getStaticAttributeName, isLiteralNode, isHTMLOpenTagNode, isHTMLTextNode, isHTMLAttributeNode, isERBContentNode, isWhitespaceNode } from "@herb-tools/core"
3
3
 
4
4
  import { ASTRewriter } from "../ast-rewriter.js"
@@ -121,37 +121,39 @@ class HTMLToActionViewTagHelperVisitor extends Visitor {
121
121
  }
122
122
  }
123
123
 
124
- const isAnchor = tagName.value === "a"
125
- const isTurboFrame = tagName.value === "turbo-frame"
126
- const isScript = tagName.value === "script"
127
- const isImg = tagName.value === "img"
124
+ const preferredHelper = findPreferredHelperForTag(tagName.value)
128
125
  const attributes = openTag.children.filter(child => !isWhitespaceNode(child))
129
- const hasSrcAttribute = (isScript || isImg) && attributes.some(child => isHTMLAttributeNode(child) && getStaticAttributeName(child.name!) === "src")
130
- const { attributes: attributesString, href, id, src } = serializeAttributes(attributes, { extractHref: isAnchor, extractId: isTurboFrame, extractSrc: isScript || isImg })
126
+ const implicitAttrName = preferredHelper?.implicitAttribute?.name
127
+ const hasSrcAttribute = attributes.some(child => isHTMLAttributeNode(child) && getStaticAttributeName(child.name!) === "src")
128
+ const { attributes: attributesString, href, id, src } = serializeAttributes(attributes, {
129
+ extractHref: implicitAttrName === "href",
130
+ extractId: implicitAttrName === "id",
131
+ extractSrc: implicitAttrName === "src" || tagName.value === "script",
132
+ })
131
133
  const hasBody = node.body && node.body.length > 0 && !node.is_void
132
134
  const isInlineContent = hasBody && isTextOnlyBody(node.body)
133
135
 
134
136
  let content: string
135
137
  let elementSource: string
136
138
 
137
- if (isAnchor) {
139
+ if (preferredHelper?.name === "link_to") {
138
140
  content = this.buildLinkToContent(node, attributesString, href, isInlineContent)
139
- elementSource = "ActionView::Helpers::UrlHelper#link_to"
140
- } else if (isTurboFrame) {
141
+ elementSource = preferredHelper.source
142
+ } else if (preferredHelper?.name === "turbo_frame_tag") {
141
143
  content = this.buildTurboFrameTagContent(node, attributesString, id, isInlineContent)
142
- elementSource = "Turbo::FramesHelper#turbo_frame_tag"
143
- } else if (isScript && hasSrcAttribute) {
144
+ elementSource = preferredHelper.source
145
+ } else if (preferredHelper?.name === "image_tag") {
146
+ content = this.buildImageTagContent(attributesString, src)
147
+ elementSource = preferredHelper.source
148
+ } else if (tagName.value === "script" && hasSrcAttribute) {
144
149
  content = this.buildJavascriptIncludeTagContent(attributesString, src)
145
- elementSource = "ActionView::Helpers::AssetTagHelper#javascript_include_tag"
146
- } else if (isScript) {
150
+ elementSource = HELPER_REGISTRY["javascript_include_tag"].source
151
+ } else if (tagName.value === "script") {
147
152
  content = this.buildJavascriptTagContent(node, attributesString, isInlineContent)
148
- elementSource = "ActionView::Helpers::JavaScriptHelper#javascript_tag"
149
- } else if (isImg) {
150
- content = this.buildImageTagContent(attributesString, src)
151
- elementSource = "ActionView::Helpers::AssetTagHelper#image_tag"
153
+ elementSource = HELPER_REGISTRY["javascript_tag"].source
152
154
  } else {
153
155
  content = this.buildTagContent(tagName.value, node, attributesString, isInlineContent)
154
- elementSource = "ActionView::Helpers::TagHelper#tag"
156
+ elementSource = HELPER_REGISTRY["tag"].source
155
157
  }
156
158
 
157
159
  const erbOpenTag = new ERBOpenTagNode({
@@ -168,8 +170,10 @@ class HTMLToActionViewTagHelperVisitor extends Visitor {
168
170
  asMutable(node).open_tag = erbOpenTag
169
171
  asMutable(node).element_source = elementSource
170
172
 
173
+ const isScript = tagName.value === "script"
171
174
  const isInlineLiteralContent = isScript && hasBody && node.body.length === 1 && isLiteralNode(node.body[0]) && !node.body[0].content.includes("\n")
172
- const isInlineForm = isInlineContent || isInlineLiteralContent || (isTurboFrame && !hasBody) || (isScript && hasSrcAttribute) || isImg
175
+ const isVoidHelper = preferredHelper?.isVoid ?? node.is_void
176
+ const isInlineForm = isInlineContent || isInlineLiteralContent || isVoidHelper || (preferredHelper?.name === "turbo_frame_tag" && !hasBody) || (isScript && hasSrcAttribute)
173
177
 
174
178
  if (node.is_void) {
175
179
  asMutable(node).close_tag = null