@herb-tools/rewriter 0.9.4 → 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.4",
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.4",
42
- "@herb-tools/tailwind-class-sorter": "0.9.4",
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.4"
46
+ "@herb-tools/printer": "0.9.6"
47
47
  },
48
48
  "files": [
49
49
  "package.json",
@@ -1,4 +1,4 @@
1
- import { Visitor, isERBOutputNode, createLiteral, createERBOutputNode, findParentArray } from "@herb-tools/core"
1
+ import { Visitor, isERBOutputNode, createLiteral, createERBOutputNode, findParentArray, isPrismNodeType } from "@herb-tools/core"
2
2
 
3
3
  import { ASTRewriter } from "../ast-rewriter.js"
4
4
 
@@ -102,9 +102,7 @@ export class ERBStringToDirectOutputRewriter extends ASTRewriter {
102
102
  }
103
103
 
104
104
  static isStringOutputNode(prismNode: PrismNode): boolean {
105
- const nodeType = prismNode.constructor.name
106
-
107
- return nodeType === STRING_NODE_TYPE || nodeType === INTERPOLATED_STRING_NODE_TYPE
105
+ return isPrismNodeType(prismNode, STRING_NODE_TYPE) || isPrismNodeType(prismNode, INTERPOLATED_STRING_NODE_TYPE)
108
106
  }
109
107
 
110
108
  static extractStringContent(stringNode: PrismNode, source: string): string {
@@ -136,14 +134,12 @@ export class ERBStringToDirectOutputRewriter extends ASTRewriter {
136
134
  }
137
135
 
138
136
  static extractReplacementParts(prismNode: PrismNode, source: string): ReplacementPart[] | null {
139
- const nodeType = prismNode.constructor.name
140
-
141
- if (nodeType === STRING_NODE_TYPE) {
137
+ if (isPrismNodeType(prismNode, STRING_NODE_TYPE)) {
142
138
  const textContent = this.extractStringContent(prismNode, source)
143
139
  return [{ type: "text", content: textContent }]
144
140
  }
145
141
 
146
- if (nodeType === INTERPOLATED_STRING_NODE_TYPE) {
142
+ if (isPrismNodeType(prismNode, INTERPOLATED_STRING_NODE_TYPE)) {
147
143
  const parts = prismNode.parts
148
144
 
149
145
  if (!parts || parts.length === 0) return null
@@ -151,15 +147,13 @@ export class ERBStringToDirectOutputRewriter extends ASTRewriter {
151
147
  const replacementParts: ReplacementPart[] = []
152
148
 
153
149
  for (const part of parts) {
154
- const partType = part.constructor.name
155
-
156
- if (partType === STRING_NODE_TYPE) {
150
+ if (isPrismNodeType(part, STRING_NODE_TYPE)) {
157
151
  const textContent = this.extractStringContent(part, source)
158
152
 
159
153
  if (textContent) {
160
154
  replacementParts.push({ type: "text", content: textContent })
161
155
  }
162
- } else if (partType === EMBEDDED_STATEMENTS_NODE_TYPE) {
156
+ } else if (isPrismNodeType(part, EMBEDDED_STATEMENTS_NODE_TYPE)) {
163
157
  const expression = this.extractExpressionSource(part, source)
164
158
 
165
159
  if (expression) {
@@ -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