@bufbuild/protoplugin 0.2.1 → 0.3.0

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.
@@ -48,7 +48,7 @@ function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsNoCheck)
48
48
  builder.push(`syntax ${file.syntax})\n`);
49
49
  builder.push("/* eslint-disable */\n");
50
50
  if (tsNoCheck) {
51
- builder.push("/* @ts-nocheck */\n");
51
+ builder.push("// @ts-nocheck\n");
52
52
  }
53
53
  builder.push("\n");
54
54
  writeLeadingComments(file.getPackageComments());
@@ -24,8 +24,21 @@ function createGeneratedFile(name, importPath, createTypeImport, runtimeImports,
24
24
  preamble(file) {
25
25
  preamble = (0, gencommon_js_1.makeFilePreamble)(file, preambleSettings.pluginName, preambleSettings.pluginVersion, preambleSettings.parameter, preambleSettings.tsNocheck);
26
26
  },
27
- print(...any) {
28
- printableToEl(any, el, createTypeImport, runtimeImports);
27
+ print(printableOrFragments, ...rest) {
28
+ let printables;
29
+ if (printableOrFragments != null &&
30
+ Object.prototype.hasOwnProperty.call(printableOrFragments, "raw")) {
31
+ // If called with a tagged template literal
32
+ printables = buildPrintablesFromFragments(printableOrFragments, rest);
33
+ }
34
+ else {
35
+ // If called with just an array of Printables
36
+ printables =
37
+ printableOrFragments != null
38
+ ? [printableOrFragments, ...rest]
39
+ : rest;
40
+ }
41
+ printableToEl(printables, el, createTypeImport, runtimeImports);
29
42
  el.push("\n");
30
43
  },
31
44
  export(name) {
@@ -56,7 +69,7 @@ function elToContent(el, importerPath) {
56
69
  const c = [];
57
70
  const symbolToIdentifier = processImports(el, importerPath, (typeOnly, from, names) => {
58
71
  const p = names.map(({ name, alias }) => alias == undefined ? name : `${name} as ${alias}`);
59
- const what = `{${p.join(", ")}}`;
72
+ const what = `{ ${p.join(", ")} }`;
60
73
  if (typeOnly) {
61
74
  c.push(`import type ${what} from ${(0, gencommon_js_1.literalString)(from)};\n`);
62
75
  }
@@ -119,6 +132,16 @@ function printableToEl(printables, el, createTypeImport, runtimeImports) {
119
132
  }
120
133
  }
121
134
  }
135
+ function buildPrintablesFromFragments(fragments, values) {
136
+ const printables = [];
137
+ fragments.forEach((fragment, i) => {
138
+ printables.push(fragment);
139
+ if (fragments.length - 1 !== i) {
140
+ printables.push(values[i]);
141
+ }
142
+ });
143
+ return printables;
144
+ }
122
145
  function processImports(el, importerPath, makeImportStatement) {
123
146
  // identifiers to use in the output
124
147
  const symbolToIdentifier = new Map();
@@ -36,6 +36,7 @@ function createRuntimeImports(bootstrapWkt) {
36
36
  ScalarType: infoToSymbol("ScalarType", bootstrapWkt),
37
37
  MethodKind: infoToSymbol("MethodKind", bootstrapWkt),
38
38
  MethodIdempotency: infoToSymbol("MethodIdempotency", bootstrapWkt),
39
+ IMessageTypeRegistry: infoToSymbol("IMessageTypeRegistry", bootstrapWkt),
39
40
  };
40
41
  }
41
42
  exports.createRuntimeImports = createRuntimeImports;
@@ -45,7 +45,7 @@ export function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsN
45
45
  builder.push(`syntax ${file.syntax})\n`);
46
46
  builder.push("/* eslint-disable */\n");
47
47
  if (tsNoCheck) {
48
- builder.push("/* @ts-nocheck */\n");
48
+ builder.push("// @ts-nocheck\n");
49
49
  }
50
50
  builder.push("\n");
51
51
  writeLeadingComments(file.getPackageComments());
@@ -21,8 +21,21 @@ export function createGeneratedFile(name, importPath, createTypeImport, runtimeI
21
21
  preamble(file) {
22
22
  preamble = makeFilePreamble(file, preambleSettings.pluginName, preambleSettings.pluginVersion, preambleSettings.parameter, preambleSettings.tsNocheck);
23
23
  },
24
- print(...any) {
25
- printableToEl(any, el, createTypeImport, runtimeImports);
24
+ print(printableOrFragments, ...rest) {
25
+ let printables;
26
+ if (printableOrFragments != null &&
27
+ Object.prototype.hasOwnProperty.call(printableOrFragments, "raw")) {
28
+ // If called with a tagged template literal
29
+ printables = buildPrintablesFromFragments(printableOrFragments, rest);
30
+ }
31
+ else {
32
+ // If called with just an array of Printables
33
+ printables =
34
+ printableOrFragments != null
35
+ ? [printableOrFragments, ...rest]
36
+ : rest;
37
+ }
38
+ printableToEl(printables, el, createTypeImport, runtimeImports);
26
39
  el.push("\n");
27
40
  },
28
41
  export(name) {
@@ -52,7 +65,7 @@ function elToContent(el, importerPath) {
52
65
  const c = [];
53
66
  const symbolToIdentifier = processImports(el, importerPath, (typeOnly, from, names) => {
54
67
  const p = names.map(({ name, alias }) => alias == undefined ? name : `${name} as ${alias}`);
55
- const what = `{${p.join(", ")}}`;
68
+ const what = `{ ${p.join(", ")} }`;
56
69
  if (typeOnly) {
57
70
  c.push(`import type ${what} from ${literalString(from)};\n`);
58
71
  }
@@ -115,6 +128,16 @@ function printableToEl(printables, el, createTypeImport, runtimeImports) {
115
128
  }
116
129
  }
117
130
  }
131
+ function buildPrintablesFromFragments(fragments, values) {
132
+ const printables = [];
133
+ fragments.forEach((fragment, i) => {
134
+ printables.push(fragment);
135
+ if (fragments.length - 1 !== i) {
136
+ printables.push(values[i]);
137
+ }
138
+ });
139
+ return printables;
140
+ }
118
141
  function processImports(el, importerPath, makeImportStatement) {
119
142
  // identifiers to use in the output
120
143
  const symbolToIdentifier = new Map();
@@ -33,6 +33,7 @@ export function createRuntimeImports(bootstrapWkt) {
33
33
  ScalarType: infoToSymbol("ScalarType", bootstrapWkt),
34
34
  MethodKind: infoToSymbol("MethodKind", bootstrapWkt),
35
35
  MethodIdempotency: infoToSymbol("MethodIdempotency", bootstrapWkt),
36
+ IMessageTypeRegistry: infoToSymbol("IMessageTypeRegistry", bootstrapWkt),
36
37
  };
37
38
  }
38
39
  function infoToSymbol(name, bootstrapWkt) {
@@ -36,7 +36,13 @@ export interface GeneratedFile {
36
36
  * - ImportSymbol: Adds an import statement and prints the name of the symbol.
37
37
  * - DescMessage or DescEnum: Imports the type if necessary, and prints the name.
38
38
  */
39
- print(...any: Printable[]): void;
39
+ print(...printables: Printable[]): void;
40
+ /**
41
+ * Add a line of code to the file with tagged template literal and
42
+ * an optional array of Printables.
43
+ * See print(Printable[]) for behavior when printing Printable items.
44
+ */
45
+ print(fragments: TemplateStringsArray, ...printables: Printable[]): void;
40
46
  /**
41
47
  * Reserves an identifier in this file.
42
48
  */
@@ -17,5 +17,6 @@ export interface RuntimeImports {
17
17
  ScalarType: ImportSymbol;
18
18
  MethodKind: ImportSymbol;
19
19
  MethodIdempotency: ImportSymbol;
20
+ IMessageTypeRegistry: ImportSymbol;
20
21
  }
21
22
  export declare function createRuntimeImports(bootstrapWkt: boolean): RuntimeImports;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoplugin",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "license": "(Apache-2.0 AND BSD-3-Clause)",
5
5
  "description": "Helps to create your own Protocol Buffers code generators.",
6
6
  "repository": {
@@ -35,7 +35,7 @@
35
35
  }
36
36
  },
37
37
  "dependencies": {
38
- "@bufbuild/protobuf": "0.2.1",
38
+ "@bufbuild/protobuf": "0.3.0",
39
39
  "@typescript/vfs": "^1.4.0",
40
40
  "typescript": "4.5.2"
41
41
  },