@xaendar/compiler 0.3.8

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.
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@xaendar/compiler",
3
+ "version": "0.3.8",
4
+ "description": "A library containing compiler engine",
5
+ "sideEffects": false,
6
+ "type": "module",
7
+ "main": "./xaendar-compiler.es.js",
8
+ "module": "./xaendar-compiler.es.js",
9
+ "types": "./xaendar-compiler.es.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "import": {
13
+ "types": "./xaendar-compiler.es.d.ts",
14
+ "default": "./xaendar-compiler.es.js"
15
+ }
16
+ }
17
+ },
18
+ "dependencies": {
19
+ "@xaendar/common": "0.3.8",
20
+ "@xaendar/types": "0.3.8",
21
+ "typescript": "0.2.1"
22
+ }
23
+ }
@@ -0,0 +1,225 @@
1
+ export declare function compile(input: string): string;
2
+
3
+ /**
4
+ * @fileoverview CompilerHost interface — the contract between the compiler and the environment it runs in.
5
+ *
6
+ * The compiler never accesses the filesystem, network, or any external resource directly.
7
+ * All I/O is delegated to the CompilerHost, allowing the same compiler core to run in
8
+ * different environments by swapping the host implementation:
9
+ *
10
+ * - **Node.js** (`build-tools`, `cli`) — reads and writes real files on disk
11
+ * - **Editor** (`language-tools`) — reads live document content from the editor buffer,
12
+ * which may differ from the file on disk when unsaved changes are present
13
+ * - **Tests** — uses in-memory virtual files without touching the real filesystem
14
+ *
15
+ * @example
16
+ * // Minimal Node.js implementation
17
+ * import * as fs from 'node:fs';
18
+ * import * as path from 'node:path';
19
+ *
20
+ * class NodeCompilerHost implements CompilerHost {
21
+ * readFile(filePath: string) {
22
+ * try { return fs.readFileSync(filePath, 'utf8'); }
23
+ * catch { return undefined; }
24
+ * }
25
+ * fileExists(filePath: string) { return fs.existsSync(filePath); }
26
+ * resolvePath(from: string, to: string) { return path.resolve(path.dirname(from), to); }
27
+ * getDirectoryEntries(dirPath: string) { return fs.readdirSync(dirPath); }
28
+ * isDirectory(filePath: string) { return fs.statSync(filePath).isDirectory(); }
29
+ * getRealPath(filePath: string) { return fs.realpathSync(filePath); }
30
+ * getCurrentDirectory() { return process.cwd(); }
31
+ * }
32
+ */
33
+ /**
34
+ * The contract between the compiler and the filesystem environment it runs in.
35
+ *
36
+ * The compiler uses this host for all I/O operations. It never accesses the filesystem,
37
+ * the network, or any other external resource directly. This makes it possible to use
38
+ * the same compiler in different environments:
39
+ *
40
+ * - **Node.js** (`build-tools`, `cli`) — reads and writes real files on disk
41
+ * - **Editor** (`language-tools`) — reads live document content from open editor buffers,
42
+ * which may differ from the file on disk when unsaved changes are present
43
+ * - **Tests** — uses in-memory virtual files without touching the real filesystem
44
+ *
45
+ * Implementations should be **synchronous** wherever possible to ensure compatibility
46
+ * with the TypeScript compiler host API, which does not support async operations
47
+ * in its core methods.
48
+ *
49
+ * @example
50
+ * // Typical usage — the compiler receives the host as a dependency
51
+ * const host: CompilerHost = new NodeCompilerHost();
52
+ * const compiler = new Compiler(host);
53
+ * const result = compiler.compile('/src/app.component.html');
54
+ */
55
+ export declare interface CompilerHost {
56
+ /**
57
+ * Checks whether a file exists and can be read.
58
+ *
59
+ * Used by the compiler to:
60
+ * - Verify that a template associated with a component exists before attempting
61
+ * to read it (e.g. `app.component.html` paired with `app.component.ts`)
62
+ * - Resolve optional imports
63
+ * - Decide which resolution strategy to apply
64
+ *
65
+ * In environments with a virtual file system (such as `language-tools`),
66
+ * this method must return `true` for virtual files that do not exist on disk,
67
+ * such as Template Check Blocks (TCBs) generated in memory.
68
+ *
69
+ * @param filePath - Absolute path of the file whose existence to check.
70
+ * @returns `true` if the file exists and is accessible, `false` otherwise.
71
+ *
72
+ * @example
73
+ * const templatePath = host.resolvePath(componentPath, './app.component.html');
74
+ * if (host.fileExists(templatePath)) {
75
+ * const template = host.readFile(templatePath);
76
+ * }
77
+ */
78
+ fileExists(filePath: string): boolean;
79
+ /**
80
+ * Returns the current working directory of the process.
81
+ *
82
+ * Used by the compiler as the base for resolving relative paths that have
83
+ * no associated source file — for example, paths specified in the project
84
+ * configuration or passed as CLI arguments.
85
+ *
86
+ * In an editor environment, this should return the root of the open workspace,
87
+ * not the directory of the editor process itself.
88
+ *
89
+ * @returns The absolute path of the current working directory.
90
+ *
91
+ * @example
92
+ * // Resolves a relative path from the project configuration
93
+ * const configRelativePath = './src';
94
+ * const absolutePath = host.resolvePath(
95
+ * host.getCurrentDirectory() + '/placeholder',
96
+ * configRelativePath
97
+ * );
98
+ */
99
+ getCurrentDirectory(): string;
100
+ /**
101
+ * Returns the list of files and subdirectories inside a directory.
102
+ *
103
+ * Used by the compiler during the discovery phase — when it needs to find
104
+ * all components in the project without them being explicitly listed, or
105
+ * when it needs to resolve glob patterns in the project configuration.
106
+ *
107
+ * Returns only entry names, not absolute paths. The caller is responsible
108
+ * for combining the results with `dirPath` via `resolvePath` to obtain
109
+ * absolute paths.
110
+ *
111
+ * @param dirPath - Absolute path of the directory to read.
112
+ * @returns An array of file and directory names inside `dirPath`.
113
+ * Returns an empty array if the directory is empty or does not exist.
114
+ *
115
+ * @example
116
+ * const entries = host.getDirectoryEntries('/src/components');
117
+ * // → ['button', 'input', 'modal']
118
+ *
119
+ * const componentFiles = entries
120
+ * .map(entry => host.resolvePath('/src/components', entry))
121
+ * .filter(p => host.fileExists(p) && p.endsWith('.ts'));
122
+ */
123
+ getDirectoryEntries(dirPath: string): string[];
124
+ /**
125
+ * Resolves symlinks and returns the real physical path of a file.
126
+ *
127
+ * Used by the compiler to normalise paths before using them as keys in
128
+ * internal caches. Two different paths pointing to the same physical file
129
+ * (one through a symlink) must produce the same compiled output and must
130
+ * not generate duplicates in the module graph.
131
+ *
132
+ * Particularly important in monorepos using workspace links (npm/yarn/pnpm),
133
+ * where local packages are often symlinked inside `node_modules`.
134
+ *
135
+ * @param filePath - Absolute path, potentially containing symlinks.
136
+ * @returns The real absolute path with all symlinks resolved.
137
+ * If the path does not exist or cannot be resolved, returns `filePath` unchanged.
138
+ *
139
+ * @example
140
+ * // In a pnpm monorepo:
141
+ * // /app/node_modules/@my-lib/core → /packages/core/src
142
+ * host.getRealPath('/app/node_modules/@my-lib/core/index.ts');
143
+ * // → '/packages/core/src/index.ts'
144
+ */
145
+ getRealPath(filePath: string): string;
146
+ /**
147
+ * Checks whether the given path refers to a directory.
148
+ *
149
+ * Used together with `getDirectoryEntries` to distinguish files from
150
+ * subdirectories when recursing through the filesystem. Also needed to
151
+ * validate output paths before writing files.
152
+ *
153
+ * @param filePath - Absolute path to check.
154
+ * @returns `true` if the path exists and is a directory, `false` in all
155
+ * other cases (regular file, symlink to a file, non-existent path).
156
+ *
157
+ * @example
158
+ * const entries = host.getDirectoryEntries('/src');
159
+ * for (const entry of entries) {
160
+ * const fullPath = host.resolvePath('/src', entry);
161
+ * if (host.isDirectory(fullPath)) {
162
+ * // recurse into subdirectory
163
+ * }
164
+ * }
165
+ */
166
+ isDirectory(filePath: string): boolean;
167
+ /**
168
+ * Reads the content of a file as a UTF-8 string.
169
+ *
170
+ * The compiler calls this method to read:
171
+ * - Template files (`.html` files containing the custom DSL)
172
+ * - TypeScript source files (`.ts`) when needed for analysis
173
+ * - Project configuration files
174
+ *
175
+ * Returns `undefined` instead of throwing when the file does not exist or
176
+ * cannot be read. The compiler handles the `undefined` case by emitting
177
+ * an appropriate diagnostic.
178
+ *
179
+ * In an editor environment, this method must return the content currently
180
+ * held in the **editor buffer**, not the content on disk. This ensures the
181
+ * compiler always operates on the most up-to-date code, even when unsaved
182
+ * changes are present.
183
+ *
184
+ * @param filePath - Absolute path of the file to read.
185
+ * @returns The file content as a string, or `undefined` if the file does
186
+ * not exist or cannot be read.
187
+ *
188
+ * @example
189
+ * const content = host.readFile('/src/app.component.html');
190
+ * if (content === undefined) {
191
+ * // file not found — the compiler will emit a diagnostic
192
+ * }
193
+ */
194
+ readFile(filePath: string): string | undefined;
195
+ /**
196
+ * Resolves a path relative to the source file that contains the reference.
197
+ *
198
+ * The compiler calls this method whenever it needs to turn a relative path
199
+ * (such as `./app.component.html` inside a `@Component` decorator) into an
200
+ * absolute path suitable for passing to `readFile` and `fileExists`.
201
+ *
202
+ * The default implementation should behave like
203
+ * `path.resolve(path.dirname(from), to)` in Node.js, but can be overridden
204
+ * to handle path aliases, `tsconfig.json` path mappings, or custom project
205
+ * conventions.
206
+ *
207
+ * @param from - Absolute path of the source file that contains the reference.
208
+ * Typically the `.ts` file of the component.
209
+ * @param to - Relative or absolute path to resolve. May be relative
210
+ * (`./template.html`), aliased (`@components/button.html`), or already absolute.
211
+ * @returns The resolved absolute path of the target file.
212
+ *
213
+ * @example
214
+ * // from: '/src/features/user/user.component.ts'
215
+ * // to: './user.component.html'
216
+ * // → '/src/features/user/user.component.html'
217
+ * const templatePath = host.resolvePath(
218
+ * '/src/features/user/user.component.ts',
219
+ * './user.component.html'
220
+ * );
221
+ */
222
+ resolvePath(from: string, to: string): string;
223
+ }
224
+
225
+ export { }