Skip to content

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

TypeScript

fix(cli): experimental icon sets — accept a generated declaration file a formatter rewrote (#10898)

Joeyec8a19e41f8c
diff --git a/.changeset/icon-sets-formatted-declarations.md b/.changeset/icon-sets-formatted-declarations.md
new file mode 100644
index 0000000000..f8545fc957
--- /dev/null
+++ b/.changeset/icon-sets-formatted-declarations.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] Icon sets: `integration check` now accepts a generated declaration file (`*.generated.d.ts`) whose long type arguments a formatter such as Prettier re-wrapped. Before, the re-wrapped line made the check report `icon_generated_types_mismatch` and fail. A changed declaration still fails the check. The experimental icon commands are unchanged.
+@josephfarina
diff --git a/packages/cli/clients/cli/commands/icon-generated-format.test.mjs b/packages/cli/clients/cli/commands/icon-generated-format.test.mjs
new file mode 100644
index 0000000000..4e26dec493
--- /dev/null
+++ b/packages/cli/clients/cli/commands/icon-generated-format.test.mjs
@@ -0,0 +1,184 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file Icon sets whose generated module and declaration file a project
+ * formatter rewrote (Prettier re-wraps long lines, and may drop semicolons
+ * and switch quotes): `integration check --strict` still passes, and a real
+ * change to a declared export still fails it.
+ */
+
+import * as fs from 'node:fs';
+import * as os from 'node:os';
+import * as path from 'node:path';
+import * as prettier from 'prettier';
+import {afterEach, beforeEach, describe, expect, it} from 'vitest';
+import {runCli} from '../../../test-utils/run-cli.mjs';
+import {ICON_NAMES} from '../../../authoring/doctypes/icon-set/icon-names.mjs';
+
+/** @type {string} */
+let tmp;
+/** @type {string} */
+let pkg;
+
+const LIBRARY = '@acme/glyph-data';
+const COMPONENTS = '@acme/glyph-components';
+
+/**
+ * @param {string} name
+ * @param {string} source its index.js
+ */
+function writeLibrary(name, source) {
+ const lib = path.join(pkg, 'node_modules', ...name.split('/'));
+ fs.mkdirSync(lib, {recursive: true});
+ fs.writeFileSync(
+ path.join(lib, 'package.json'),
+ JSON.stringify({
+ name,
+ version: '1.0.0',
+ license: 'MIT',
+ type: 'module',
+ exports: {'.': './index.js'},
+ }),
+ );
+ fs.writeFileSync(path.join(lib, 'LICENSE'), 'MIT License\n');
+ fs.writeFileSync(path.join(lib, 'index.js'), source);
+}
+
+beforeEach(() => {
+ tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-icon-formatted-'));
+ pkg = path.join(tmp, 'icons');
+ fs.mkdirSync(path.join(pkg, 'src'), {recursive: true});
+ fs.writeFileSync(
+ path.join(pkg, 'package.json'),
+ JSON.stringify(
+ {
+ name: '@acme/icons',
+ version: '1.0.0',
+ license: 'MIT',
+ type: 'module',
+ exports: {'.': './index.js'},
+ dependencies: {[LIBRARY]: '^1.0.0', [COMPONENTS]: '^1.0.0'},
+ },
+ null,
+ 2,
+ ),
+ );
+ fs.writeFileSync(
+ path.join(pkg, 'src', 'adapt.js'),
+ 'export function adapt(data) { return () => null; }\n',
+ );
+ fs.writeFileSync(
+ path.join(pkg, 'index.js'),
+ "export * from './icon-sets/glyphs/glyphs.generated.js';\n",
+ );
+ // Long names make every kind of generated line wrap at 80 columns.
+ writeLibrary(
+ LIBRARY,
+ [
+ "export const glyphMagnifyingGlassWithPlusSign = {path: 'M0 0'};",
+ "export const glyphArrowsPointingOutwardDiagonal = {path: 'M1 1'};",
+ "export const glyphX = {path: 'M2 2'};",
+ '',
+ ].join('\n'),
+ );
+ writeLibrary(
+ COMPONENTS,
+ [
+ 'export function CalendarDaysWithClockOverlayOutline(props) { return null; }',
+ 'export function HomeModern(props) { return null; }',
+ '',
+ ].join('\n'),
+ );
+});
+afterEach(() => {
+ fs.rmSync(tmp, {recursive: true, force: true});
+});
+
+/** @param {string[]} args */
+async function json(args) {
+ const res = await runCli([...args, '--json'], pkg);
+ return {code: res.code, body: JSON.parse(res.stdout)};
+}
+
+const setFile = (/** @type {string} */ ext) =>
+ path.join(pkg, 'icon-sets', 'glyphs', `glyphs.${ext}`);
+
+/** A set of adapted data icons and component re-exports, fully mapped. */
+async function writeSet() {
+ const created = await json(['experimental', 'icon', 'new-set', 'glyphs']);
+ expect(created.code, JSON.stringify(created.body)).toBe(0);
+ const data = JSON.parse(fs.readFileSync(setFile('icons.json'), 'utf-8'));
+ data.adapter = {module: './src/adapt.js', export: 'adapt'};
+ fs.writeFileSync(setFile('icons.json'), JSON.stringify(data));
+ for (const args of [
+ ['new-set', 'glyphs', '--from', LIBRARY],
+ [
+ 'add',
+ 'CalendarDaysWithClockOverlayOutline',
+ '--from',
+ COMPONENTS,
+ '--definition',
+ 'calendar',
+ ],
+ ['add', 'HomeModern', '--from', COMPONENTS, '--definition', 'home'],
+ ['map', '--pairs', ICON_NAMES.map(name => `${name}=x`).join(',')],
+ ]) {
+ const done = await json(['experimental', 'icon', ...args]);
+ expect(done.code, `${args[0]}: ${JSON.stringify(done.body)}`).toBe(0);
+ }
+}
+
+/**
+ * Format the generated module and its declaration file in place.
+ * @param {import('prettier').Options} options
+ */
+async function formatGenerated(options) {
+ for (const ext of ['generated.js', 'generated.d.ts']) {
+ const file = setFile(ext);
+ const text = fs.readFileSync(file, 'utf-8');
+ fs.writeFileSync(
+ file,
+ await prettier.format(text, {filepath: file, printWidth: 80, ...options}),
+ );
+ }
+}
+
+/**
+ * The codes the strict check reports: every issue of every check, and every
+ * failure except the skipped TypeScript work.
+ */
+async function strictCodes() {
+ const res = await runCli(['integration', 'check', '--strict', '--json'], pkg);
+ const {checks, failures} = JSON.parse(res.stdout).data;
+ return [
+ ...checks.flatMap(
+ (/** @type {any} */ each) => each.response?.data?.issues ?? [],
+ ),
+ ...failures,
+ ]
+ .map((/** @type {{code: string}} */ each) => each.code)
+ .filter((/** @type {string} */ code) => code !== 'doc_types_skipped');
+}
+
+describe('generated files a formatter rewrote', () => {
+ it.each([
+ ['Prettier defaults at 80 columns', {}],
+ ['no semicolons and single quotes', {semi: false, singleQuote: true}],
+ ])(
+ 'the strict check passes after %s, and fails on a changed export name',
+ async (_, options) => {
+ await writeSet();
+ expect(await strictCodes()).toEqual([]);
+ await formatGenerated(options);
+ const types = fs.readFileSync(setFile('generated.d.ts'), 'utf-8');
+ // The formatter really re-wrapped a long type argument.
+ expect(types).toMatch(/ComponentType<\n\s+SVGProps<SVGSVGElement>\n>/u);
+ expect(await strictCodes()).toEqual([]);
+ fs.writeFileSync(
+ setFile('generated.d.ts'),
+ types.replace('HomeModernIcon', 'HomeModernGlyph'),
+ );
+ expect(await strictCodes()).toContain('icon_generated_types_mismatch');
+ },
+ );
+});
diff --git a/packages/cli/foundation/discovery/icon-set-writer.mjs b/packages/cli/foundation/discovery/icon-set-writer.mjs
index b98fa05e30..67c3345e4c 100644
--- a/packages/cli/foundation/discovery/icon-set-writer.mjs
+++ b/packages/cli/foundation/discovery/icon-set-writer.mjs
@@ -1423,10 +1423,39 @@ function terminated(statement) {
: `${statement};`;
}
+/** Syntax-tree fields that only say how a file is laid out. */
+const LAYOUT_KEYS = new Set([
+ 'start',
+ 'end',
+ 'loc',
+ 'range',
+ 'extra',
+ 'leadingComments',
+ 'trailingComments',
+ 'innerComments',
+]);
+
+/**
+ * The top-level statements of a module as syntax trees without positions,
+ * comments, or raw text, one string each. A formatter changes line breaks,
+ * quotes, semicolons, trailing commas, and the wrapping of long type
+ * arguments; none of these change the tree.
+ * @param {string} text
+ * @returns {string[]}
+ */
+function statementTrees(text) {
+ const ast = parse(text, {sourceType: 'module', plugins: ['typescript']});
+ return ast.program.body.map(node =>
+ JSON.stringify(node, (key, value) =>
+ LAYOUT_KEYS.has(key) ? undefined : value,
+ ),
+ );
+}
+
/**
* Whether a generated declaration file declares what its generated module
* gives, statement by statement however a formatter left either file
- * ({@link generatedStatements}); the header comment is ignored.
+ * ({@link statementTrees}); the header comment is ignored.
* @param {string} actual the declaration file as it is
* @param {string} expected {@link generatedTypesText} for the module
* @param {string} label the declaration file's path, for a parse error
@@ -1434,11 +1463,19 @@ function terminated(statement) {
export function generatedTypesMatch(actual, expected, label) {
let have;
try {
- have = generatedStatements(actual, label);
+ have = statementTrees(actual);
} catch {
return false;
}
- const want = generatedStatements(expected, label);
+ let want;
+ try {
+ want = statementTrees(expected);
+ } catch (error) {
+ throw new IconSetAuthoringError(
+ `The declarations written for ${label} do not parse (${error instanceof Error ? error.message : String(error)}).`,
+ 'ERR_INVALID_ARGUMENT',
+ );
+ }
return have.length === want.length && have.every((s, i) => s === want[i]);
}