Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

[CLI] doctor --built does not count type-only Astryx imports as use (#7945)

Joey1b56e001e704
diff --git a/.changeset/built-type-imports.md b/.changeset/built-type-imports.md
new file mode 100644
index 0000000000..90007209c1
--- /dev/null
+++ b/.changeset/built-type-imports.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] `astryx doctor --built` no longer counts a type-only Astryx import (`import type`, `import {type X}`, `export type … from`) as Astryx use, since the build erases it.
+
+@josephfarina
diff --git a/packages/cli/api/doctor/built-checks.mjs b/packages/cli/api/doctor/built-checks.mjs
index 2f48c83bb1..6b1a5007ad 100644
--- a/packages/cli/api/doctor/built-checks.mjs
+++ b/packages/cli/api/doctor/built-checks.mjs
@@ -9,7 +9,7 @@
* built `.html` page or a source file), and the package names whose
* components count as Astryx: every `@astryxdesign/*` package and each
* integration the project loads (its component catalog's providers).
- * @output `built-astryx-usage`: fails when the path holds no import from those
+ * @output `built-astryx-usage`: fails when the path holds no value import from those
* packages, no use of a component imported from them, and no Astryx
* stylesheet in built CSS or HTML. `built-astryx-purity`: warns with the
* findings of the source check in `astryx docs is-my-code-good` (raw
@@ -323,12 +323,17 @@ function astImports(ast) {
/** @type {{spec: string, names: string[], at: number}[]} */
const found = [];
for (const node of ast.program.body) {
+ // A type-only import or re-export is erased from the built page, so it
+ // is not Astryx use.
+ if (node.importKind === 'type' || node.exportKind === 'type') continue;
if (node.type === 'ImportDeclaration') {
+ const values = node.specifiers.filter(
+ (/** @type {any} */ each) => each.importKind !== 'type',
+ );
+ if (node.specifiers.length > 0 && values.length === 0) continue;
found.push({
spec: node.source.value,
- names: node.specifiers.map(
- (/** @type {any} */ each) => each.local.name,
- ),
+ names: values.map((/** @type {any} */ each) => each.local.name),
at: node.start,
});
} else if (
@@ -336,6 +341,15 @@ function astImports(ast) {
node.type === 'ExportAllDeclaration') &&
node.source
) {
+ const specifiers = node.specifiers ?? [];
+ if (
+ specifiers.length > 0 &&
+ specifiers.every(
+ (/** @type {any} */ each) => each.exportKind === 'type',
+ )
+ ) {
+ continue;
+ }
found.push({spec: node.source.value, names: [], at: node.start});
}
}
@@ -406,14 +420,26 @@ function importsOf(text) {
/** @type {{spec: string, names: string[], at: number}[]} */
const found = [];
const fromImport =
- /\bimport\s+(?:type\s+)?([^'";]*?)\s+from\s+['"]([^'"]+)['"]|\bimport\s+['"]([^'"]+)['"]/g;
+ /\bimport\s+(type\s+)?([^'";]*?)\s+from\s+['"]([^'"]+)['"]|\bimport\s+['"]([^'"]+)['"]/g;
+ /** Whether every name in a `{…}` list is marked `type`. */
+ const allTypes = (/** @type {string} */ list) => {
+ const parts = list
+ .split(',')
+ .map(part => part.trim())
+ .filter(Boolean);
+ return parts.length > 0 && parts.every(part => /^type\s/.test(part));
+ };
for (const match of text.matchAll(fromImport)) {
const at = /** @type {number} */ (match.index);
- if (match[3]) {
- found.push({spec: match[3], names: [], at});
+ if (match[4]) {
+ found.push({spec: match[4], names: [], at});
continue;
}
- const clause = match[1];
+ // A type-only import is erased from the build, so it is not use.
+ if (match[1]) continue;
+ const clause = match[2];
+ const onlyBraces = /^\{([\s\S]*)\}$/.exec(clause.trim());
+ if (onlyBraces && allTypes(onlyBraces[1])) continue;
/** @type {string[]} */
const names = [];
const braces = /\{([\s\S]*)\}/.exec(clause);
@@ -433,14 +459,18 @@ function importsOf(text) {
const name = part.trim().replace(/^\*\s+as\s+/, '');
if (/^[A-Za-z_$][\w$]*$/.test(name)) names.push(name);
}
- found.push({spec: match[2], names, at});
+ found.push({spec: match[3], names, at});
}
- // A barrel that re-exports a package reaches it as an import does.
+ // A barrel that re-exports a package reaches it as an import does; a
+ // type-only re-export is erased like a type-only import.
for (const match of text.matchAll(
- /\bexport\s+(?:type\s+)?(?:\*(?:\s+as\s+[\w$]+)?|\{[^}]*\})\s*from\s+['"]([^'"]+)['"]/g,
+ /\bexport\s+(type\s+)?(\*(?:\s+as\s+[\w$]+)?|\{[^}]*\})\s*from\s+['"]([^'"]+)['"]/g,
)) {
+ if (match[1]) continue;
+ const list = /^\{([\s\S]*)\}$/.exec(match[2]);
+ if (list && allTypes(list[1])) continue;
found.push({
- spec: match[1],
+ spec: match[3],
names: [],
at: /** @type {number} */ (match.index),
});
@@ -738,7 +768,7 @@ export function checkBuilt(target, {cwd, packages}) {
id: 'built-astryx-usage',
label: 'Built output uses Astryx',
status: 'fail',
- message: `Read ${read}: no import from an Astryx package, no Astryx component, and no Astryx styles, so this output does not use Astryx.`,
+ message: `Read ${read}: no value import from an Astryx package (a type-only import is erased from the build), no Astryx component, and no Astryx styles, so this output does not use Astryx.`,
fix: `Rebuild the page from an Astryx template: run \`${formatCliCommand('build "<what the page is>"', cwd)}\`, scaffold the template it names, and fill it with Astryx components. Do not deliver this output.`,
};
const unreadNames =
diff --git a/packages/cli/api/doctor/built-checks.test.mjs b/packages/cli/api/doctor/built-checks.test.mjs
index 491275048b..145ecd70b7 100644
--- a/packages/cli/api/doctor/built-checks.test.mjs
+++ b/packages/cli/api/doctor/built-checks.test.mjs
@@ -425,6 +425,35 @@ describe('--built edge cases', () => {
expect(check('dist').usage.status).toBe('fail');
});
+ it('does not count a type-only Astryx import as use', () => {
+ write({
+ 'src/Page.tsx': [
+ "import type {ButtonProps} from '@astryxdesign/core/Button';",
+ "import {type TextProps} from '@astryxdesign/core/Text';",
+ "export type {CardProps} from '@astryxdesign/core/Card';",
+ "export {type BadgeProps} from '@astryxdesign/core/Badge';",
+ 'export const Page = (props: ButtonProps & TextProps) => null;',
+ '',
+ ].join('\n'),
+ });
+ expect(check('src').usage.status).toBe('fail');
+ });
+
+ it('does not count a type-only import in a file the parser cannot read', () => {
+ write({
+ 'src/Broken.tsx': [
+ "import type {ButtonProps} from '@astryxdesign/core/Button';",
+ "import {type TextProps} from '@astryxdesign/core/Text';",
+ "export type {CardProps} from '@astryxdesign/core/Card';",
+ "export {type BadgeProps} from '@astryxdesign/core/Badge';",
+ 'export const B = () => <div>{</div>;',
+ '@@ enum ;',
+ '',
+ ].join('\n'),
+ });
+ expect(check('src').usage.status).not.toBe('pass');
+ });
+
it('does not count an import written only in a string or JSX text', () => {
write({
'src/Page.tsx': [
diff --git a/packages/cli/api/doctor/doctor.doc.mjs b/packages/cli/api/doctor/doctor.doc.mjs
index dd12045de4..f6964f081b 100644
--- a/packages/cli/api/doctor/doctor.doc.mjs
+++ b/packages/cli/api/doctor/doctor.doc.mjs
@@ -41,7 +41,7 @@ export const doc = {
name: 'options.built',
type: 'string',
description:
- 'A finished build to check before you deliver it: a project folder, a build output folder, or one built `.html` file, relative to `cwd`. Adds two checks after the others. `built-astryx-usage` fails when the path holds no import from an Astryx package (any `@astryxdesign/*` package or an integration the project installs), no component imported from one, and no Astryx styles in built CSS or HTML. `built-astryx-purity` warns with the source check of `astryx docs is-my-code-good`: raw elements Astryx has a component for, raw SVG and icon-library icons, inline styles, classes, `stylex.create` and imported stylesheets, each with its file and line, in `.tsx` and `.jsx` files and JSX in `.js` source, found by parsing it; it is info, not inspected, when the path holds no source with JSX, such as a bundle. It reads at most 20,000 files, each up to 5 MB, skipping installs and version control; when either limit leaves source unread and nothing was found, the purity check is info, not fully inspected, and when the file limit ends the read before any Astryx turns up, `built-astryx-usage` warns that the rest went unread instead of failing. Given one built `.html` file, it also reads the local stylesheets and scripts that page links, finding a root-relative link from the nearest folder above the page that holds it; a linked file that is not there makes `built-astryx-usage` warn instead of fail. An empty folder fails with nothing checked, like a missing path. A source tree counts as using Astryx through an import, a re-export (`export … from`) or a stylesheet `@import` of an Astryx package. `--built` goes with `doctor` alone; a `doctor integration` check refuses it. A path that does not exist fails `built-astryx-usage` with nothing checked, and the purity check is left out. The integrations that count are the dependencies the nearest package.json above the path declares whose installed copy, found the way the CLI autolinks (a hoisted workspace copy included), has an integration manifest. The files under the path, and that configuration, are only read, never run. Without it, doctor runs exactly as before.',
+ 'A finished build to check before you deliver it: a project folder, a build output folder, or one built `.html` file, relative to `cwd`. Adds two checks after the others. `built-astryx-usage` fails when the path holds no value import from an Astryx package (any `@astryxdesign/*` package or an integration the project installs), no component imported from one, and no Astryx styles in built CSS or HTML. `built-astryx-purity` warns with the source check of `astryx docs is-my-code-good`: raw elements Astryx has a component for, raw SVG and icon-library icons, inline styles, classes, `stylex.create` and imported stylesheets, each with its file and line, in `.tsx` and `.jsx` files and JSX in `.js` source, found by parsing it; it is info, not inspected, when the path holds no source with JSX, such as a bundle. It reads at most 20,000 files, each up to 5 MB, skipping installs and version control; when either limit leaves source unread and nothing was found, the purity check is info, not fully inspected, and when the file limit ends the read before any Astryx turns up, `built-astryx-usage` warns that the rest went unread instead of failing. Given one built `.html` file, it also reads the local stylesheets and scripts that page links, finding a root-relative link from the nearest folder above the page that holds it; a linked file that is not there makes `built-astryx-usage` warn instead of fail. An empty folder fails with nothing checked, like a missing path. A source tree counts as using Astryx through an import, a re-export (`export … from`) or a stylesheet `@import` of an Astryx package; a type-only import or re-export (`import type`, `export type`, or a list whose names are all `type`) does not count, since the build erases it. `--built` goes with `doctor` alone; a `doctor integration` check refuses it. A path that does not exist fails `built-astryx-usage` with nothing checked, and the purity check is left out. The integrations that count are the dependencies the nearest package.json above the path declares whose installed copy, found the way the CLI autolinks (a hoisted workspace copy included), has an integration manifest. The files under the path, and that configuration, are only read, never run. Without it, doctor runs exactly as before.',
},
],
returns: [