prettier-plugin-ejs 1.0.2 → 1.0.4

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 (3) hide show
  1. package/README.md +2 -1
  2. package/index.js +103 -5
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -67,4 +67,5 @@ Please check [Prettier Docs](https://prettier.io/docs/en/plugins.html) for other
67
67
 
68
68
  ## Note
69
69
 
70
- Tags which contains the greater then symbol `>` are ignored.
70
+ EJS inside `<script>`, `<style>`, `<textarea>` and `<title>` is left untouched.
71
+ Attributes containing EJS are printed as is.
package/index.js CHANGED
@@ -1,15 +1,113 @@
1
1
  /*
2
- * Copyright (c) 1986-2023 Ecmel Ercan (https://ecmel.dev/)
2
+ * Copyright (c) 1986-2024 Ecmel Ercan (https://ecmel.dev/)
3
3
  * Licensed under the MIT License
4
4
  */
5
5
 
6
6
  const { parsers } = require("prettier/parser-html");
7
7
 
8
+ // Elements whose content is left untouched, EJS included.
9
+ const RAW = /^<(script|style|textarea|title)(?=[\s/>])/i;
10
+
11
+ function ejsEnd(text, start) {
12
+ const end = text.indexOf("%>", start + 2);
13
+ return end < 0 ? text.length : end + 2;
14
+ }
15
+
16
+ function fill(length) {
17
+ return "_".repeat(length);
18
+ }
19
+
20
+ // Replaces EJS tags with placeholders of the same length so that every
21
+ // offset in the parsed AST still points at the original text.
22
+ function mask(text) {
23
+ let out = "";
24
+ let i = 0;
25
+ const ranges = [];
26
+
27
+ const copyTo = (end) => {
28
+ out += text.slice(i, end);
29
+ i = end;
30
+ };
31
+
32
+ // Opening tag: EJS becomes attribute-safe filler.
33
+ const tag = () => {
34
+ let quote = null;
35
+ while (i < text.length) {
36
+ if (text.startsWith("<%%", i)) {
37
+ copyTo(i + 3);
38
+ } else if (text.startsWith("<%", i)) {
39
+ const end = ejsEnd(text, i);
40
+ ranges.push([i, end]);
41
+ out += fill(end - i);
42
+ i = end;
43
+ } else {
44
+ const c = text[i];
45
+ if (quote) {
46
+ if (c === quote) quote = null;
47
+ } else if (c === '"' || c === "'") {
48
+ quote = c;
49
+ } else if (c === ">") {
50
+ copyTo(i + 1);
51
+ return;
52
+ }
53
+ copyTo(i + 1);
54
+ }
55
+ }
56
+ };
57
+
58
+ const skipTo = (str) => {
59
+ const end = text.indexOf(str, i);
60
+ copyTo(end < 0 ? text.length : end + str.length);
61
+ };
62
+
63
+ while (i < text.length) {
64
+ if (text.startsWith("<%%", i)) {
65
+ copyTo(i + 3);
66
+ } else if (text.startsWith("<%", i)) {
67
+ const end = ejsEnd(text, i);
68
+ out += "<!" + fill(end - i - 4) + "!>";
69
+ i = end;
70
+ } else if (text.startsWith("<!--", i)) {
71
+ skipTo("-->");
72
+ } else if (text[i] === "<" && /[a-zA-Z]/.test(text[i + 1] || "")) {
73
+ const raw = RAW.exec(text.slice(i, i + 12));
74
+ tag();
75
+ if (raw) {
76
+ const close = text.toLowerCase().indexOf("</" + raw[1].toLowerCase(), i);
77
+ copyTo(close < 0 ? text.length : close);
78
+ }
79
+ } else if (text[i] === "<" && /[/!?]/.test(text[i + 1] || "")) {
80
+ skipTo(">");
81
+ } else {
82
+ copyTo(i + 1);
83
+ }
84
+ }
85
+
86
+ return { text: out, ranges };
87
+ }
88
+
89
+ // Attributes containing EJS are printed verbatim from the original text.
90
+ function restore(node, original, ranges) {
91
+ for (const attr of node.attrs || []) {
92
+ const start = attr.sourceSpan.start.offset;
93
+ const end = attr.sourceSpan.end.offset;
94
+ if (ranges.some(([s, e]) => s < end && e > start)) {
95
+ attr.name = original.slice(start, end);
96
+ attr.value = null;
97
+ attr.namespace = null;
98
+ attr.hasExplicitNamespace = false;
99
+ }
100
+ }
101
+ for (const child of node.children || []) {
102
+ restore(child, original, ranges);
103
+ }
104
+ }
105
+
8
106
  function parse(text, options, legacy) {
9
- const find =
10
- /(?:<(textarea|title|script).*?<\/\1|<\s*[a-zA-Z!].*?[^%/]>|<%([^>]*)%>)/gs;
11
- text = text.replace(find, (match, p1, p2) => (p2 ? `<!${p2}!>` : match));
12
- return parsers.html.parse(text, options, legacy);
107
+ const masked = mask(text);
108
+ const ast = parsers.html.parse(masked.text, options, legacy);
109
+ if (masked.ranges.length) restore(ast, text, masked.ranges);
110
+ return ast;
13
111
  }
14
112
 
15
113
  module.exports = {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "prettier-plugin-ejs",
3
3
  "displayName": "Prettier EJS Plugin",
4
- "version": "1.0.2",
4
+ "version": "1.0.4",
5
5
  "description": "A Prettier plugin for Embedded JavaScript Templating (EJS).",
6
6
  "keywords": [
7
7
  "prettier",