@herb-tools/linter 0.7.4 → 0.7.5

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 (44) hide show
  1. package/README.md +2 -2
  2. package/dist/herb-lint.js +116 -42
  3. package/dist/herb-lint.js.map +1 -1
  4. package/dist/index.cjs +47 -8
  5. package/dist/index.cjs.map +1 -1
  6. package/dist/index.js +47 -10
  7. package/dist/index.js.map +1 -1
  8. package/dist/package.json +5 -5
  9. package/dist/src/cli/argument-parser.js +4 -2
  10. package/dist/src/cli/argument-parser.js.map +1 -1
  11. package/dist/src/cli.js +2 -1
  12. package/dist/src/cli.js.map +1 -1
  13. package/dist/src/default-rules.js +4 -0
  14. package/dist/src/default-rules.js.map +1 -1
  15. package/dist/src/rules/erb-comment-syntax.js +19 -0
  16. package/dist/src/rules/erb-comment-syntax.js.map +1 -0
  17. package/dist/src/rules/erb-require-whitespace-inside-tags.js +1 -9
  18. package/dist/src/rules/erb-require-whitespace-inside-tags.js.map +1 -1
  19. package/dist/src/rules/erb-right-trim.js +28 -0
  20. package/dist/src/rules/erb-right-trim.js.map +1 -0
  21. package/dist/src/rules/index.js +2 -0
  22. package/dist/src/rules/index.js.map +1 -1
  23. package/dist/src/rules/rule-utils.js.map +1 -1
  24. package/dist/tsconfig.tsbuildinfo +1 -1
  25. package/dist/types/rules/erb-comment-syntax.d.ts +7 -0
  26. package/dist/types/rules/erb-require-whitespace-inside-tags.d.ts +1 -1
  27. package/dist/types/rules/erb-right-trim.d.ts +7 -0
  28. package/dist/types/rules/index.d.ts +2 -0
  29. package/dist/types/src/rules/erb-comment-syntax.d.ts +7 -0
  30. package/dist/types/src/rules/erb-require-whitespace-inside-tags.d.ts +1 -1
  31. package/dist/types/src/rules/erb-right-trim.d.ts +7 -0
  32. package/dist/types/src/rules/index.d.ts +2 -0
  33. package/docs/rules/README.md +3 -1
  34. package/docs/rules/erb-comment-syntax.md +44 -0
  35. package/docs/rules/erb-right-trim.md +57 -0
  36. package/package.json +5 -5
  37. package/src/cli/argument-parser.ts +4 -2
  38. package/src/cli.ts +3 -1
  39. package/src/default-rules.ts +4 -0
  40. package/src/rules/erb-comment-syntax.ts +30 -0
  41. package/src/rules/erb-require-whitespace-inside-tags.ts +4 -12
  42. package/src/rules/erb-right-trim.ts +44 -0
  43. package/src/rules/index.ts +2 -0
  44. package/src/rules/rule-utils.ts +2 -2
@@ -1,20 +1,12 @@
1
- import type { ParseResult, Token, Node } from "@herb-tools/core"
2
- import { isERBNode } from "@herb-tools/core";
3
1
  import { ParserRule } from "../types.js"
4
- import type { LintOffense, LintContext } from "../types.js"
5
2
  import { BaseRuleVisitor } from "./rule-utils.js"
6
3
 
7
- class RequireWhitespaceInsideTags extends BaseRuleVisitor {
4
+ import type { LintOffense, LintContext } from "../types.js"
5
+ import type { ParseResult, Token, ERBNode } from "@herb-tools/core"
8
6
 
9
- visitChildNodes(node: Node): void {
10
- this.checkWhitespace(node)
11
- super.visitChildNodes(node)
12
- }
7
+ class RequireWhitespaceInsideTags extends BaseRuleVisitor {
13
8
 
14
- private checkWhitespace(node: Node): void {
15
- if (!isERBNode(node)) {
16
- return
17
- }
9
+ visitERBNode(node: ERBNode): void {
18
10
  const openTag = node.tag_opening
19
11
  const closeTag = node.tag_closing
20
12
  const content = node.content
@@ -0,0 +1,44 @@
1
+ import { BaseRuleVisitor } from "./rule-utils.js"
2
+ import { ParserRule } from "../types.js"
3
+ import { isERBOutputNode } from "@herb-tools/core"
4
+
5
+ import type { LintOffense, LintContext } from "../types.js"
6
+ import type { ERBNode, ParseResult } from "@herb-tools/core"
7
+
8
+ class ERBRightTrimVisitor extends BaseRuleVisitor {
9
+ visitERBNode(node: ERBNode): void {
10
+ if (!node.tag_closing) return
11
+
12
+ const trimClosing = node.tag_closing.value
13
+
14
+ if (trimClosing !== "=%>" && trimClosing !== "-%>") return
15
+
16
+ if (!isERBOutputNode(node)) {
17
+ this.addOffense(
18
+ `Right-trimming with \`${trimClosing}\` has no effect on non-output ERB tags. Use \`%>\` instead`,
19
+ node.tag_closing.location
20
+ )
21
+
22
+ return
23
+ }
24
+
25
+ if (trimClosing === "=%>") {
26
+ this.addOffense(
27
+ "Use `-%>` instead of `=%>` for right-trimming. The `=%>` syntax is obscure and not well-supported in most ERB engines",
28
+ node.tag_closing.location
29
+ )
30
+ }
31
+ }
32
+ }
33
+
34
+ export class ERBRightTrimRule extends ParserRule {
35
+ name = "erb-right-trim"
36
+
37
+ check(result: ParseResult, context?: Partial<LintContext>): LintOffense[] {
38
+ const visitor = new ERBRightTrimVisitor(this.name, context)
39
+
40
+ visitor.visit(result.value)
41
+
42
+ return visitor.offenses
43
+ }
44
+ }
@@ -1,4 +1,5 @@
1
1
  export * from "./rule-utils.js"
2
+ export * from "./erb-comment-syntax.js"
2
3
  export * from "./erb-no-empty-tags.js"
3
4
  export * from "./erb-no-output-control-flow.js"
4
5
  export * from "./erb-no-silent-tag-in-attribute-name.js"
@@ -30,3 +31,4 @@ export * from "./html-no-title-attribute.js"
30
31
  export * from "./html-tag-name-lowercase.js"
31
32
  export * from "./svg-tag-name-capitalization.js"
32
33
  export * from "./html-no-underscores-in-attribute-names.js"
34
+ export * from "./erb-right-trim.js"
@@ -13,7 +13,7 @@ import {
13
13
  } from "@herb-tools/core"
14
14
 
15
15
  import type {
16
- ERBNode,
16
+ ERBContentNode,
17
17
  HTMLAttributeNameNode,
18
18
  HTMLAttributeNode,
19
19
  HTMLAttributeValueNode,
@@ -300,7 +300,7 @@ export function getAttributeValue(attributeNode: HTMLAttributeNode): string | nu
300
300
  for (const child of valueNode.children) {
301
301
  switch (child.type) {
302
302
  case "AST_ERB_CONTENT_NODE": {
303
- const erbNode = child as ERBNode
303
+ const erbNode = child as ERBContentNode
304
304
 
305
305
  if (erbNode.content) {
306
306
  result += `${erbNode.tag_opening?.value}${erbNode.content.value}${erbNode.tag_closing?.value}`