prettier-plugin-embed 0.0.2 → 0.0.3

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.
@@ -1,6 +1,2 @@
1
- import type { Parser, SupportOptions } from "prettier";
2
- import type { EmbeddedPrinter } from "../types.js";
3
- export declare const embeddedLanguageNames: readonly ["embeddedXml", "embeddedSql"];
4
- export declare const embeddedParsers: Record<string, Parser>;
5
- export declare const embeddedPrinters: Record<string, EmbeddedPrinter>;
6
- export declare const embeddedOptions: SupportOptions;
1
+ export * from "./register.js";
2
+ export type { EmbeddedNames, EmbeddedParsers, EmbeddedEmbedders, EmbeddedOptions, } from "./types.js";
@@ -0,0 +1,2 @@
1
+ export * from "./name.js";
2
+ export * from "./option.js";
@@ -0,0 +1,6 @@
1
+ export declare const name = "embeddedNoop";
2
+ declare module "../types.js" {
3
+ interface EmbeddedNamesHolder {
4
+ [name]: void;
5
+ }
6
+ }
@@ -0,0 +1,15 @@
1
+ import { SupportOption } from "prettier";
2
+ import { name } from "./name.js";
3
+ export declare const option: SupportOption;
4
+ declare module "../types.js" {
5
+ interface EmbeddedOptions {
6
+ [name]: typeof option;
7
+ }
8
+ }
9
+ export interface PrettierPluginEmbedOptions {
10
+ [name]?: string[];
11
+ }
12
+ declare module "prettier" {
13
+ interface Options extends PrettierPluginEmbedOptions {
14
+ }
15
+ }
@@ -0,0 +1,9 @@
1
+ import type { Options } from "prettier";
2
+ import type { Embedder } from "../../types.js";
3
+ import { name } from "./name.js";
4
+ export declare const embedder: Embedder<Options>;
5
+ declare module "../types.js" {
6
+ interface EmbeddedEmbedders {
7
+ [name]: typeof embedder;
8
+ }
9
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./name.js";
2
+ export * from "./embedder.js";
3
+ export * from "./option.js";
@@ -0,0 +1,6 @@
1
+ export declare const name = "embeddedPhp";
2
+ declare module "../types.js" {
3
+ interface EmbeddedNamesHolder {
4
+ [name]: void;
5
+ }
6
+ }
@@ -0,0 +1,20 @@
1
+ import { SupportOption } from "prettier";
2
+ import { name } from "./name.js";
3
+ export declare const option: SupportOption;
4
+ declare module "../types.js" {
5
+ interface EmbeddedOptions {
6
+ [name]: typeof option;
7
+ }
8
+ }
9
+ export interface PrettierPluginDepsOptions {
10
+ phpVersion?: "5.0" | "5.1" | "5.2" | "5.3" | "5.4" | "5.5" | "5.6" | "7.0" | "7.1" | "7.2" | "7.3" | "7.4" | "8.0" | "8.1" | "8.2";
11
+ trailingCommaPHP?: boolean;
12
+ braceStyle?: "psr-2" | "per-cs" | "1tbs";
13
+ }
14
+ export interface PrettierPluginEmbedOptions {
15
+ [name]?: string[];
16
+ }
17
+ declare module "prettier" {
18
+ interface Options extends PrettierPluginDepsOptions, PrettierPluginEmbedOptions {
19
+ }
20
+ }
@@ -0,0 +1,5 @@
1
+ import type { EmbeddedEmbedders, EmbeddedNames, EmbeddedParsers, EmbeddedOptions } from "./types.js";
2
+ export declare const embeddedNames: EmbeddedNames[];
3
+ export declare const embeddedParsers: EmbeddedParsers;
4
+ export declare const embeddedEmbedders: EmbeddedEmbedders;
5
+ export declare const embeddedOptions: EmbeddedOptions;
@@ -0,0 +1,9 @@
1
+ import type { Options } from "prettier";
2
+ import type { Embedder } from "../../types.js";
3
+ import { name } from "./name.js";
4
+ export declare const embedder: Embedder<Options>;
5
+ declare module "../types.js" {
6
+ interface EmbeddedEmbedders {
7
+ [name]: typeof embedder;
8
+ }
9
+ }
@@ -1,4 +1,3 @@
1
1
  export * from "./name.js";
2
- export * from "./printer.js";
2
+ export * from "./embedder.js";
3
3
  export * from "./option.js";
4
- export * from "./types.js";
@@ -1 +1,6 @@
1
1
  export declare const name = "embeddedSql";
2
+ declare module "../types.js" {
3
+ interface EmbeddedNamesHolder {
4
+ [name]: void;
5
+ }
6
+ }
@@ -1,2 +1,21 @@
1
1
  import { SupportOption } from "prettier";
2
- export declare const embeddedOption: SupportOption;
2
+ import type { SqlBaseOptions as PrettierPluginDepsOptions } from "prettier-plugin-sql";
3
+ import { name } from "./name.js";
4
+ export declare const option: SupportOption;
5
+ declare module "../types.js" {
6
+ interface EmbeddedOptions {
7
+ [name]: typeof option;
8
+ }
9
+ }
10
+ export declare const SQL_FORMATTER_LANGUAGES: readonly ["sql", "bigquery", "hive", "mariadb", "mysql", "postgresql", "db2", "plsql", "n1ql", "redshift", "singlestoredb", "spark", "sqlite", "transactsql", "tsql", "trino"];
11
+ export declare const NODE_SQL_PARSER_DATABASES: readonly ["bigquery", "db2", "hive", "mariadb", "mysql", "postgresql", "transactsql", "flinksql"];
12
+ export type SqlFormatterLanguage = (typeof SQL_FORMATTER_LANGUAGES)[number];
13
+ export type NodeSqlParserDataBase = (typeof NODE_SQL_PARSER_DATABASES)[number];
14
+ export { PrettierPluginDepsOptions };
15
+ export interface PrettierPluginEmbedOptions {
16
+ [name]?: SqlFormatterLanguage[] | NodeSqlParserDataBase[] | string[];
17
+ }
18
+ declare module "prettier" {
19
+ interface Options extends PrettierPluginDepsOptions, PrettierPluginEmbedOptions {
20
+ }
21
+ }
@@ -1 +1,14 @@
1
- export * from "./xml/types.js";
1
+ import { Parser, SupportOption, Options } from "prettier";
2
+ import { Embedder } from "../types.js";
3
+ export interface EmbeddedParsers {
4
+ [name: string]: Parser | undefined;
5
+ }
6
+ export interface EmbeddedEmbedders {
7
+ [name: string]: Embedder<Options> | undefined;
8
+ }
9
+ export interface EmbeddedOptions {
10
+ [name: string]: SupportOption | undefined;
11
+ }
12
+ export interface EmbeddedNamesHolder {
13
+ }
14
+ export type EmbeddedNames = keyof EmbeddedNamesHolder;
@@ -1,8 +1,10 @@
1
1
  import { Expression, Comment, TemplateLiteral } from "estree";
2
- import { AstPath } from "prettier";
2
+ import { AstPath, Options } from "prettier";
3
3
  import { builders } from "prettier/doc";
4
4
  import { InternalPrintFun } from "../types.js";
5
5
  export declare function printTemplateExpression(path: AstPath<Expression & {
6
6
  comments?: Comment[];
7
7
  }>, print: InternalPrintFun): builders.Doc[];
8
8
  export declare function printTemplateExpressions(path: AstPath<TemplateLiteral>, print: InternalPrintFun): builders.Doc[][];
9
+ export declare function throwIfPluginIsNotFound(pluginName: string, options: Options, lang: string): void;
10
+ export declare function insertEmbeddedLanguageName(names: string[], name: string, headName: string): void;
@@ -0,0 +1,9 @@
1
+ import type { Options } from "prettier";
2
+ import type { Embedder } from "../../types.js";
3
+ import { name } from "./name.js";
4
+ export declare const embedder: Embedder<Options>;
5
+ declare module "../types.js" {
6
+ interface EmbeddedEmbedders {
7
+ [name]: typeof embedder;
8
+ }
9
+ }
@@ -1,5 +1,4 @@
1
1
  export * from "./name.js";
2
2
  export * from "./parser.js";
3
- export * from "./printer.js";
3
+ export * from "./embedder.js";
4
4
  export * from "./option.js";
5
- export * from "./types.js";
@@ -1 +1,6 @@
1
1
  export declare const name = "embeddedXml";
2
+ declare module "../types.js" {
3
+ interface EmbeddedNamesHolder {
4
+ [name]: void;
5
+ }
6
+ }
@@ -1,2 +1,21 @@
1
1
  import { SupportOption } from "prettier";
2
- export declare const embeddedOption: SupportOption;
2
+ import { name } from "./name.js";
3
+ export declare const option: SupportOption;
4
+ declare module "../types.js" {
5
+ interface EmbeddedOptions {
6
+ [name]: typeof option;
7
+ }
8
+ }
9
+ export interface PrettierPluginDepsOptions {
10
+ xmlSelfClosingSpace?: boolean;
11
+ xmlWhitespaceSensitivity?: "strict" | "preserve" | "ignore";
12
+ xmlSortAttributesByKey?: boolean;
13
+ xmlQuoteAttributes?: "preserve" | "single" | "double";
14
+ }
15
+ export interface PrettierPluginEmbedOptions {
16
+ [name]?: string[];
17
+ }
18
+ declare module "prettier" {
19
+ interface Options extends PrettierPluginDepsOptions, PrettierPluginEmbedOptions {
20
+ }
21
+ }
@@ -1,5 +1,11 @@
1
1
  import type { Parser } from "prettier";
2
2
  import { parse as xmlToolsParse } from "@xml-tools/parser";
3
+ import { name } from "./name.js";
3
4
  type CstNode = ReturnType<typeof xmlToolsParse>["cst"];
4
- export declare const embeddedParser: Parser<CstNode>;
5
+ export declare const parser: Parser<CstNode>;
6
+ declare module "../types.js" {
7
+ interface EmbeddedParsers {
8
+ [name]: Parser;
9
+ }
10
+ }
5
11
  export {};
package/dist/index.d.ts CHANGED
@@ -2,3 +2,4 @@ export { parsers } from "./parsers.js";
2
2
  export { printers } from "./printers.js";
3
3
  export { options } from "./options.js";
4
4
  export type * from "./types.js";
5
+ export * from "./embedded/index.js";
package/dist/index.js CHANGED
@@ -1,151 +1,152 @@
1
- import P from "prettier/parser-babel";
2
- import F from "prettier/parser-espree";
3
- import W from "prettier/parser-flow";
4
- import _ from "prettier/parser-typescript";
5
- import { parse as j } from "@xml-tools/parser";
6
- import { builders as S, utils as I } from "prettier/doc";
7
- import { printers as B } from "prettier/plugins/estree.mjs";
8
- const q = "embeddedXml";
9
- function L(e, t) {
10
- const r = new SyntaxError(
11
- e + " (" + t.loc.start.line + ":" + t.loc.start.column + ")"
12
- );
13
- return Object.assign(r, t);
14
- }
15
- const G = {
16
- parse(e) {
17
- const { lexErrors: t, parseErrors: r, cst: o } = j(e);
18
- if (t.length > 0) {
19
- const n = t[0];
20
- throw L(n.message, {
21
- loc: {
22
- start: { line: n.line, column: n.column },
23
- end: {
24
- line: n.line,
25
- column: n.column + n.length
26
- }
27
- }
28
- });
29
- }
30
- if (r.length > 0) {
31
- const n = r[0];
32
- throw L(n.message, {
33
- loc: {
34
- start: {
35
- line: n.token.startLine ?? NaN,
36
- column: n.token.startColumn ?? NaN
37
- },
38
- end: {
39
- line: n.token.endLine ?? NaN,
40
- column: n.token.endColumn ?? NaN
41
- }
42
- }
43
- });
44
- }
45
- return o;
46
- },
47
- astFormat: "xml",
48
- locStart(e) {
49
- return e.location.startOffset;
50
- },
51
- locEnd(e) {
52
- return e.location.endOffset ?? NaN;
53
- }
54
- };
55
- let $;
56
- const V = new Uint8Array(16);
57
- function Q() {
58
- if (!$ && ($ = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !$))
1
+ import H from "prettier/parser-babel";
2
+ import X from "prettier/parser-espree";
3
+ import J from "prettier/parser-flow";
4
+ import K from "prettier/parser-typescript";
5
+ import { builders as E, utils as N } from "prettier/doc";
6
+ import { parse as Y } from "@xml-tools/parser";
7
+ import { printers as Z } from "prettier/plugins/estree.mjs";
8
+ const C = "embeddedNoop", ee = {
9
+ category: "Global",
10
+ type: "string",
11
+ array: !0,
12
+ default: [{ value: [] }],
13
+ description: "Specify embedded languages that will not be formatted."
14
+ }, te = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
15
+ __proto__: null,
16
+ name: C,
17
+ option: ee
18
+ }, Symbol.toStringTag, { value: "Module" })), ne = "embeddedPhp";
19
+ let S;
20
+ const re = new Uint8Array(16);
21
+ function oe() {
22
+ if (!S && (S = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !S))
59
23
  throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
60
- return $(V);
24
+ return S(re);
61
25
  }
62
- const s = [];
26
+ const c = [];
63
27
  for (let e = 0; e < 256; ++e)
64
- s.push((e + 256).toString(16).slice(1));
65
- function z(e, t = 0) {
66
- return s[e[t + 0]] + s[e[t + 1]] + s[e[t + 2]] + s[e[t + 3]] + "-" + s[e[t + 4]] + s[e[t + 5]] + "-" + s[e[t + 6]] + s[e[t + 7]] + "-" + s[e[t + 8]] + s[e[t + 9]] + "-" + s[e[t + 10]] + s[e[t + 11]] + s[e[t + 12]] + s[e[t + 13]] + s[e[t + 14]] + s[e[t + 15]];
28
+ c.push((e + 256).toString(16).slice(1));
29
+ function se(e, t = 0) {
30
+ return c[e[t + 0]] + c[e[t + 1]] + c[e[t + 2]] + c[e[t + 3]] + "-" + c[e[t + 4]] + c[e[t + 5]] + "-" + c[e[t + 6]] + c[e[t + 7]] + "-" + c[e[t + 8]] + c[e[t + 9]] + "-" + c[e[t + 10]] + c[e[t + 11]] + c[e[t + 12]] + c[e[t + 13]] + c[e[t + 14]] + c[e[t + 15]];
67
31
  }
68
- const M = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), U = {
69
- randomUUID: M
32
+ const ie = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), I = {
33
+ randomUUID: ie
70
34
  };
71
- function O(e, t, r) {
72
- if (U.randomUUID && !t && !e)
73
- return U.randomUUID();
35
+ function k(e, t, n) {
36
+ if (I.randomUUID && !t && !e)
37
+ return I.randomUUID();
74
38
  e = e || {};
75
- const o = e.random || (e.rng || Q)();
39
+ const o = e.random || (e.rng || oe)();
76
40
  if (o[6] = o[6] & 15 | 64, o[8] = o[8] & 63 | 128, t) {
77
- r = r || 0;
78
- for (let n = 0; n < 16; ++n)
79
- t[r + n] = o[n];
41
+ n = n || 0;
42
+ for (let r = 0; r < 16; ++r)
43
+ t[n + r] = o[r];
80
44
  return t;
81
45
  }
82
- return z(o);
46
+ return se(o);
83
47
  }
84
- const { group: X, indent: H, softline: k, lineSuffixBoundary: J } = S;
85
- function K(e, t) {
86
- var n;
87
- const { node: r } = e;
48
+ const { group: le, indent: ce, softline: U, lineSuffixBoundary: ae } = E;
49
+ function ue(e, t) {
50
+ var r;
51
+ const { node: n } = e;
88
52
  let o = t();
89
- return (n = r == null ? void 0 : r.comments) != null && n.length && (o = X([H([k, o]), k])), ["${", o, J, "}"];
53
+ return (r = n == null ? void 0 : n.comments) != null && r.length && (o = le([ce([U, o]), U])), ["${", o, ae, "}"];
90
54
  }
91
- function C(e, t) {
55
+ function j(e, t) {
92
56
  return e.map(
93
- (r) => K(r, t),
57
+ (n) => ue(n, t),
94
58
  "expressions"
95
59
  );
96
60
  }
97
- const { label: Y, hardline: Z, line: ee, group: E, indent: te } = S, { mapDoc: ne } = I, re = async (e, t, r, o) => {
61
+ function A(e, t, n) {
62
+ var o;
63
+ if (!(((o = t.plugins) == null ? void 0 : o.map((r) => r.name)) ?? []).includes(e))
64
+ throw new Error(
65
+ `Cannot format embedded language "${n}" because plugin "${e}" is not loaded.`
66
+ );
67
+ }
68
+ function de(e, t, n) {
69
+ if (t === n) {
70
+ e.unshift(t);
71
+ return;
72
+ }
73
+ let o = 0, r = e.length;
74
+ for (; o < r; ) {
75
+ const s = o + r >>> 1;
76
+ if (e[s] === n) {
77
+ e.push(t);
78
+ return;
79
+ }
80
+ e[s] < t ? o = s + 1 : r = s;
81
+ }
82
+ e.splice(o, 0, t);
83
+ }
84
+ const { label: pe, hardline: me, line: ge, group: w, indent: fe } = E, { mapDoc: be } = N, ye = async (e, t, n, o, r) => {
98
85
  try {
99
- const n = O(), { node: i } = r, p = i.quasis.map(
100
- (l, c, { length: u }) => c === u - 1 ? l.value.cooked : l.value.cooked + `${n}-${c}`
86
+ A("@prettier/plugin-php", o, r);
87
+ const s = "$p" + k().replaceAll("-", ""), { node: a } = n, b = a.quasis.map(
88
+ (i, l, { length: m }) => l === m - 1 ? i.value.cooked : i.value.cooked + `${s}${l}`
101
89
  ).join("");
102
- let d = "", g = 0;
103
- const f = /^\s+/.exec(p);
104
- f && (g = f.index + f[0].length, d = " ");
105
- const y = /\s$/.test(p) ? " " : "", b = C(r, t), N = await e(p.slice(g), {
106
- parser: q
107
- }), x = ne(N, (l) => {
108
- if (typeof l != "string")
109
- return l;
110
- const c = [], u = l.split(new RegExp(n + "-(\\d+)", "g"));
111
- for (let m = 0; m < u.length; m++) {
112
- let a = u[m];
113
- if (m % 2 == 0) {
114
- if (!a)
90
+ let d = "", y = 0;
91
+ const g = /^\s+/.exec(b);
92
+ g && (y = g.index + g[0].length, d = " ");
93
+ const x = /\s$/.test(b) ? " " : "", _ = j(n, t), q = await e(b.slice(y), {
94
+ parser: "php"
95
+ });
96
+ let h = !1;
97
+ const $ = be(q, (i) => {
98
+ if (typeof i != "string")
99
+ return Array.isArray(i) || (i.type === "group" && i.break || i.type === "line" && (i.hard || i.literal)) && (h = !0), i;
100
+ const l = [], m = i.split(new RegExp("\\" + s + "(\\d+)", "g"));
101
+ for (let u = 0; u < m.length; u++) {
102
+ let f = m[u];
103
+ if (u % 2 == 0) {
104
+ if (!f)
115
105
  continue;
116
- a = a.replaceAll(/([\\`]|\${)/g, "\\$1"), c.push(a);
106
+ f = f.replaceAll(/([\\`]|\${)/g, "\\$1"), l.push(f);
117
107
  } else {
118
- const T = Number(a);
119
- c.push(b[T]);
108
+ const v = Number(f);
109
+ l.push(_[v]);
120
110
  }
121
111
  }
122
- return c;
123
- }), h = o.xmlWhitespaceSensitivity === "ignore" ? Z : d && y ? ee : null;
124
- return h ? E([
112
+ return l;
113
+ }), p = h ? me : d && x ? ge : null;
114
+ return p ? w([
125
115
  "`",
126
- te([h, E(x)]),
127
- h,
116
+ fe([p, w($)]),
117
+ p,
128
118
  "`"
129
- ]) : Y(
119
+ ]) : pe(
130
120
  { hug: !1 },
131
- E([
121
+ w([
132
122
  "`",
133
123
  d,
134
- E(x),
135
- y,
124
+ w([$]),
125
+ x,
136
126
  "`"
137
127
  ])
138
128
  );
139
- } catch (n) {
140
- throw console.error(n), n;
129
+ } catch (s) {
130
+ throw console.error(s), s;
141
131
  }
142
- }, oe = {
132
+ }, he = {
143
133
  category: "Global",
144
134
  type: "string",
145
135
  array: !0,
146
- default: [{ value: ["xml", "svg"] }],
147
- description: "Config embedded XML formatting. This requires @prettier/plugin-xml"
148
- }, D = "embeddedSql", A = [
136
+ default: [{ value: ["php"] }],
137
+ description: "Specify embedded PHP languages. This requires @prettier/plugin-php"
138
+ }, $e = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
139
+ __proto__: null,
140
+ embedder: ye,
141
+ name: ne,
142
+ option: he
143
+ }, Symbol.toStringTag, { value: "Module" })), xe = "embeddedSql", _e = {
144
+ category: "Global",
145
+ type: "string",
146
+ array: !0,
147
+ default: [{ value: ["sql"] }],
148
+ description: "Specify embedded SQL languages. This requires prettier-plugin-sql"
149
+ }, G = [
149
150
  "sql",
150
151
  "bigquery",
151
152
  "hive",
@@ -162,7 +163,7 @@ const { label: Y, hardline: Z, line: ee, group: E, indent: te } = S, { mapDoc: n
162
163
  "transactsql",
163
164
  "tsql",
164
165
  "trino"
165
- ], R = [
166
+ ], W = [
166
167
  "bigquery",
167
168
  "db2",
168
169
  "hive",
@@ -171,149 +172,296 @@ const { label: Y, hardline: Z, line: ee, group: E, indent: te } = S, { mapDoc: n
171
172
  "postgresql",
172
173
  "transactsql",
173
174
  "flinksql"
174
- ], { label: se, hardline: ie, group: w } = S, { mapDoc: le } = I, ce = async (e, t, r, o, n) => {
175
+ ], { label: ve, hardline: R, group: O, line: qe, indent: Ee } = E, { mapDoc: Se } = N, we = async (e, t, n, o, r, s) => {
175
176
  try {
176
- const i = "p" + O().replaceAll("-", ""), { node: p } = r, d = p.quasis.map(
177
- (l, c, { length: u }) => c === u - 1 ? l.value.cooked : l.value.cooked + `${i}${c}`
178
- ).join(""), g = /^\s/.test(d) ? " " : "", f = /\s$/.test(d) ? " " : "", y = C(r, t);
179
- n = n.toLowerCase();
180
- let b = {};
181
- if (o.formatter === "sql-formatter")
182
- if (A.includes(n))
183
- b = {
184
- formatter: "sql-formatter",
185
- language: n
186
- };
187
- else if (R.includes(n))
188
- b = {
189
- formatter: "node-sql-parser",
190
- database: n
191
- };
192
- else
193
- throw new SyntaxError(`Unrecognized language: ${n}`);
194
- else if (R.includes(n))
195
- b = {
196
- formatter: "node-sql-parser",
197
- database: n
198
- };
199
- else if (A.includes(n))
200
- b = {
201
- formatter: "sql-formatter",
202
- language: n
203
- };
204
- else
205
- throw new SyntaxError(`Unrecognized language: ${n}`);
206
- const N = await e(d, {
177
+ A("prettier-plugin-sql", o, r);
178
+ const a = "p" + k().replaceAll("-", ""), { node: b } = n, d = b.quasis.map(
179
+ (i, l, { length: m }) => l === m - 1 ? i.value.cooked : i.value.cooked + `${a}${l}`
180
+ ).join(""), y = /^\s/.test(d) ? " " : "", g = /\s$/.test(d) ? " " : "", x = j(n, t), _ = Te(o, r, s);
181
+ if (typeof _ > "u")
182
+ throw new SyntaxError(`Unrecognized language: ${r}`);
183
+ const q = await e(d, {
207
184
  parser: "sql",
208
- ...b
209
- }), x = le(N, (l) => {
210
- if (typeof l != "string")
211
- return l;
212
- const c = [], u = l.split(new RegExp(i + "(\\d+)", "g"));
213
- for (let m = 0; m < u.length; m++) {
214
- let a = u[m];
215
- if (m % 2 == 0) {
216
- if (!a)
185
+ ..._
186
+ });
187
+ let h = !1;
188
+ const $ = Se(q, (i) => {
189
+ if (typeof i != "string")
190
+ return i;
191
+ const l = [], m = i.split(new RegExp(a + "(\\d+)", "g"));
192
+ for (let u = 0; u < m.length; u++) {
193
+ let f = m[u];
194
+ if (u % 2 == 0) {
195
+ if (!f)
217
196
  continue;
218
- a = a.replaceAll(/([\\`]|\${)/g, "\\$1"), c.push(a);
197
+ f = f.replaceAll(/([\\`]|\${)/g, "\\$1"), f.split(/(\n)/).forEach(
198
+ (v) => v === `
199
+ ` ? (h = !0, l.push(R)) : l.push(v)
200
+ );
219
201
  } else {
220
- const T = Number(a);
221
- c.push(y[T]);
202
+ const v = Number(f);
203
+ l.push(x[v]);
222
204
  }
223
205
  }
224
- return c;
225
- }), h = ie;
226
- return h ? w(["`", h, w(x), h, "`"]) : se(
206
+ return l;
207
+ }), p = h ? R : y && g ? qe : null;
208
+ return p ? O([
209
+ "`",
210
+ Ee([p, O($)]),
211
+ p,
212
+ "`"
213
+ ]) : ve(
227
214
  { hug: !1 },
228
- w([
215
+ O([
229
216
  "`",
217
+ y,
218
+ O($),
230
219
  g,
231
- w(x),
232
- f,
233
220
  "`"
234
221
  ])
235
222
  );
236
- } catch (i) {
237
- throw console.error(i), i;
223
+ } catch (a) {
224
+ throw console.error(a), a;
238
225
  }
239
- }, ae = {
226
+ }, Oe = {
227
+ "sql-formatter": (e) => G.includes(e),
228
+ "node-sql-parser": (e) => W.includes(e)
229
+ };
230
+ function B(e, t) {
231
+ for (const n of t)
232
+ if (Oe[n](e))
233
+ return {
234
+ formatter: n,
235
+ language: e
236
+ };
237
+ }
238
+ function F(e, t, n) {
239
+ let o = B(e, n);
240
+ if (o)
241
+ return o;
242
+ const r = t.indexOf(e);
243
+ for (let s = r - 1; s >= 0; --s)
244
+ if (o = B(t[s], n), o)
245
+ return o;
246
+ }
247
+ function Te(e, t, n) {
248
+ return e.formatter === "sql-formatter" ? F(t, n, [
249
+ "sql-formatter",
250
+ "node-sql-parser"
251
+ ]) : F(t, n, [
252
+ "node-sql-parser",
253
+ "sql-formatter"
254
+ ]);
255
+ }
256
+ const De = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
257
+ __proto__: null,
258
+ NODE_SQL_PARSER_DATABASES: W,
259
+ SQL_FORMATTER_LANGUAGES: G,
260
+ embedder: we,
261
+ name: xe,
262
+ option: _e
263
+ }, Symbol.toStringTag, { value: "Module" })), M = "embeddedXml";
264
+ function P(e, t) {
265
+ const n = new SyntaxError(
266
+ e + " (" + t.loc.start.line + ":" + t.loc.start.column + ")"
267
+ );
268
+ return Object.assign(n, t);
269
+ }
270
+ const Ne = {
271
+ parse(e) {
272
+ const { lexErrors: t, parseErrors: n, cst: o } = Y(e);
273
+ if (t.length > 0) {
274
+ const r = t[0];
275
+ throw P(r.message, {
276
+ loc: {
277
+ start: { line: r.line, column: r.column },
278
+ end: {
279
+ line: r.line,
280
+ column: r.column + r.length
281
+ }
282
+ }
283
+ });
284
+ }
285
+ if (n.length > 0) {
286
+ const r = n[0];
287
+ throw P(r.message, {
288
+ loc: {
289
+ start: {
290
+ line: r.token.startLine ?? NaN,
291
+ column: r.token.startColumn ?? NaN
292
+ },
293
+ end: {
294
+ line: r.token.endLine ?? NaN,
295
+ column: r.token.endColumn ?? NaN
296
+ }
297
+ }
298
+ });
299
+ }
300
+ return o;
301
+ },
302
+ astFormat: "xml",
303
+ locStart(e) {
304
+ return e.location.startOffset;
305
+ },
306
+ locEnd(e) {
307
+ return e.location.endOffset ?? NaN;
308
+ }
309
+ }, { label: ke, hardline: je, line: Ae, group: T, indent: Le } = E, { mapDoc: Ie } = N, Ue = async (e, t, n, o, r) => {
310
+ try {
311
+ A("@prettier/plugin-xml", o, r);
312
+ const s = k(), { node: a } = n, b = a.quasis.map(
313
+ (p, i, { length: l }) => i === l - 1 ? p.value.cooked : p.value.cooked + `${s}-${i}`
314
+ ).join("");
315
+ let d = "", y = 0;
316
+ const g = /^\s+/.exec(b);
317
+ g && (y = g.index + g[0].length, d = " ");
318
+ const x = /\s$/.test(b) ? " " : "", _ = j(n, t), q = await e(b.slice(y), {
319
+ parser: M
320
+ }), h = Ie(q, (p) => {
321
+ if (typeof p != "string")
322
+ return p;
323
+ const i = [], l = p.split(new RegExp(s + "-(\\d+)", "g"));
324
+ for (let m = 0; m < l.length; m++) {
325
+ let u = l[m];
326
+ if (m % 2 == 0) {
327
+ if (!u)
328
+ continue;
329
+ u = u.replaceAll(/([\\`]|\${)/g, "\\$1"), i.push(u);
330
+ } else {
331
+ const f = Number(u);
332
+ i.push(_[f]);
333
+ }
334
+ }
335
+ return i;
336
+ }), $ = o.xmlWhitespaceSensitivity === "ignore" ? je : d && x ? Ae : null;
337
+ return $ ? T([
338
+ "`",
339
+ Le([$, T(h)]),
340
+ $,
341
+ "`"
342
+ ]) : ke(
343
+ { hug: !1 },
344
+ T([
345
+ "`",
346
+ d,
347
+ T(h),
348
+ x,
349
+ "`"
350
+ ])
351
+ );
352
+ } catch (s) {
353
+ throw console.error(s), s;
354
+ }
355
+ }, Re = {
240
356
  category: "Global",
241
357
  type: "string",
242
358
  array: !0,
243
- default: [{ value: ["sql"] }],
244
- description: "Config embedded SQL formatting. This requires prettier-plugin-sql"
245
- }, de = [
246
- q,
247
- D
248
- ], ue = {
249
- [q]: G
250
- }, me = {
251
- [q]: re,
252
- [D]: ce
253
- }, pe = {
254
- [q]: oe,
255
- [D]: ae
256
- }, ve = {
359
+ default: [{ value: ["xml", "svg"] }],
360
+ description: "Specify embedded XML languages. This requires @prettier/plugin-xml"
361
+ }, Be = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
362
+ __proto__: null,
363
+ embedder: Ue,
364
+ name: M,
365
+ option: Re,
366
+ parser: Ne
367
+ }, Symbol.toStringTag, { value: "Module" })), L = [], z = {}, V = {}, Q = {}, Fe = /* @__PURE__ */ Object.assign({
368
+ "./noop/index.ts": te,
369
+ "./php/index.ts": $e,
370
+ "./sql/index.ts": De,
371
+ "./xml/index.ts": Be
372
+ });
373
+ Object.values(Fe).forEach(({ name: e, parser: t, embedder: n, option: o }) => {
374
+ de(L, e, C), t && (z[e] = t), n && (V[e] = n), Q[e] = o;
375
+ });
376
+ const Je = {
257
377
  // parsers from internal
258
- ...P.parsers,
259
- ...F.parsers,
260
- ...W.parsers,
261
- ..._.parsers,
262
- // parsers from plugin
263
- ...ue
264
- }, { estree: v } = B, ge = function(e, t) {
378
+ // TODO: find out which parsers are required and which are optional
379
+ ...H.parsers,
380
+ ...X.parsers,
381
+ ...J.parsers,
382
+ ...K.parsers,
383
+ // parsers from this plugin
384
+ ...z
385
+ };
386
+ console.log(L);
387
+ const { estree: D } = Z, Pe = function(e, t) {
265
388
  var o;
266
- const { node: r } = e;
267
- if (r.type !== "TemplateLiteral" || r.quasis.some(({ value: { cooked: n } }) => n === null))
389
+ const { node: n } = e;
390
+ if (n.type !== "TemplateLiteral" || n.quasis.some(({ value: { cooked: r } }) => r === null))
268
391
  return null;
269
- for (const n of de) {
270
- const i = t[n];
271
- if (!i)
392
+ for (const r of L) {
393
+ const s = t[r];
394
+ if (!s)
272
395
  continue;
273
- const p = fe(e, i) ?? ye(e, i);
274
- if (p === void 0)
396
+ const a = Ce(
397
+ e,
398
+ s,
399
+ t.disableEmbeddedDetectionByComment ?? []
400
+ ) ?? Ge(e, s, t.disableEmbeddedDetectionByTag ?? []);
401
+ if (a === void 0)
275
402
  continue;
276
- const d = me[n], g = e.node;
277
- return g.quasis.length === 1 && g.quasis[0].value.raw.trim() === "" ? "``" : async (...f) => {
278
- const y = await d(...f, p);
279
- return S.label(
280
- { embed: !0, ...y.label },
281
- y
403
+ const b = V[r];
404
+ if (!b)
405
+ return null;
406
+ const d = e.node;
407
+ return d.quasis.length === 1 && d.quasis[0].value.raw.trim() === "" ? "``" : async (...y) => {
408
+ const g = await b(...y, a, s);
409
+ return E.label(
410
+ { embed: !0, ...g.label },
411
+ g
282
412
  );
283
413
  };
284
414
  }
285
- return ((o = v.embed) == null ? void 0 : o.call(v, e, t)) ?? null;
415
+ return ((o = D.embed) == null ? void 0 : o.call(D, e, t)) ?? null;
286
416
  };
287
- function fe({ node: e, parent: t }, r) {
288
- if (r.length === 0 || e.type !== "TemplateLiteral")
417
+ function Ce({ node: e, parent: t }, n, o) {
418
+ if (n.length === 0 || e.type !== "TemplateLiteral")
289
419
  return;
290
- const o = e.comments ?? (t == null ? void 0 : t.comments);
291
- if (!o)
420
+ const r = e.comments ?? (t == null ? void 0 : t.comments);
421
+ if (!r)
292
422
  return;
293
- const n = o[o.length - 1];
294
- if (!(n.type !== "Block" || !n.leading)) {
295
- for (const i of r)
296
- if (` ${i} ` === n.value)
297
- return i;
423
+ const s = r[r.length - 1];
424
+ if (!(s.type !== "Block" || !s.leading)) {
425
+ for (const a of n)
426
+ if (` ${a} ` === s.value && !o.includes(a))
427
+ return a;
298
428
  }
299
429
  }
300
- function ye({ node: e, parent: t }, r) {
301
- if (r.length !== 0 && !(e.type !== "TemplateLiteral" || (t == null ? void 0 : t.type) !== "TaggedTemplateExpression" || t.tag.type !== "Identifier")) {
302
- for (const o of r)
303
- if (t.tag.name === o)
304
- return o;
430
+ function Ge({ node: e, parent: t }, n, o) {
431
+ if (n.length !== 0 && !(e.type !== "TemplateLiteral" || (t == null ? void 0 : t.type) !== "TaggedTemplateExpression" || t.tag.type !== "Identifier")) {
432
+ for (const r of n)
433
+ if (t.tag.name === r && !o.includes(r))
434
+ return r;
305
435
  }
306
436
  }
307
- const Se = {
437
+ const Ke = {
308
438
  estree: {
309
- ...v,
310
- embed: ge
439
+ ...D,
440
+ embed: Pe
441
+ }
442
+ }, Ye = {
443
+ ...Q,
444
+ disableEmbeddedDetectionByComment: {
445
+ category: "Global",
446
+ type: "string",
447
+ array: !0,
448
+ default: [{ value: [] }],
449
+ description: 'This option turns off "/* lang */`...`" comment-based language detection for the specified languages.'
450
+ },
451
+ disableEmbeddedDetectionByTag: {
452
+ category: "Global",
453
+ type: "string",
454
+ array: !0,
455
+ default: [{ value: [] }],
456
+ description: 'This option turns off "lang`...`" tag-based language detection for the specified languages.'
311
457
  }
312
- }, Ne = {
313
- ...pe
314
458
  };
315
459
  export {
316
- Ne as options,
317
- ve as parsers,
318
- Se as printers
460
+ V as embeddedEmbedders,
461
+ L as embeddedNames,
462
+ Q as embeddedOptions,
463
+ z as embeddedParsers,
464
+ Ye as options,
465
+ Je as parsers,
466
+ Ke as printers
319
467
  };
package/dist/options.d.ts CHANGED
@@ -1,2 +1,8 @@
1
1
  import type { SupportOptions } from "prettier";
2
2
  export declare const options: SupportOptions;
3
+ declare module "prettier" {
4
+ interface Options {
5
+ disableEmbeddedDetectionByComment?: string[];
6
+ disableEmbeddedDetectionByTag?: string[];
7
+ }
8
+ }
package/dist/parsers.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export declare const parsers: {
2
+ embeddedXml: import("prettier").Parser<any>;
2
3
  typescript: import("prettier").Parser<any>;
3
4
  flow: import("prettier").Parser<any>;
4
5
  acorn: import("prettier").Parser<any>;
@@ -1,2 +1,2 @@
1
- import { type Plugin } from "prettier";
1
+ import type { Plugin } from "prettier";
2
2
  export declare const printers: Plugin["printers"];
package/dist/types.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { Options, Doc, AstPath } from "prettier";
2
2
  import type { Node as EsTreeNode, TemplateLiteral, Comment } from "estree";
3
- export * from "./embedded/types.js";
4
3
  export type PrettierNode = EsTreeNode & {
5
4
  comments?: (Comment & {
6
5
  leading: boolean;
@@ -9,4 +8,4 @@ export type PrettierNode = EsTreeNode & {
9
8
  })[];
10
9
  };
11
10
  export type InternalPrintFun = (selector?: string | number | (string | number)[] | AstPath<TemplateLiteral>) => Doc;
12
- export type EmbeddedPrinter<T extends Options = Options> = (textToDoc: (text: string, options: T) => Promise<Doc>, print: InternalPrintFun, path: AstPath<TemplateLiteral>, options: T, lang: string) => Promise<Doc>;
11
+ export type Embedder<T extends Options = Options> = (textToDoc: (text: string, options: T) => Promise<Doc>, print: InternalPrintFun, path: AstPath<TemplateLiteral>, options: T, lang: string, langs: string[]) => Promise<Doc>;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "prettier-plugin-embed",
3
3
  "description": "A prettier plugin to format embedded languages in js/ts files.",
4
4
  "private": false,
5
- "version": "0.0.2",
5
+ "version": "0.0.3",
6
6
  "type": "module",
7
7
  "files": [
8
8
  "./dist"
@@ -54,15 +54,14 @@
54
54
  "ncu": "npx npm-check-updates -u",
55
55
  "postncu": "npm i"
56
56
  },
57
- "peerDependencies": {
58
- "@xml-tools/parser": "^1.0.11",
59
- "prettier": "^3.0.3",
60
- "@prettier/plugin-xml": "^3.2.1",
61
- "prettier-plugin-sql": "^0.15.1"
57
+ "dependencies": {
58
+ "uuid": "^9.0.1"
62
59
  },
63
60
  "devDependencies": {
64
61
  "@commitlint/cli": "^17.7.2",
65
62
  "@commitlint/config-conventional": "^17.7.0",
63
+ "@prettier/plugin-php": "^0.20.1",
64
+ "@prettier/plugin-xml": "^3.2.1",
66
65
  "@semantic-release/git": "^10.0.1",
67
66
  "@types/estree": "^1.0.2",
68
67
  "@types/uuid": "^9.0.4",
@@ -73,14 +72,16 @@
73
72
  "@vitest/ui": "^0.34.6",
74
73
  "code-tag": "^1.1.0",
75
74
  "eslint": "^8.50.0",
76
- "npm-check-updates": "^16.14.4",
75
+ "npm-check-updates": "^16.14.5",
77
76
  "playwright": "^1.38.1",
77
+ "prettier-plugin-sql": "^0.15.1",
78
78
  "semantic-release": "^22.0.5",
79
79
  "typescript": "^5.2.2",
80
- "vite": "^4.4.10",
80
+ "vite": "^4.4.11",
81
81
  "vitest": "^0.34.6"
82
82
  },
83
- "dependencies": {
84
- "uuid": "^9.0.1"
83
+ "peerDependencies": {
84
+ "@xml-tools/parser": "^1.0.11",
85
+ "prettier": "^3.0.3"
85
86
  }
86
87
  }
@@ -1,3 +0,0 @@
1
- import type { Options } from "prettier";
2
- import type { EmbeddedPrinter } from "../../types.js";
3
- export declare const embeddedPrinter: EmbeddedPrinter<Options>;
@@ -1,13 +0,0 @@
1
- import { name } from "./name.js";
2
- import { SqlBaseOptions as PrettierPluginSqlOptions } from "prettier-plugin-sql";
3
- export declare const SQL_FORMATTER_LANGUAGES: readonly ["sql", "bigquery", "hive", "mariadb", "mysql", "postgresql", "db2", "plsql", "n1ql", "redshift", "singlestoredb", "spark", "sqlite", "transactsql", "tsql", "trino"];
4
- export declare const NODE_SQL_PARSER_DATABASES: readonly ["bigquery", "db2", "hive", "mariadb", "mysql", "postgresql", "transactsql", "flinksql"];
5
- export type SqlFormatterLanguage = (typeof SQL_FORMATTER_LANGUAGES)[number];
6
- export type NodeSqlParserDataBase = (typeof NODE_SQL_PARSER_DATABASES)[number];
7
- export interface PrettierPluginEmbedSqlOptions {
8
- [name]?: SqlFormatterLanguage[] | NodeSqlParserDataBase[];
9
- }
10
- declare module "prettier" {
11
- interface Options extends PrettierPluginSqlOptions, PrettierPluginEmbedSqlOptions {
12
- }
13
- }
@@ -1,3 +0,0 @@
1
- import type { Options } from "prettier";
2
- import type { EmbeddedPrinter } from "../../types.js";
3
- export declare const embeddedPrinter: EmbeddedPrinter<Options>;
@@ -1,14 +0,0 @@
1
- import { name } from "./name.js";
2
- export interface PrettierPluginXmlOptions {
3
- xmlSelfClosingSpace?: boolean;
4
- xmlWhitespaceSensitivity?: "strict" | "preserve" | "ignore";
5
- xmlSortAttributesByKey?: boolean;
6
- xmlQuoteAttributes?: "preserve" | "single" | "double";
7
- }
8
- export interface PrettierPluginEmbedXmlOptions {
9
- [name]?: string[];
10
- }
11
- declare module "prettier" {
12
- interface Options extends PrettierPluginXmlOptions, PrettierPluginEmbedXmlOptions {
13
- }
14
- }