@astryxdesign/cli 0.7.3-canary.641d08e → 0.7.3-canary.84c7b4f
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/README.md +63 -63
- package/api/component/component-replacement.test.mjs +1 -0
- package/api/component/component.mjs +1 -1
- package/api/hook/hook.mjs +1 -1
- package/api/integration/validate-integration-fixes.test.mjs +6 -5
- package/api/integration/validate-integration-gaps.test.mjs +9 -1
- package/api/integration/validate-integration.d.mts +4 -0
- package/api/integration/validate-integration.mjs +15 -0
- package/api/integration/validate-integration.type.d.mts +33 -4
- package/api/integration/validate-integration.type.mjs +24 -5
- package/api/integration/validateIntegration.doc.mjs +1 -1
- package/api/search/_adapter.d.mts +16 -0
- package/api/search/_adapter.mjs +39 -0
- package/api/search/_adapter.test.mjs +173 -0
- package/api/search/search.mjs +6 -2
- package/assets/docs/tree/checks.doc.mjs +1 -1
- package/assets/docs/use-integrations.doc.mjs +120 -0
- package/clients/cli/command-result-coverage.test.mjs +6 -5
- package/clients/cli/commands/build-theme.color-scheme.test.mjs +1 -1
- package/clients/cli/commands/{component/index.mjs → component.mjs} +37 -37
- package/clients/cli/commands/component.test.mjs +6 -6
- package/clients/cli/commands/doctor-integration.test.mjs +49 -1
- package/clients/cli/commands/doctor.mjs +10 -0
- package/clients/cli/commands/{hook/index.mjs → hook.mjs} +22 -22
- package/clients/cli/commands/import-hint-correctness.test.mjs +1 -1
- package/clients/cli/commands/manifest.doc.mjs +1 -1
- package/clients/cli/commands/manifest.mjs +67 -0
- package/clients/cli/commands/postinstall.doc.mjs +23 -0
- package/clients/cli/commands/postinstall.mjs +31 -0
- package/clients/cli/commands/{build-theme.mjs → theme.mjs} +1 -1
- package/clients/cli/index.mjs +10 -85
- package/clients/cli/lib/component-format.mjs +1 -1
- package/clients/cli/lib/define-command.mjs +12 -7
- package/clients/cli/lib/exit-codes.test.mjs +5 -0
- package/clients/cli/lib/manifest.test.mjs +12 -0
- package/clients/cli/lib/welcome-box.mjs +50 -0
- package/foundation/discovery/runner-commands.mjs +1 -0
- package/foundation/doc-compiler/doc-loads.test.mjs +21 -9
- package/foundation/fs/read-record.d.mts +18 -0
- package/foundation/fs/read-record.mjs +131 -0
- package/foundation/fs/read-record.test.mjs +77 -0
- package/foundation/integrations/exports-subpaths.d.mts +57 -0
- package/foundation/integrations/exports-subpaths.mjs +113 -0
- package/foundation/integrations/public-imports.d.mts +81 -0
- package/foundation/integrations/public-imports.mjs +976 -0
- package/foundation/integrations/public-imports.test.mjs +868 -0
- package/foundation/response/response-types.doc.mjs +1 -1
- package/package.json +9 -9
|
@@ -0,0 +1,976 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Source-level checks that what an integration package tells apps to
|
|
5
|
+
* import is importable, read without a build.
|
|
6
|
+
*
|
|
7
|
+
* @input A package directory and its loaded integration record.
|
|
8
|
+
* @output Notes, listed on validate's result and never counted (they fail
|
|
9
|
+
* `--strict` from the next minor), for: a component's import (the doc's `import`, or the one
|
|
10
|
+
* the CLI derives) or a template's public import whose subpath
|
|
11
|
+
* package.json `exports` does not declare (or that names another package);
|
|
12
|
+
* a component example that imports an undeclared subpath; a template
|
|
13
|
+
* source that imports a relative path outside the templates root, or an
|
|
14
|
+
* undeclared subpath of its own package or of Core. Imports it cannot
|
|
15
|
+
* judge (Core not installed) are listed in a report.
|
|
16
|
+
* @position foundation/integrations, beside the other on-disk contribution
|
|
17
|
+
* validators; run by `astryx doctor integration validate` (and so by
|
|
18
|
+
* `integration check`) through api/integration/validate-integration.mjs.
|
|
19
|
+
*
|
|
20
|
+
* An `exports` target may point at build output that does not exist yet, so
|
|
21
|
+
* only the declared subpath is judged here, never the target file or the
|
|
22
|
+
* names it exports; `integration verify` resolves those from the packed
|
|
23
|
+
* package.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import * as fs from 'node:fs';
|
|
27
|
+
import * as path from 'node:path';
|
|
28
|
+
import {parse} from '@babel/parser';
|
|
29
|
+
import {findProjectCoreDir} from '../fs/paths.mjs';
|
|
30
|
+
import {normalizeProviderId} from '../identity/provider-identity.mjs';
|
|
31
|
+
import {
|
|
32
|
+
discoverIntegrationComponents,
|
|
33
|
+
resolveIntegrationImportPath,
|
|
34
|
+
} from '../discovery/component-discovery.mjs';
|
|
35
|
+
import {loadComponentDoc} from '../discovery/component-loader.mjs';
|
|
36
|
+
import {discoverIntegrationTemplatesForOne} from '../discovery/template-adapter.mjs';
|
|
37
|
+
import {
|
|
38
|
+
allTargets,
|
|
39
|
+
importable,
|
|
40
|
+
isRecord,
|
|
41
|
+
subpathDeclaration,
|
|
42
|
+
} from './exports-subpaths.mjs';
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @typedef {import('./issue').AstryxIntegrationIssue} Issue
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
const CORE = '@astryxdesign/core';
|
|
49
|
+
const MODULE_EXTENSIONS = [
|
|
50
|
+
'.tsx',
|
|
51
|
+
'.ts',
|
|
52
|
+
'.mts',
|
|
53
|
+
'.jsx',
|
|
54
|
+
'.mjs',
|
|
55
|
+
'.js',
|
|
56
|
+
'.cjs',
|
|
57
|
+
'.cts',
|
|
58
|
+
];
|
|
59
|
+
|
|
60
|
+
/** @param {string} file */
|
|
61
|
+
function readJson(file) {
|
|
62
|
+
try {
|
|
63
|
+
return JSON.parse(fs.readFileSync(file, 'utf-8'));
|
|
64
|
+
} catch {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** @param {string} file */
|
|
70
|
+
function isFile(file) {
|
|
71
|
+
try {
|
|
72
|
+
return fs.statSync(file).isFile();
|
|
73
|
+
} catch {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** @param {string} from @param {string} to */
|
|
79
|
+
function rel(from, to) {
|
|
80
|
+
return path.relative(from, to).split(path.sep).join('/');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// ── Import specifiers ───────────────────────────────────────────────
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Split a bare specifier into its package name and `./`-prefixed subpath.
|
|
87
|
+
* @param {string} specifier
|
|
88
|
+
* @returns {{pkg: string, subpath: string} | null}
|
|
89
|
+
*/
|
|
90
|
+
export function splitSpecifier(specifier) {
|
|
91
|
+
const m = /^((?:@[^/\s]+\/)?[^/\s]+)(\/.*)?$/.exec(specifier);
|
|
92
|
+
if (!m || specifier.startsWith('.') || specifier.startsWith('/')) return null;
|
|
93
|
+
return {pkg: m[1], subpath: m[2] ? `.${m[2]}` : '.'};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @typedef {object} ImportBinding
|
|
98
|
+
* @property {string} imported the exported name (`default` for a default import)
|
|
99
|
+
* @property {boolean} typeOnly
|
|
100
|
+
*/
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @typedef {object} ImportStatement
|
|
104
|
+
* @property {string} source
|
|
105
|
+
* @property {ImportBinding[]} names
|
|
106
|
+
* @property {boolean} namespace
|
|
107
|
+
* @property {boolean} [typeOnly] the whole statement is `import type`
|
|
108
|
+
*/
|
|
109
|
+
|
|
110
|
+
const PARSE_PLUGINS = /** @type {any} */ ([
|
|
111
|
+
'jsx',
|
|
112
|
+
'typescript',
|
|
113
|
+
'decorators-legacy',
|
|
114
|
+
'importAttributes',
|
|
115
|
+
'exportDefaultFrom',
|
|
116
|
+
]);
|
|
117
|
+
|
|
118
|
+
/** @param {string} text */
|
|
119
|
+
function parseModuleText(text) {
|
|
120
|
+
return parse(text, {
|
|
121
|
+
sourceType: 'module',
|
|
122
|
+
allowImportExportEverywhere: true,
|
|
123
|
+
allowReturnOutsideFunction: true,
|
|
124
|
+
allowAwaitOutsideFunction: true,
|
|
125
|
+
errorRecovery: true,
|
|
126
|
+
plugins: PARSE_PLUGINS,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const IMPORT_RE =
|
|
131
|
+
/(?:^|[;\n])\s*import\s+(type\s+)?([^'";]*?)\s*from\s*['"]([^'"]+)['"]/g;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Read the import statements of a module or a snippet: named, default,
|
|
135
|
+
* aliased, namespace and type-only. A snippet that does not parse (an example
|
|
136
|
+
* with elisions, say) falls back to reading `import … from '…'` lines.
|
|
137
|
+
* @param {string} text
|
|
138
|
+
* @returns {ImportStatement[]}
|
|
139
|
+
*/
|
|
140
|
+
export function readImports(text) {
|
|
141
|
+
/** @type {ImportStatement[]} */
|
|
142
|
+
const out = [];
|
|
143
|
+
let ast;
|
|
144
|
+
try {
|
|
145
|
+
ast = parseModuleText(text);
|
|
146
|
+
} catch {
|
|
147
|
+
// Not a module: read the import lines below.
|
|
148
|
+
}
|
|
149
|
+
if (ast) {
|
|
150
|
+
for (const node of ast.program.body) {
|
|
151
|
+
if (node.type !== 'ImportDeclaration') continue;
|
|
152
|
+
const typeOnly = node.importKind === 'type';
|
|
153
|
+
/** @type {ImportStatement} */
|
|
154
|
+
const statement = {
|
|
155
|
+
source: String(node.source.value),
|
|
156
|
+
names: [],
|
|
157
|
+
namespace: false,
|
|
158
|
+
typeOnly,
|
|
159
|
+
};
|
|
160
|
+
for (const s of node.specifiers ?? []) {
|
|
161
|
+
if (s.type === 'ImportDefaultSpecifier') {
|
|
162
|
+
statement.names.push({imported: 'default', typeOnly});
|
|
163
|
+
} else if (s.type === 'ImportNamespaceSpecifier') {
|
|
164
|
+
statement.namespace = true;
|
|
165
|
+
} else if (s.type === 'ImportSpecifier') {
|
|
166
|
+
const imported =
|
|
167
|
+
s.imported.type === 'Identifier'
|
|
168
|
+
? s.imported.name
|
|
169
|
+
: String(s.imported.value);
|
|
170
|
+
statement.names.push({
|
|
171
|
+
imported,
|
|
172
|
+
typeOnly: typeOnly || s.importKind === 'type',
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
out.push(statement);
|
|
177
|
+
}
|
|
178
|
+
return out;
|
|
179
|
+
}
|
|
180
|
+
for (const m of text.matchAll(IMPORT_RE)) {
|
|
181
|
+
const typeOnly = Boolean(m[1]);
|
|
182
|
+
const clause = m[2].trim();
|
|
183
|
+
/** @type {ImportStatement} */
|
|
184
|
+
const statement = {
|
|
185
|
+
source: m[3],
|
|
186
|
+
names: [],
|
|
187
|
+
namespace: /\*\s*as\s/.test(clause),
|
|
188
|
+
typeOnly,
|
|
189
|
+
};
|
|
190
|
+
const braces = /\{([^}]*)\}/.exec(clause);
|
|
191
|
+
const head = clause
|
|
192
|
+
.replace(/\{[^}]*\}/, '')
|
|
193
|
+
.replace(/\*\s*as\s+[\w$]+/, '');
|
|
194
|
+
const defaultName = /^([\w$]+)/.exec(head.trim());
|
|
195
|
+
if (defaultName) statement.names.push({imported: 'default', typeOnly});
|
|
196
|
+
if (braces) {
|
|
197
|
+
for (const part of braces[1].split(',')) {
|
|
198
|
+
const item = part.trim();
|
|
199
|
+
if (!item) continue;
|
|
200
|
+
const t = /^type\s+/.test(item);
|
|
201
|
+
const name = item
|
|
202
|
+
.replace(/^type\s+/, '')
|
|
203
|
+
.split(/\s+as\s+/)[0]
|
|
204
|
+
.trim();
|
|
205
|
+
if (/^[\w$]+$/.test(name))
|
|
206
|
+
statement.names.push({imported: name, typeOnly: typeOnly || t});
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
out.push(statement);
|
|
210
|
+
}
|
|
211
|
+
return out;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// ── package.json exports ────────────────────────────────────────────
|
|
215
|
+
|
|
216
|
+
/** @typedef {import('./exports-subpaths.mjs').SubpathDeclaration} SubpathDeclaration */
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* How a package declares one subpath. A package with no `exports` keeps its
|
|
220
|
+
* legacy deep paths: a subpath that names a file on disk (with or without a
|
|
221
|
+
* module extension, or a folder index) counts as declared.
|
|
222
|
+
* @param {unknown} exportsField
|
|
223
|
+
* @param {string} packageDir
|
|
224
|
+
* @param {string} subpath
|
|
225
|
+
* @returns {SubpathDeclaration}
|
|
226
|
+
*/
|
|
227
|
+
function declarationIn(exportsField, packageDir, subpath) {
|
|
228
|
+
const declaration = subpathDeclaration(exportsField, subpath);
|
|
229
|
+
if (declaration.state !== 'no_exports') return declaration;
|
|
230
|
+
if (subpath === '.') return {state: 'declared', value: null, wildcard: ''};
|
|
231
|
+
const root = path.resolve(packageDir);
|
|
232
|
+
const base = path.resolve(root, subpath.slice(2));
|
|
233
|
+
if (!base.startsWith(root + path.sep)) return declaration;
|
|
234
|
+
const file = [
|
|
235
|
+
base,
|
|
236
|
+
...MODULE_EXTENSIONS.map(ext => `${base}${ext}`),
|
|
237
|
+
...MODULE_EXTENSIONS.map(ext => path.join(base, `index${ext}`)),
|
|
238
|
+
].find(isFile);
|
|
239
|
+
return file
|
|
240
|
+
? {state: 'declared', value: `./${rel(root, file)}`, wildcard: ''}
|
|
241
|
+
: declaration;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* A declared subpath of the package whose target is `file`, if any: an exact
|
|
246
|
+
* key, or a wildcard key whose target pattern matches the file.
|
|
247
|
+
* @param {unknown} exportsField
|
|
248
|
+
* @param {string} packageDir
|
|
249
|
+
* @param {string} file
|
|
250
|
+
* @returns {string | null}
|
|
251
|
+
*/
|
|
252
|
+
function subpathFor(exportsField, packageDir, file) {
|
|
253
|
+
if (!isRecord(exportsField)) return null;
|
|
254
|
+
const wanted = `./${rel(packageDir, file)}`;
|
|
255
|
+
for (const [key, value] of Object.entries(exportsField)) {
|
|
256
|
+
if (!key.startsWith('.') || !importable(value)) continue;
|
|
257
|
+
for (const target of allTargets(value)) {
|
|
258
|
+
if (!key.includes('*')) {
|
|
259
|
+
if (target === wanted) return key;
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
const star = target.indexOf('*');
|
|
263
|
+
if (star === -1) continue;
|
|
264
|
+
const prefix = target.slice(0, star);
|
|
265
|
+
const suffix = target.slice(star + 1);
|
|
266
|
+
if (
|
|
267
|
+
wanted.startsWith(prefix) &&
|
|
268
|
+
wanted.endsWith(suffix) &&
|
|
269
|
+
wanted.length >= prefix.length + suffix.length
|
|
270
|
+
) {
|
|
271
|
+
return key.replace(
|
|
272
|
+
'*',
|
|
273
|
+
wanted.slice(prefix.length, wanted.length - suffix.length),
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* The condition names an export value uses, at any depth, in order.
|
|
283
|
+
* @param {unknown} value
|
|
284
|
+
* @param {string[]} [out]
|
|
285
|
+
* @returns {string[]}
|
|
286
|
+
*/
|
|
287
|
+
function conditionNames(value, out = []) {
|
|
288
|
+
if (Array.isArray(value)) for (const v of value) conditionNames(v, out);
|
|
289
|
+
else if (isRecord(value)) {
|
|
290
|
+
for (const [key, v] of Object.entries(value)) {
|
|
291
|
+
if (!out.includes(key)) out.push(key);
|
|
292
|
+
conditionNames(v, out);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return out;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Why a declared state is not importable, in words.
|
|
300
|
+
* @param {SubpathDeclaration} declaration
|
|
301
|
+
* @param {string} subpath
|
|
302
|
+
*/
|
|
303
|
+
function whyUndeclared(declaration, subpath) {
|
|
304
|
+
const {state} = declaration;
|
|
305
|
+
switch (state) {
|
|
306
|
+
case 'no_exports':
|
|
307
|
+
return `package.json has no "exports" and no file exists at "${subpath}"`;
|
|
308
|
+
case 'root_only':
|
|
309
|
+
return `package.json "exports" names only the package root, which blocks every subpath`;
|
|
310
|
+
case 'blocked':
|
|
311
|
+
return `package.json "exports" maps "${subpath}" to null`;
|
|
312
|
+
case 'not_importable': {
|
|
313
|
+
const names = conditionNames(declaration.value);
|
|
314
|
+
const listed = names.map(name => `"${name}"`).join(', ');
|
|
315
|
+
return names.length === 0
|
|
316
|
+
? `package.json "exports" gives "${subpath}" no target an ES module import can load`
|
|
317
|
+
: `package.json "exports" gives "${subpath}" only the ${listed} condition${names.length === 1 ? '' : 's'}, none of which an ES module import loads`;
|
|
318
|
+
}
|
|
319
|
+
default:
|
|
320
|
+
return `package.json "exports" does not declare "${subpath}"`;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* The paste-ready package.json change that declares `subpath` → `target`.
|
|
326
|
+
* @param {unknown} exportsField
|
|
327
|
+
* @param {SubpathDeclaration['state']} state
|
|
328
|
+
* @param {string} subpath
|
|
329
|
+
* @param {string} target `./`-prefixed package-relative file
|
|
330
|
+
*/
|
|
331
|
+
function exportsFix(exportsField, state, subpath, target) {
|
|
332
|
+
const entry = `${JSON.stringify(subpath)}: ${JSON.stringify(target)}`;
|
|
333
|
+
if (state === 'no_exports') {
|
|
334
|
+
return `point it at a file the package ships, or add "exports": {${entry}} to package.json (an "exports" map makes every path it does not list private, so list "." and the package's other public paths too).`;
|
|
335
|
+
}
|
|
336
|
+
if (state === 'root_only') {
|
|
337
|
+
return `make "exports" a subpath map: {".": ${JSON.stringify(exportsField)}, ${entry}}.`;
|
|
338
|
+
}
|
|
339
|
+
if (state === 'not_importable') {
|
|
340
|
+
return `add an ES module condition to "${subpath}" in package.json "exports", e.g. "default": ${JSON.stringify(target)} (or the built file).`;
|
|
341
|
+
}
|
|
342
|
+
if (state === 'blocked') {
|
|
343
|
+
return `replace the null for "${subpath}" in package.json "exports" with ${JSON.stringify(target)}, or stop advertising it.`;
|
|
344
|
+
}
|
|
345
|
+
return `add ${entry} to package.json "exports" (or the built file it compiles to).`;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Whether `name` is a bare npm package name (an optional `@scope/` and a
|
|
350
|
+
* name), the only form an import can name the package by.
|
|
351
|
+
* @param {string} name
|
|
352
|
+
*/
|
|
353
|
+
function isPackageName(name) {
|
|
354
|
+
try {
|
|
355
|
+
normalizeProviderId(name);
|
|
356
|
+
return true;
|
|
357
|
+
} catch {
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// ── Listed findings ─────────────────────────────────────────────────
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* A public-import finding as a note on validate's result: listed with its
|
|
366
|
+
* fix inside the message, never counted or failed on. It becomes a failing
|
|
367
|
+
* warning in the next minor release (a new failure code joins only in a
|
|
368
|
+
* minor).
|
|
369
|
+
* @param {string} code
|
|
370
|
+
* @param {string} message
|
|
371
|
+
* @returns {PublicImportNote}
|
|
372
|
+
*/
|
|
373
|
+
function listed(code, message) {
|
|
374
|
+
return {code, message};
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// ── Checks ──────────────────────────────────────────────────────────
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* @typedef {object} Context
|
|
381
|
+
* @property {string} packageDir
|
|
382
|
+
* @property {string | null} name the package name; null when package.json has
|
|
383
|
+
* none, and then no import of the package's own name is judged
|
|
384
|
+
* @property {unknown} exportsField
|
|
385
|
+
* @property {string | null} coreDir
|
|
386
|
+
* @property {any} corePkg
|
|
387
|
+
* @property {PublicImportUnchecked[]} unchecked what could not be judged
|
|
388
|
+
* @property {string[]} files the package.json `files` entries, normalized
|
|
389
|
+
* @property {string[]} roots the integration's contribution roots, absolute
|
|
390
|
+
*/
|
|
391
|
+
|
|
392
|
+
const BUILT_EXTENSION_RE = /\.[cm]?jsx?$/u;
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Whether an undeclared deep path, in a package with no `exports`, points
|
|
396
|
+
* into build output that is not built yet: its top folder is missing, or is
|
|
397
|
+
* a `files` folder that holds no contribution root. Such an import cannot be
|
|
398
|
+
* judged from source.
|
|
399
|
+
* @param {Context} ctx
|
|
400
|
+
* @param {string} subpath
|
|
401
|
+
* @param {SubpathDeclaration} declaration
|
|
402
|
+
*/
|
|
403
|
+
function unbuiltOutput(ctx, subpath, declaration) {
|
|
404
|
+
if (declaration.state !== 'no_exports' || subpath === '.') return false;
|
|
405
|
+
const top = subpath.slice(2).split('/')[0];
|
|
406
|
+
if (!top) return false;
|
|
407
|
+
const topDir = path.resolve(ctx.packageDir, top);
|
|
408
|
+
if (!fs.existsSync(topDir)) return true;
|
|
409
|
+
return (
|
|
410
|
+
ctx.files.includes(top) &&
|
|
411
|
+
!ctx.roots.some(
|
|
412
|
+
root => root === topDir || root.startsWith(topDir + path.sep),
|
|
413
|
+
)
|
|
414
|
+
);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* The reason an import into unbuilt output is listed as not checked.
|
|
419
|
+
* @param {string} subpath
|
|
420
|
+
*/
|
|
421
|
+
function unbuiltReason(subpath) {
|
|
422
|
+
return `package.json has no "exports", and "${subpath}" is build output that is not built yet, so it was not checked`;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* The target a fix maps `subpath` to: the component's source for a source
|
|
427
|
+
* path, never a TypeScript source for a built `.js` path.
|
|
428
|
+
* @param {string} subpath
|
|
429
|
+
* @param {string | null} sourceTarget
|
|
430
|
+
*/
|
|
431
|
+
function fixTarget(subpath, sourceTarget) {
|
|
432
|
+
if (
|
|
433
|
+
sourceTarget &&
|
|
434
|
+
!(
|
|
435
|
+
BUILT_EXTENSION_RE.test(subpath) && !BUILT_EXTENSION_RE.test(sourceTarget)
|
|
436
|
+
)
|
|
437
|
+
) {
|
|
438
|
+
return sourceTarget;
|
|
439
|
+
}
|
|
440
|
+
return BUILT_EXTENSION_RE.test(subpath)
|
|
441
|
+
? `<the built file "${subpath}" names>`
|
|
442
|
+
: `./${subpath.slice(2)}.tsx`;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/** @typedef {import('../../api/integration/validate-integration.type.mjs').PublicImportUnchecked} PublicImportUnchecked */
|
|
446
|
+
/** @typedef {import('../../api/integration/validate-integration.type.mjs').PublicImportsReport} PublicImportsReport */
|
|
447
|
+
/** @typedef {{code: string, message: string}} PublicImportNote */
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Whether nothing a statement imports exists at run time: a statement-level
|
|
451
|
+
* `import type`, or named bindings that are each an inline `type`.
|
|
452
|
+
* @param {ImportStatement} statement
|
|
453
|
+
*/
|
|
454
|
+
function typeOnlyStatement(statement) {
|
|
455
|
+
if (statement.typeOnly) return true;
|
|
456
|
+
return (
|
|
457
|
+
!statement.namespace &&
|
|
458
|
+
statement.names.length > 0 &&
|
|
459
|
+
statement.names.every(binding => binding.typeOnly)
|
|
460
|
+
);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* The files the `types` conditions of an export value name that an ES module
|
|
465
|
+
* import can reach (never one under `require`), with a wildcard filled in.
|
|
466
|
+
* @param {unknown} value
|
|
467
|
+
* @param {string} wildcard
|
|
468
|
+
* @param {boolean} [underTypes]
|
|
469
|
+
* @param {string[]} [out]
|
|
470
|
+
* @returns {string[]}
|
|
471
|
+
*/
|
|
472
|
+
function typesTargets(value, wildcard, underTypes = false, out = []) {
|
|
473
|
+
if (typeof value === 'string') {
|
|
474
|
+
if (underTypes) out.push(value.replace('*', wildcard));
|
|
475
|
+
} else if (Array.isArray(value)) {
|
|
476
|
+
for (const v of value) typesTargets(v, wildcard, underTypes, out);
|
|
477
|
+
} else if (isRecord(value)) {
|
|
478
|
+
for (const [condition, v] of Object.entries(value)) {
|
|
479
|
+
if (condition === 'require') continue;
|
|
480
|
+
typesTargets(v, wildcard, underTypes || condition === 'types', out);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return out;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* The names a declaration file exports as types: exported interfaces and
|
|
488
|
+
* type aliases, `export type {…}`, and `export {…}` of a local interface or
|
|
489
|
+
* type alias. Anything else (a value, an `export *`) is not counted, so an
|
|
490
|
+
* import of it is listed as not checked. Null when the file cannot be read.
|
|
491
|
+
* @param {string} file
|
|
492
|
+
* @returns {Set<string> | null}
|
|
493
|
+
*/
|
|
494
|
+
function declaredTypeNames(file) {
|
|
495
|
+
let ast;
|
|
496
|
+
try {
|
|
497
|
+
ast = parse(fs.readFileSync(file, 'utf-8'), {
|
|
498
|
+
sourceType: 'module',
|
|
499
|
+
errorRecovery: true,
|
|
500
|
+
plugins: /** @type {any} */ ([
|
|
501
|
+
['typescript', {dts: /\.d\.[cm]?ts$/u.test(file)}],
|
|
502
|
+
]),
|
|
503
|
+
});
|
|
504
|
+
} catch {
|
|
505
|
+
return null;
|
|
506
|
+
}
|
|
507
|
+
/** @type {Set<string>} */
|
|
508
|
+
const local = new Set();
|
|
509
|
+
/** @type {Set<string>} */
|
|
510
|
+
const names = new Set();
|
|
511
|
+
/** @param {any} node */
|
|
512
|
+
const typeName = node =>
|
|
513
|
+
node &&
|
|
514
|
+
(node.type === 'TSInterfaceDeclaration' ||
|
|
515
|
+
node.type === 'TSTypeAliasDeclaration')
|
|
516
|
+
? node.id.name
|
|
517
|
+
: null;
|
|
518
|
+
for (const node of ast.program.body) {
|
|
519
|
+
const own = typeName(node);
|
|
520
|
+
if (own) local.add(own);
|
|
521
|
+
if (node.type === 'ExportNamedDeclaration') {
|
|
522
|
+
const declared = typeName(node.declaration);
|
|
523
|
+
if (declared) local.add(declared);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
/** @param {any} n */
|
|
527
|
+
const exportedName = n =>
|
|
528
|
+
n.type === 'Identifier' ? n.name : String(n.value);
|
|
529
|
+
for (const node of ast.program.body) {
|
|
530
|
+
if (node.type !== 'ExportNamedDeclaration') continue;
|
|
531
|
+
const declared = typeName(node.declaration);
|
|
532
|
+
if (declared) names.add(declared);
|
|
533
|
+
for (const s of node.specifiers ?? []) {
|
|
534
|
+
if (s.type !== 'ExportSpecifier') continue;
|
|
535
|
+
const exported = exportedName(s.exported);
|
|
536
|
+
if (node.exportKind === 'type' || s.exportKind === 'type')
|
|
537
|
+
names.add(exported);
|
|
538
|
+
else if (!node.source && local.has(exportedName(s.local)))
|
|
539
|
+
names.add(exported);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
return names;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* An import of a subpath whose `exports` entry has only `types` targets is
|
|
547
|
+
* never judged as a run-time import: accepted when it is `import type`, its
|
|
548
|
+
* every binding is an inline `type`, or every name it imports is one the
|
|
549
|
+
* `types` file declares as a type; otherwise the reason it was not checked.
|
|
550
|
+
* @param {Context} ctx
|
|
551
|
+
* @param {ImportStatement} statement
|
|
552
|
+
* @param {string} subpath
|
|
553
|
+
* @param {SubpathDeclaration} declaration
|
|
554
|
+
* @returns {string | null}
|
|
555
|
+
*/
|
|
556
|
+
function typesOnlyImport(ctx, statement, subpath, declaration) {
|
|
557
|
+
if (typeOnlyStatement(statement)) return null;
|
|
558
|
+
const not = `"${subpath}" reaches an ES module import only through a "types" target in package.json "exports", which nothing loads at run time`;
|
|
559
|
+
if (statement.namespace || statement.names.length === 0)
|
|
560
|
+
return `${not}, and a namespace or side-effect import of it cannot be judged from source, so it was not checked`;
|
|
561
|
+
const files = typesTargets(declaration.value, declaration.wildcard ?? '');
|
|
562
|
+
/** @type {Set<string>} */
|
|
563
|
+
const types = new Set();
|
|
564
|
+
let read = false;
|
|
565
|
+
for (const target of files) {
|
|
566
|
+
const found = declaredTypeNames(path.resolve(ctx.packageDir, target));
|
|
567
|
+
if (!found) continue;
|
|
568
|
+
read = true;
|
|
569
|
+
for (const n of found) types.add(n);
|
|
570
|
+
}
|
|
571
|
+
if (!read)
|
|
572
|
+
return `${not}, and its types file ${files.map(f => `"${f}"`).join(', ')} could not be read (it may be build output), so it was not checked`;
|
|
573
|
+
const unknown = statement.names
|
|
574
|
+
.filter(binding => !binding.typeOnly && !types.has(binding.imported))
|
|
575
|
+
.map(binding => binding.imported);
|
|
576
|
+
if (unknown.length === 0) return null;
|
|
577
|
+
return `${not}, and its types file does not declare ${unknown.join(', ')} as a type, so it was not checked`;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* Whether an import of the package's own name names a subpath `exports`
|
|
582
|
+
* declares: the declaration when it does not, or the reason it was not
|
|
583
|
+
* checked.
|
|
584
|
+
* @param {Context} ctx
|
|
585
|
+
* @param {ImportStatement} statement
|
|
586
|
+
* @param {string} subpath
|
|
587
|
+
* @returns {{undeclared: SubpathDeclaration | null, unchecked?: string}}
|
|
588
|
+
*/
|
|
589
|
+
function undeclaredOwnImport(ctx, statement, subpath) {
|
|
590
|
+
const declaration = declarationIn(ctx.exportsField, ctx.packageDir, subpath);
|
|
591
|
+
// A subpath an ES module import reaches only through `types` (any other
|
|
592
|
+
// condition beside it, such as `require`, loads nothing for it) is judged as
|
|
593
|
+
// a type import, never as a run-time one.
|
|
594
|
+
if (
|
|
595
|
+
declaration.state === 'not_importable' &&
|
|
596
|
+
typesTargets(declaration.value, declaration.wildcard ?? '').length > 0
|
|
597
|
+
) {
|
|
598
|
+
const unchecked = typesOnlyImport(ctx, statement, subpath, declaration);
|
|
599
|
+
return unchecked ? {undeclared: null, unchecked} : {undeclared: null};
|
|
600
|
+
}
|
|
601
|
+
return {undeclared: declaration.state === 'declared' ? null : declaration};
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* A component doc's import specifier, and every example's imports.
|
|
606
|
+
* @param {Context} ctx
|
|
607
|
+
* @param {any} integration
|
|
608
|
+
* @returns {Promise<PublicImportNote[]>}
|
|
609
|
+
*/
|
|
610
|
+
async function componentIssues(ctx, integration) {
|
|
611
|
+
/** @type {PublicImportNote[]} */
|
|
612
|
+
const issues = [];
|
|
613
|
+
// Every component check compares imports with the package's own name.
|
|
614
|
+
if (ctx.name == null) return issues;
|
|
615
|
+
let records;
|
|
616
|
+
try {
|
|
617
|
+
records = discoverIntegrationComponents(integration);
|
|
618
|
+
} catch {
|
|
619
|
+
return issues; // an unreadable root is reported by the root validators
|
|
620
|
+
}
|
|
621
|
+
/** @type {Map<string, typeof records>} */
|
|
622
|
+
const byDoc = new Map();
|
|
623
|
+
for (const record of records) {
|
|
624
|
+
const list = byDoc.get(record.docPath) ?? [];
|
|
625
|
+
list.push(record);
|
|
626
|
+
byDoc.set(record.docPath, list);
|
|
627
|
+
}
|
|
628
|
+
for (const [docPath, list] of byDoc) {
|
|
629
|
+
let doc;
|
|
630
|
+
try {
|
|
631
|
+
doc = await loadComponentDoc(docPath);
|
|
632
|
+
} catch {
|
|
633
|
+
continue; // reported by the doc validators
|
|
634
|
+
}
|
|
635
|
+
if (!isRecord(doc)) continue;
|
|
636
|
+
const shownDoc = rel(ctx.packageDir, docPath);
|
|
637
|
+
const record = list[0];
|
|
638
|
+
const source = record.sourcePath;
|
|
639
|
+
const sourceTarget = source ? `./${rel(ctx.packageDir, source)}` : null;
|
|
640
|
+
// The import the doc writes, or the one the CLI derives for it (its
|
|
641
|
+
// folder's subpath when exported, else the package root).
|
|
642
|
+
const authored = typeof doc.import === 'string' ? doc.import : null;
|
|
643
|
+
const specifier =
|
|
644
|
+
authored ??
|
|
645
|
+
resolveIntegrationImportPath(
|
|
646
|
+
{
|
|
647
|
+
exportsMap: /** @type {any} */ (ctx.exportsField) ?? null,
|
|
648
|
+
packageDir: ctx.packageDir,
|
|
649
|
+
docPath,
|
|
650
|
+
packageName: ctx.name,
|
|
651
|
+
},
|
|
652
|
+
record.name,
|
|
653
|
+
);
|
|
654
|
+
const split = splitSpecifier(specifier);
|
|
655
|
+
// The entry that exports this component alone, and the doc import that
|
|
656
|
+
// uses it: never one component's file as the package root or folder.
|
|
657
|
+
const entryKey = sourceTarget
|
|
658
|
+
? sourceTarget.replace(/\.[cm]?[jt]sx?$/u, '')
|
|
659
|
+
: null;
|
|
660
|
+
const perComponent = entryKey
|
|
661
|
+
? `add "${entryKey}": "${sourceTarget}" to package.json "exports" and set import: '${ctx.name}${entryKey.slice(1)}' in the doc`
|
|
662
|
+
: null;
|
|
663
|
+
const gives = authored
|
|
664
|
+
? `gives import "${specifier}"`
|
|
665
|
+
: `has no import, so apps are told to import from "${specifier}"`;
|
|
666
|
+
const declared = source
|
|
667
|
+
? subpathFor(ctx.exportsField, ctx.packageDir, source)
|
|
668
|
+
: null;
|
|
669
|
+
const suggested =
|
|
670
|
+
declared ??
|
|
671
|
+
(sourceTarget ? sourceTarget.replace(/\.[cm]?[jt]sx?$/u, '') : null);
|
|
672
|
+
if (split && split.pkg !== ctx.name) {
|
|
673
|
+
issues.push(
|
|
674
|
+
listed(
|
|
675
|
+
'component_import_other_package',
|
|
676
|
+
`Component doc "${shownDoc}" ${gives}, which names package "${split.pkg}", not this package "${ctx.name}", so apps are told to import the component from somewhere it is not. Fix: set import: '${ctx.name}${suggested ? suggested.slice(1) : ''}' in the doc.`,
|
|
677
|
+
),
|
|
678
|
+
);
|
|
679
|
+
} else if (split) {
|
|
680
|
+
const declaration = declarationIn(
|
|
681
|
+
ctx.exportsField,
|
|
682
|
+
ctx.packageDir,
|
|
683
|
+
split.subpath,
|
|
684
|
+
);
|
|
685
|
+
if (unbuiltOutput(ctx, split.subpath, declaration)) {
|
|
686
|
+
ctx.unchecked.push({
|
|
687
|
+
file: shownDoc,
|
|
688
|
+
specifier,
|
|
689
|
+
reason: unbuiltReason(split.subpath),
|
|
690
|
+
});
|
|
691
|
+
} else if (declaration.state !== 'declared') {
|
|
692
|
+
const already =
|
|
693
|
+
declared && declared !== split.subpath
|
|
694
|
+
? `set import: '${ctx.name}${declared.slice(1)}' (already exported), or `
|
|
695
|
+
: '';
|
|
696
|
+
issues.push(
|
|
697
|
+
listed(
|
|
698
|
+
'component_import_not_exported',
|
|
699
|
+
`Component doc "${shownDoc}" ${gives}, but ${whyUndeclared(declaration, split.subpath)}, so apps cannot import the component from it. Fix: ${already}${split.subpath === '.' && perComponent ? `${perComponent}.` : exportsFix(ctx.exportsField, declaration.state, split.subpath, fixTarget(split.subpath, sourceTarget))}`,
|
|
700
|
+
),
|
|
701
|
+
);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
/**
|
|
706
|
+
* The `exports` entry an example with an undeclared import needs: its
|
|
707
|
+
* subpath mapped to the file there, or to the component's source when
|
|
708
|
+
* the example imports only components this doc documents, else to the
|
|
709
|
+
* file that exports what it imports.
|
|
710
|
+
* @param {ImportStatement} statement
|
|
711
|
+
* @param {string} subpath
|
|
712
|
+
* @param {SubpathDeclaration} undeclared
|
|
713
|
+
*/
|
|
714
|
+
const exampleFix = (statement, subpath, undeclared) => {
|
|
715
|
+
const values = statement.names
|
|
716
|
+
.filter(binding => !binding.typeOnly)
|
|
717
|
+
.map(binding => binding.imported);
|
|
718
|
+
const atSubpath = [
|
|
719
|
+
'',
|
|
720
|
+
...MODULE_EXTENSIONS,
|
|
721
|
+
...MODULE_EXTENSIONS.map(ext => `/index${ext}`),
|
|
722
|
+
]
|
|
723
|
+
.map(ext => path.resolve(ctx.packageDir, `${subpath.slice(2)}${ext}`))
|
|
724
|
+
.find(
|
|
725
|
+
file =>
|
|
726
|
+
file.startsWith(path.resolve(ctx.packageDir) + path.sep) &&
|
|
727
|
+
isFile(file),
|
|
728
|
+
);
|
|
729
|
+
const documented = new Set(list.map(r => r.name));
|
|
730
|
+
const onlyDocumented =
|
|
731
|
+
values.length > 0 && values.every(name => documented.has(name));
|
|
732
|
+
const target = atSubpath
|
|
733
|
+
? `./${rel(ctx.packageDir, atSubpath)}`
|
|
734
|
+
: onlyDocumented && sourceTarget
|
|
735
|
+
? fixTarget(subpath, sourceTarget)
|
|
736
|
+
: `<the file that exports ${values.join(', ') || 'them'}>`;
|
|
737
|
+
return exportsFix(ctx.exportsField, undeclared.state, subpath, target);
|
|
738
|
+
};
|
|
739
|
+
|
|
740
|
+
const examples = Array.isArray(doc.examples) ? doc.examples : [];
|
|
741
|
+
for (const [index, example] of examples.entries()) {
|
|
742
|
+
if (!isRecord(example) || typeof example.code !== 'string') continue;
|
|
743
|
+
const label =
|
|
744
|
+
typeof example.label === 'string'
|
|
745
|
+
? `"${example.label}"`
|
|
746
|
+
: `#${index + 1}`;
|
|
747
|
+
for (const statement of readImports(example.code)) {
|
|
748
|
+
const parts = splitSpecifier(statement.source);
|
|
749
|
+
if (!parts || parts.pkg !== ctx.name) continue;
|
|
750
|
+
const {undeclared, unchecked} = undeclaredOwnImport(
|
|
751
|
+
ctx,
|
|
752
|
+
statement,
|
|
753
|
+
parts.subpath,
|
|
754
|
+
);
|
|
755
|
+
if (unchecked) {
|
|
756
|
+
ctx.unchecked.push({
|
|
757
|
+
file: shownDoc,
|
|
758
|
+
specifier: statement.source,
|
|
759
|
+
reason: `example ${label}: ${unchecked}`,
|
|
760
|
+
});
|
|
761
|
+
} else if (
|
|
762
|
+
undeclared &&
|
|
763
|
+
unbuiltOutput(ctx, parts.subpath, undeclared)
|
|
764
|
+
) {
|
|
765
|
+
ctx.unchecked.push({
|
|
766
|
+
file: shownDoc,
|
|
767
|
+
specifier: statement.source,
|
|
768
|
+
reason: `example ${label}: ${unbuiltReason(parts.subpath)}`,
|
|
769
|
+
});
|
|
770
|
+
} else if (undeclared) {
|
|
771
|
+
issues.push(
|
|
772
|
+
listed(
|
|
773
|
+
'example_import_not_exported',
|
|
774
|
+
`Component doc "${shownDoc}" example ${label} imports "${statement.source}", but ${whyUndeclared(undeclared, parts.subpath)}, so the example fails when an app copies it. Fix: ${exampleFix(statement, parts.subpath, undeclared)}`,
|
|
775
|
+
),
|
|
776
|
+
);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
return issues;
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
/**
|
|
785
|
+
* Template public imports and what each template source imports.
|
|
786
|
+
* @param {Context} ctx
|
|
787
|
+
* @param {any} integration
|
|
788
|
+
* @returns {Promise<PublicImportNote[]>}
|
|
789
|
+
*/
|
|
790
|
+
async function templateIssues(ctx, integration) {
|
|
791
|
+
/** @type {PublicImportNote[]} */
|
|
792
|
+
const issues = [];
|
|
793
|
+
const root = integration.templates;
|
|
794
|
+
if (!root) return issues;
|
|
795
|
+
let templates;
|
|
796
|
+
try {
|
|
797
|
+
({templates} = await discoverIntegrationTemplatesForOne(integration));
|
|
798
|
+
} catch {
|
|
799
|
+
return issues;
|
|
800
|
+
}
|
|
801
|
+
for (const template of templates) {
|
|
802
|
+
const id = template.dirName;
|
|
803
|
+
const file = template.filePath;
|
|
804
|
+
const shown = rel(ctx.packageDir, file);
|
|
805
|
+
const sourceTarget = `./${shown}`;
|
|
806
|
+
const subpath = `./${shown.replace(/\.tsx?$/u, '')}`;
|
|
807
|
+
const declaration = declarationIn(
|
|
808
|
+
ctx.exportsField,
|
|
809
|
+
ctx.packageDir,
|
|
810
|
+
subpath,
|
|
811
|
+
);
|
|
812
|
+
if (ctx.name != null && declaration.state !== 'declared') {
|
|
813
|
+
issues.push(
|
|
814
|
+
listed(
|
|
815
|
+
'template_import_not_exported',
|
|
816
|
+
`Template "${id}" has public import "${ctx.name}${subpath.slice(1)}", but ${whyUndeclared(declaration, subpath)}, so it cannot be imported once published. Fix: ${exportsFix(ctx.exportsField, declaration.state, subpath, sourceTarget)}`,
|
|
817
|
+
),
|
|
818
|
+
);
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
let text;
|
|
822
|
+
try {
|
|
823
|
+
text = fs.readFileSync(file, 'utf-8');
|
|
824
|
+
} catch {
|
|
825
|
+
text = '';
|
|
826
|
+
}
|
|
827
|
+
for (const statement of readImports(text)) {
|
|
828
|
+
const spec = statement.source;
|
|
829
|
+
if (spec.startsWith('.')) {
|
|
830
|
+
const target = path.resolve(path.dirname(file), spec);
|
|
831
|
+
if (target !== root && !target.startsWith(root + path.sep)) {
|
|
832
|
+
issues.push(
|
|
833
|
+
listed(
|
|
834
|
+
'template_import_outside_root',
|
|
835
|
+
`Template "${id}" (${shown}) imports "${spec}", a path outside the templates root; \`astryx template\` copies only the template into an app, where that path does not exist. Fix: import it through the package's public name (e.g. '${ctx.name ?? '<package name>'}/…' declared in package.json "exports"), or move the code into the template.`,
|
|
836
|
+
),
|
|
837
|
+
);
|
|
838
|
+
}
|
|
839
|
+
continue;
|
|
840
|
+
}
|
|
841
|
+
const parts = splitSpecifier(spec);
|
|
842
|
+
if (!parts) continue;
|
|
843
|
+
if (ctx.name != null && parts.pkg === ctx.name) {
|
|
844
|
+
const {undeclared, unchecked} = undeclaredOwnImport(
|
|
845
|
+
ctx,
|
|
846
|
+
statement,
|
|
847
|
+
parts.subpath,
|
|
848
|
+
);
|
|
849
|
+
if (unchecked) {
|
|
850
|
+
ctx.unchecked.push({file: shown, specifier: spec, reason: unchecked});
|
|
851
|
+
} else if (
|
|
852
|
+
undeclared &&
|
|
853
|
+
unbuiltOutput(ctx, parts.subpath, undeclared)
|
|
854
|
+
) {
|
|
855
|
+
ctx.unchecked.push({
|
|
856
|
+
file: shown,
|
|
857
|
+
specifier: spec,
|
|
858
|
+
reason: unbuiltReason(parts.subpath),
|
|
859
|
+
});
|
|
860
|
+
} else if (undeclared) {
|
|
861
|
+
issues.push(
|
|
862
|
+
listed(
|
|
863
|
+
'template_private_import',
|
|
864
|
+
`Template "${id}" (${shown}) imports "${spec}", but ${whyUndeclared(undeclared, parts.subpath)}, so the template breaks in an app. Fix: import from a subpath package.json "exports" declares, or add one for it.`,
|
|
865
|
+
),
|
|
866
|
+
);
|
|
867
|
+
}
|
|
868
|
+
} else if (parts.pkg === CORE && !ctx.corePkg) {
|
|
869
|
+
ctx.unchecked.push({
|
|
870
|
+
file: shown,
|
|
871
|
+
specifier: spec,
|
|
872
|
+
reason:
|
|
873
|
+
'@astryxdesign/core is not installed, so Core\'s package.json "exports" could not be read',
|
|
874
|
+
});
|
|
875
|
+
} else if (parts.pkg === CORE && ctx.corePkg && parts.subpath !== '.') {
|
|
876
|
+
const core = declarationIn(
|
|
877
|
+
ctx.corePkg.exports,
|
|
878
|
+
/** @type {string} */ (ctx.coreDir),
|
|
879
|
+
parts.subpath,
|
|
880
|
+
);
|
|
881
|
+
if (core.state !== 'declared') {
|
|
882
|
+
const top = parts.subpath
|
|
883
|
+
.split('/')
|
|
884
|
+
.filter(s => s && s !== '.' && s !== 'src')[0];
|
|
885
|
+
const hint =
|
|
886
|
+
top &&
|
|
887
|
+
subpathDeclaration(ctx.corePkg.exports, `./${top}`).state ===
|
|
888
|
+
'declared'
|
|
889
|
+
? `'${CORE}/${top}'`
|
|
890
|
+
: `a subpath Core's package.json "exports" declares`;
|
|
891
|
+
issues.push(
|
|
892
|
+
listed(
|
|
893
|
+
'template_private_import',
|
|
894
|
+
`Template "${id}" (${shown}) imports "${spec}", a path Core's package.json "exports" does not declare, so the template breaks in an app. Fix: import from ${hint}.`,
|
|
895
|
+
),
|
|
896
|
+
);
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
return issues;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
/**
|
|
905
|
+
* Every public-import finding for one package, as notes, and the report of
|
|
906
|
+
* what could not be judged. It returns no issues: the notes are listed
|
|
907
|
+
* only, and fail `--strict` from the next minor release.
|
|
908
|
+
* @param {string} packageDir
|
|
909
|
+
* @param {any} integration the loaded integration record
|
|
910
|
+
* @returns {Promise<{issues: Issue[], notes: PublicImportNote[], report: PublicImportsReport}>}
|
|
911
|
+
* `issues` is always empty: kept so validate's call site stays one shape
|
|
912
|
+
*/
|
|
913
|
+
export async function publicImportIssues(packageDir, integration) {
|
|
914
|
+
/** @type {PublicImportsReport} */
|
|
915
|
+
const report = {status: 'checked', findings: [], unchecked: []};
|
|
916
|
+
const pkg = readJson(path.join(packageDir, 'package.json'));
|
|
917
|
+
// What cannot run is listed as not checked, never a skip `--strict` would
|
|
918
|
+
// fail on.
|
|
919
|
+
if (!isRecord(pkg)) {
|
|
920
|
+
report.unchecked.push({
|
|
921
|
+
file: 'package.json',
|
|
922
|
+
specifier: '',
|
|
923
|
+
reason: 'package.json could not be read, so no import was checked',
|
|
924
|
+
});
|
|
925
|
+
return {issues: [], notes: [], report};
|
|
926
|
+
}
|
|
927
|
+
const raw = typeof pkg.name === 'string' && pkg.name !== '' ? pkg.name : null;
|
|
928
|
+
// A name that is not a bare package name (`acme/kit`) cannot be told apart
|
|
929
|
+
// from an import of another package: checked as if there were none.
|
|
930
|
+
const name = raw != null && isPackageName(raw) ? raw : null;
|
|
931
|
+
if (name == null) {
|
|
932
|
+
report.unchecked.push({
|
|
933
|
+
file: 'package.json',
|
|
934
|
+
specifier: '',
|
|
935
|
+
reason: `${raw == null ? 'package.json has no "name"' : `package.json "name" ${JSON.stringify(raw)} is not a bare package name`}, so imports of the package's own name were not checked; only template imports outside the templates root and of Core were`,
|
|
936
|
+
});
|
|
937
|
+
}
|
|
938
|
+
// The Core installed beside the package, which its templates' imports
|
|
939
|
+
// resolve against; never the CLI's own copy, which may be another release.
|
|
940
|
+
// A workspace's own `packages/core` (another package) is not it: then Core
|
|
941
|
+
// counts as not installed and its imports are listed as not checked.
|
|
942
|
+
const foundCore = name === CORE ? packageDir : findProjectCoreDir(packageDir);
|
|
943
|
+
const foundCorePkg = foundCore
|
|
944
|
+
? readJson(path.join(foundCore, 'package.json'))
|
|
945
|
+
: null;
|
|
946
|
+
const isCore = isRecord(foundCorePkg) && foundCorePkg.name === CORE;
|
|
947
|
+
/** @type {Context} */
|
|
948
|
+
const ctx = {
|
|
949
|
+
packageDir,
|
|
950
|
+
name,
|
|
951
|
+
exportsField: pkg.exports,
|
|
952
|
+
coreDir: isCore ? foundCore : null,
|
|
953
|
+
corePkg: isCore ? foundCorePkg : null,
|
|
954
|
+
unchecked: report.unchecked,
|
|
955
|
+
files: Array.isArray(pkg.files)
|
|
956
|
+
? pkg.files
|
|
957
|
+
.filter(entry => typeof entry === 'string')
|
|
958
|
+
.map(entry => entry.replace(/^\.\//u, '').replace(/\/+$/u, ''))
|
|
959
|
+
: [],
|
|
960
|
+
roots: [
|
|
961
|
+
integration.components,
|
|
962
|
+
integration.templates,
|
|
963
|
+
integration.docs,
|
|
964
|
+
integration.themes,
|
|
965
|
+
integration.codemods,
|
|
966
|
+
]
|
|
967
|
+
.filter(root => typeof root === 'string')
|
|
968
|
+
.map(root => path.resolve(root)),
|
|
969
|
+
};
|
|
970
|
+
const notes = [
|
|
971
|
+
...(await componentIssues(ctx, integration)),
|
|
972
|
+
...(await templateIssues(ctx, integration)),
|
|
973
|
+
];
|
|
974
|
+
// Listed only: none is an issue, so none counts or fails a check.
|
|
975
|
+
return {issues: [], notes, report};
|
|
976
|
+
}
|