@lwc-bolts/sf2ts 0.0.5 → 1.0.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.
Files changed (98) hide show
  1. package/LICENSE.md +7 -0
  2. package/README.md +285 -0
  3. package/dist/apex/apexdoc.d.ts +19 -0
  4. package/dist/apex/apexdoc.js +79 -0
  5. package/dist/apex/file.d.ts +37 -0
  6. package/dist/apex/file.js +623 -0
  7. package/dist/apex/library.config.d.ts +23 -0
  8. package/dist/apex/library.config.js +17 -0
  9. package/dist/apex/library.d.ts +54 -0
  10. package/dist/apex/library.generated.d.ts +2 -0
  11. package/dist/apex/library.generated.js +215 -0
  12. package/dist/apex/library.js +47 -0
  13. package/dist/apex/namespace.d.ts +32 -0
  14. package/dist/apex/namespace.js +41 -0
  15. package/dist/apex/resolution.d.ts +77 -0
  16. package/dist/apex/resolution.js +306 -0
  17. package/dist/apex/sobjects.d.ts +45 -0
  18. package/dist/apex/sobjects.js +141 -0
  19. package/dist/context.d.ts +16 -0
  20. package/dist/context.js +28 -0
  21. package/dist/converters/apex-class.d.ts +7 -0
  22. package/dist/converters/apex-class.js +23 -0
  23. package/dist/converters/content-asset.d.ts +3 -0
  24. package/dist/converters/content-asset.js +20 -0
  25. package/dist/converters/custom-field.d.ts +3 -0
  26. package/dist/converters/custom-field.js +107 -0
  27. package/dist/converters/custom-label.d.ts +10 -0
  28. package/dist/converters/custom-label.js +36 -0
  29. package/dist/converters/custom-labels.d.ts +3 -0
  30. package/dist/converters/custom-labels.js +10 -0
  31. package/dist/converters/custom-object.d.ts +3 -0
  32. package/dist/converters/custom-object.js +37 -0
  33. package/dist/converters/custom-permission.d.ts +3 -0
  34. package/dist/converters/custom-permission.js +21 -0
  35. package/dist/converters/lightning-component-bundle.d.ts +7 -0
  36. package/dist/converters/lightning-component-bundle.js +31 -0
  37. package/dist/converters/lightning-message-channel.d.ts +10 -0
  38. package/dist/converters/lightning-message-channel.js +39 -0
  39. package/dist/converters/registry.d.ts +22 -0
  40. package/dist/converters/registry.js +18 -0
  41. package/dist/converters/static-resource.d.ts +3 -0
  42. package/dist/converters/static-resource.js +20 -0
  43. package/dist/diagnostics.d.ts +5 -0
  44. package/dist/diagnostics.js +18 -0
  45. package/dist/identifiers.d.ts +12 -0
  46. package/dist/identifiers.js +19 -0
  47. package/dist/index.d.ts +34 -0
  48. package/dist/index.js +47 -0
  49. package/dist/jsdoc.d.ts +14 -0
  50. package/dist/jsdoc.js +28 -0
  51. package/dist/lightning-component-bundle/component-source.d.ts +6 -0
  52. package/dist/lightning-component-bundle/component-source.js +21 -0
  53. package/dist/lightning-component-bundle/metadata.d.ts +36 -0
  54. package/dist/lightning-component-bundle/metadata.js +93 -0
  55. package/dist/lightning-component-bundle/templates/ast.d.ts +47 -0
  56. package/dist/lightning-component-bundle/templates/ast.js +46 -0
  57. package/dist/lightning-component-bundle/templates/base-components.d.ts +6 -0
  58. package/dist/lightning-component-bundle/templates/base-components.js +17 -0
  59. package/dist/lightning-component-bundle/templates/declarations.d.ts +30 -0
  60. package/dist/lightning-component-bundle/templates/declarations.js +82 -0
  61. package/dist/lightning-component-bundle/templates/expressions.d.ts +16 -0
  62. package/dist/lightning-component-bundle/templates/expressions.js +33 -0
  63. package/dist/lightning-component-bundle/templates/helpers.d.ts +10 -0
  64. package/dist/lightning-component-bundle/templates/helpers.js +47 -0
  65. package/dist/lightning-component-bundle/templates/slots.d.ts +24 -0
  66. package/dist/lightning-component-bundle/templates/slots.js +78 -0
  67. package/dist/lightning-component-bundle/templates/source.d.ts +36 -0
  68. package/dist/lightning-component-bundle/templates/source.js +81 -0
  69. package/dist/lightning-component-bundle/templates/tags.d.ts +41 -0
  70. package/dist/lightning-component-bundle/templates/tags.js +36 -0
  71. package/dist/lightning-component-bundle/templates/template.d.ts +53 -0
  72. package/dist/lightning-component-bundle/templates/template.js +518 -0
  73. package/dist/lightning-component-bundle/templates/writer.d.ts +9 -0
  74. package/dist/lightning-component-bundle/templates/writer.js +32 -0
  75. package/dist/module-declaration.d.ts +27 -0
  76. package/dist/module-declaration.js +28 -0
  77. package/dist/names.d.ts +39 -0
  78. package/dist/names.js +67 -0
  79. package/dist/text-builder.d.ts +34 -0
  80. package/dist/text-builder.js +46 -0
  81. package/dist/types.d.ts +128 -0
  82. package/dist/types.js +3 -0
  83. package/dist/xml.d.ts +32 -0
  84. package/dist/xml.js +80 -0
  85. package/package.json +37 -26
  86. package/README.MD +0 -5
  87. package/lib/converts/_index.mjs +0 -8
  88. package/lib/converts/apex-class.mjs +0 -469
  89. package/lib/converts/content-asset.mjs +0 -48
  90. package/lib/converts/custom-field.mjs +0 -63
  91. package/lib/converts/custom-label.mjs +0 -61
  92. package/lib/converts/custom-labels.mjs +0 -34
  93. package/lib/converts/custom-object.mjs +0 -51
  94. package/lib/converts/custom-permission.mjs +0 -51
  95. package/lib/converts/static-resource.mjs +0 -56
  96. package/lib/index.mjs +0 -51
  97. package/lib/util.mjs +0 -68
  98. package/lib-cjs/bundle.cjs +0 -962
@@ -0,0 +1,81 @@
1
+ export class TemplateSource {
2
+ text;
3
+ #options;
4
+ /** Text nodes in a run of text share its location, so each is found after the previous, by the run's start. */
5
+ #cursors = new Map();
6
+ constructor(text, options) {
7
+ this.text = text;
8
+ this.#options = options;
9
+ }
10
+ /** The expression in an attribute or directive spanning `location`, if there is one. */
11
+ attribute(location) {
12
+ const brace = this.text.indexOf("{", location.start);
13
+ return brace !== -1 && brace < location.end ? { brace, at: (offset) => brace + offset } : undefined;
14
+ }
15
+ /**
16
+ * The expression of the next text node in a run of text, or undefined for a literal, which only moves
17
+ * the cursor. The compiler tidies a run's lines before parsing it, so a node's `raw` is in that text, not
18
+ * the template's.
19
+ *
20
+ * Without complex expressions, an expression is on one line, so `raw` is in the template as it is, and
21
+ * its offsets are relative to `raw`.
22
+ *
23
+ * With them, its offsets are relative to the tidied run. Each `{` in the text starts an expression, so
24
+ * the next one is this node's, and from it `raw` lines up with the template but for whitespace.
25
+ */
26
+ textExpression(raw, expression, location) {
27
+ const cursor = this.#cursors.get(location.start) ?? { source: location.start, tidied: 0 };
28
+ this.#cursors.set(location.start, cursor);
29
+ const tidied = cursor.tidied;
30
+ cursor.tidied += raw.length;
31
+ if (expression === undefined) {
32
+ return undefined;
33
+ }
34
+ if (!this.#options.complexExpressions) {
35
+ const position = this.text.indexOf(raw, cursor.source);
36
+ if (position === -1 || position >= location.end) {
37
+ return undefined;
38
+ }
39
+ cursor.source = position + raw.length;
40
+ return { brace: position, at: (offset) => position + offset };
41
+ }
42
+ const brace = this.text.indexOf("{", cursor.source);
43
+ if (brace === -1 || brace >= location.end) {
44
+ return undefined;
45
+ }
46
+ const offsets = this.#align(raw, brace);
47
+ cursor.source = offsets[raw.length];
48
+ return { brace, at: (offset) => offsets[offset - tidied] };
49
+ }
50
+ /** For each offset of `raw`, and its end, the template offset of the same text, starting at `start`. */
51
+ #align(raw, start) {
52
+ const offsets = [];
53
+ let position = start;
54
+ for (const character of raw) {
55
+ while (this.text[position] !== character && isSpace(this.text[position])) {
56
+ position++;
57
+ }
58
+ // a space the compiler made of a line break has nothing of its own in the template
59
+ offsets.push(position);
60
+ if (this.text[position] === character) {
61
+ position++;
62
+ }
63
+ }
64
+ offsets.push(position);
65
+ return offsets;
66
+ }
67
+ /** The offset of some literal text in a run of text, if it's there as it is. */
68
+ literal(text, location) {
69
+ const position = this.text.indexOf(text, location.start);
70
+ return position === -1 || position >= location.end ? undefined : position;
71
+ }
72
+ /** The offset of a name in an attribute such as `for:item="row"`, after its `=` (or its `:`). */
73
+ name(name, location, after) {
74
+ const marker = this.text.indexOf(after, location.start);
75
+ const position = marker === -1 ? -1 : this.text.indexOf(name, marker);
76
+ return position !== -1 && position + name.length <= location.end ? position : undefined;
77
+ }
78
+ }
79
+ function isSpace(character) {
80
+ return character !== undefined && /\s/.test(character);
81
+ }
@@ -0,0 +1,41 @@
1
+ import type { LightningComponent } from "@lwc-bolts/lightning-base-components";
2
+ import type { BaseElement } from "./ast.ts";
3
+ /**
4
+ * Salesforce's own namespaces of components that templates use, which aren't installed packages':
5
+ * `lightning`'s are described by LIGHTNING_COMPONENTS, and the others aren't described at all. It isn't
6
+ * every one there is, so a tag of one that's missing is reported as a component the project doesn't have.
7
+ */
8
+ export declare const PLATFORM_NAMESPACES: Set<string>;
9
+ /** The DOM type of a plain element, by its namespace, as `<a>` in an `<svg>` isn't an HTML `<a>`. */
10
+ export declare function elementType(node: BaseElement): string;
11
+ /** What a tag is, which decides how its properties, events and refs are checked. */
12
+ export type TagKind =
13
+ /** A plain HTML, SVG or MathML element. */
14
+ {
15
+ kind: "element";
16
+ }
17
+ /** One of the project's components: `name` is what the project's lookups know it by. */
18
+ | {
19
+ kind: "child";
20
+ name: string;
21
+ module: string;
22
+ }
23
+ /** An installed package's component, whose source is hidden. */
24
+ | {
25
+ kind: "package";
26
+ module: string;
27
+ }
28
+ /** A base component with a definition. */
29
+ | {
30
+ kind: "base";
31
+ tag: string;
32
+ }
33
+ /** Anything nothing describes, such as `<slot>`, `<lwc:component>` or another Salesforce namespace's component. */
34
+ | {
35
+ kind: "other";
36
+ };
37
+ /**
38
+ * What a tag is. `tagModule` is the compiler's, as a tag names what the compiled template imports.
39
+ * Installed packages' components are of any namespace but `c`, the project's own and Salesforce's.
40
+ */
41
+ export declare function tagKind(node: BaseElement, tagModule: (tag: string) => string, projectNamespace: string | undefined, baseComponents: Readonly<Record<string, LightningComponent>>): TagKind;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Salesforce's own namespaces of components that templates use, which aren't installed packages':
3
+ * `lightning`'s are described by LIGHTNING_COMPONENTS, and the others aren't described at all. It isn't
4
+ * every one there is, so a tag of one that's missing is reported as a component the project doesn't have.
5
+ */
6
+ export const PLATFORM_NAMESPACES = new Set(["lightning", "lightningsnapin", "force", "ui"]);
7
+ /** Which helper types an element in a namespace other than HTML, whose tags can have the same names. */
8
+ const ELEMENT_HELPERS = {
9
+ "http://www.w3.org/2000/svg": "__lwcSvgElement",
10
+ "http://www.w3.org/1998/Math/MathML": "__lwcMathElement",
11
+ };
12
+ /** The DOM type of a plain element, by its namespace, as `<a>` in an `<svg>` isn't an HTML `<a>`. */
13
+ export function elementType(node) {
14
+ return `${ELEMENT_HELPERS[node.namespace] ?? "__lwcElement"}<${JSON.stringify(node.name)}>`;
15
+ }
16
+ /**
17
+ * What a tag is. `tagModule` is the compiler's, as a tag names what the compiled template imports.
18
+ * Installed packages' components are of any namespace but `c`, the project's own and Salesforce's.
19
+ */
20
+ export function tagKind(node, tagModule, projectNamespace, baseComponents) {
21
+ if (node.type === "Element") {
22
+ return { kind: "element" };
23
+ }
24
+ if (node.type !== "Component") {
25
+ return { kind: "other" };
26
+ }
27
+ const module = tagModule(node.name);
28
+ const [namespace, name] = module.split("/");
29
+ if (namespace === "c") {
30
+ return { kind: "child", name, module };
31
+ }
32
+ if (namespace !== projectNamespace?.toLowerCase() && !PLATFORM_NAMESPACES.has(namespace)) {
33
+ return { kind: "package", module };
34
+ }
35
+ return baseComponents[node.name] === undefined ? { kind: "other" } : { kind: "base", tag: node.name };
36
+ }
@@ -0,0 +1,53 @@
1
+ import { type LightningComponent } from "@lwc-bolts/lightning-base-components";
2
+ import type { ConversionContext } from "../../context.ts";
3
+ import type { ConversionOutput, LightningComponentBundleComponent, MapPair } from "../../types.ts";
4
+ /** An error the LWC template compiler reports in a template, at a UTF-16 offset and length. */
5
+ export interface TemplateDiagnostic {
6
+ message: string;
7
+ start: number;
8
+ length: number;
9
+ }
10
+ /** A template converted to TypeScript, with where its pieces came from and the compiler's errors. */
11
+ export interface ConvertedTemplate {
12
+ text: string;
13
+ mapData: MapPair[];
14
+ diagnostics: TemplateDiagnostic[];
15
+ }
16
+ /**
17
+ * Converts one of a bundle's templates (`convert` with a `LightningComponentBundle`'s `.html`). What the
18
+ * template needs to know about its bundle is on the component: how it imports the bundle's component
19
+ * (`componentModule`), the bundle's name, its API version, and whether it has other templates. Without
20
+ * them, the template is taken to be in the bundle's folder, named for it. Other components' slots come
21
+ * from their own templates' declarations. The compiler's errors are `TemplateError` diagnostics.
22
+ */
23
+ export declare function convertBundleTemplate(component: LightningComponentBundleComponent, context: ConversionContext): Promise<ConversionOutput>;
24
+ /**
25
+ * The name of a template's default export, which editors show and offer in auto-imports: `<file>Template`,
26
+ * so `default.html`'s is `defaultTemplate` rather than a reserved word. A file name can have characters
27
+ * a name can't.
28
+ */
29
+ export declare function templateExportName(fileName: string): string;
30
+ /** What a template converter can find out about the rest of the project. */
31
+ export interface TemplateContext {
32
+ /** The template's file, which names its default export (see templateExportName): `default.html` when not given. */
33
+ fileName?: string;
34
+ /**
35
+ * The module other components import the template's component as, such as `c/orders`, under which it
36
+ * declares its slots for their templates. Without it, the template declares none.
37
+ */
38
+ module?: string;
39
+ /** Whether the bundle has other templates, any of which may be the one rendered. */
40
+ alternateTemplates?: boolean;
41
+ /** The API version of the template's component, which it's parsed at: the compiler's latest when not given. */
42
+ apiVersion?: number;
43
+ /** The project's own namespace, whose tags are left unchecked: components in it use `c`. */
44
+ projectNamespace?: string;
45
+ /** The `lightning-*` base components, by tag, that templates are checked against: LIGHTNING_COMPONENTS by default. */
46
+ baseComponents?: Readonly<Record<string, LightningComponent>>;
47
+ }
48
+ /**
49
+ * Converts a component's template to TypeScript that checks its bindings against the component, imported
50
+ * as `componentModule` (such as `./orders.js`). `context` tells it about its own bundle, and names
51
+ * the component's module, for the slots it declares.
52
+ */
53
+ export declare function convertTemplate(content: string, componentModule: string, context?: TemplateContext): Promise<ConvertedTemplate>;