An open source design system that's fully customizable and agent ready
TypeScript
chore(lint): Core's internal subpaths stay inside Core and Lab (#8561)
Cindy Zhangf19b6547b509
diff --git a/eslint.config.js b/eslint.config.jsindex 422479140a..5ce53ba8dc 100644--- a/eslint.config.js+++ b/eslint.config.js@@ -259,6 +259,18 @@ export default defineConfig('@typescript-eslint/switch-exhaustiveness-check': reactSeverity,},},+ // Core's internal subpaths (`@astryxdesign/core/internal/*`) and its+ // gesture, motion and touch-support source are for Core and Lab only.+ {+ files: ['**/*.{ts,tsx,mts,cts,js,jsx,mjs,cjs}'],+ ignores: ['packages/core/**', 'packages/lab/**', '**/dist/**'],+ plugins: {+ '@astryx': astryxEslintPlugin,+ },+ rules: {+ '@astryx/no-core-internal-import': 'error',+ },+ },// Copyright header — all source files must have the Meta copyright notice{files: ['**/*.{ts,tsx}'],@@ -763,20 +775,17 @@ export default defineConfig('Register commands with defineCommand(parent, doc, {fn, action}) so --help and the manifest come from the colocated CommandDoc. See CONTRIBUTING > Working on the astryx CLI.',},{- selector:- "CallExpression[callee.property.name='padEnd']",+ selector: "CallExpression[callee.property.name='padEnd']",message:'Use the formatter kit — record(), records({layout: "inline"}), list() — not .padEnd() (INV23, FR3). See architecture:cli-surface.',},{- selector:- "CallExpression[callee.property.name='padStart']",+ selector: "CallExpression[callee.property.name='padStart']",message:'Use the formatter kit — record(), records({layout: "inline"}), list() — not .padStart() (INV23, FR3). See architecture:cli-surface.',},{- selector:- "CallExpression[callee.property.name='repeat']",+ selector: "CallExpression[callee.property.name='repeat']",message:'Use the formatter kit — section(), text(), code() — not .repeat() for layout (INV23, FR3). See architecture:cli-surface.',},diff --git a/internal/eslint-plugin-astryx/README.md b/internal/eslint-plugin-astryx/README.mdindex 42ac66ec16..e3a2977b40 100644--- a/internal/eslint-plugin-astryx/README.md+++ b/internal/eslint-plugin-astryx/README.md@@ -185,6 +185,32 @@ function useSortedNames(names: string[]) {}```+### `@astryx/no-core-internal-import`++Reports imports of Core's internal subpaths (`@astryxdesign/core/internal` and+`@astryxdesign/core/internal/*`), which are reserved for Astryx's own packages+and sit outside the compatibility promise (`spec:AST-017` FR21), and of Core's+internal, gesture, motion and touch-support source (`@astryxdesign/core/gestures`,+`@astryxdesign/core/motion` and their subpaths, or any path into+`core/src/{internal,gestures,motion,touch-support}`), which a package outside Core and Lab reaches only through public entries+(`architecture:react-component-runtime` INV13). It checks `import`,+`export … from`, `import()`, `require()` and `typeof import()`, a dynamic+import or require named by a string or a template literal with no expressions. The repo config+turns it on for every linted file outside `packages/core` and `packages/lab`+(the global ignores still leave some scripts unlinted), so only Core and Lab+may import them.++An internal subpath is one file, `packages/core/src/internal/<name>.ts`, whose+header says it is reserved for Astryx's own packages, undocumented, and outside+the compatibility promise. It gets its own explicit entry in Core's+`package.json` `exports` (`"./internal/<name>": {source, types, default}`, never+a wildcard), listed with the file-module subpaths in `scripts/sync-exports.js`+(`FILE_MODULE_SUBPATH_EXPORTS`).++It is a custom rule, not a `no-restricted-imports` block: flat config overrides+a same-named rule per file, so a repo-wide block would undo the layer blocks+that already use `no-restricted-imports`.+### `@astryx/no-hardcoded-styles`Detects hardcoded CSS values in `stylex.create()` that should use Astryx tokens:diff --git a/internal/eslint-plugin-astryx/index.js b/internal/eslint-plugin-astryx/index.jsindex 65bc7a6897..ec3077132d 100644--- a/internal/eslint-plugin-astryx/index.js+++ b/internal/eslint-plugin-astryx/index.js@@ -21,6 +21,7 @@* - disabled-cursor: Flags a cursor that promises an interaction without giving way to not-allowed on a disabled element* - no-unstable-merged-refs: Flags render-time mergeRefs callbacks and unstable callback inputs to useMergedRefs* - no-light-dark-outside-theme: Flags CSS light-dark() in component source (a light/dark decision belongs to the theme layer, where a token pair reaches both schemes in every theme)+ * - no-core-internal-import: Keeps Core's internal subpaths (@astryxdesign/core/internal/*) inside Core and Lab* - no-raw-color: Flags a raw colour value (hex, rgb(), hsl(), oklch(), …) anywhere in component source, including inside light-dark()/color-mix(), behind a const, in a template literal, or as a var() fallback — the shapes no-hardcoded-styles cannot see** Philosophy: Strict for agents (CI), lenient for humans (local dev)@@ -52,6 +53,7 @@ import noReactNamespaceHooksRule from './no-react-namespace-hooks.js';import noUnstableMergedRefsRule from './no-unstable-merged-refs.js';import copyrightHeaderRule from './copyright-header.js';import noRawConsoleCliRule from './no-raw-console-cli.js';+import noCoreInternalImportRule from './no-core-internal-import.js';import requireBasePropsRule from './require-base-props.js';import requireRefPropRule from './require-ref-prop.js';import requireBasePropsPassthroughRule from './require-baseprops-passthrough.js';@@ -357,6 +359,7 @@ const plugin = {'require-baseprops-passthrough': requireBasePropsPassthroughRule,'copyright-header': copyrightHeaderRule,'no-raw-console-cli': noRawConsoleCliRule,+ 'no-core-internal-import': noCoreInternalImportRule,'no-hardcoded-i18n-string': noHardcodedI18nStringRule,'i18n-key-format': i18nKeyFormatRule,'require-table-section': requireTableSectionRule,diff --git a/internal/eslint-plugin-astryx/no-core-internal-import.js b/internal/eslint-plugin-astryx/no-core-internal-import.jsnew file mode 100644index 0000000000..7105371792--- /dev/null+++ b/internal/eslint-plugin-astryx/no-core-internal-import.js@@ -0,0 +1,115 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file no-core-internal-import.js+ * @description Reports imports of Core's internal subpaths and of its+ * internal, gesture, motion and touch-support source. The repo config turns it on for+ * every linted file outside packages/core and packages/lab (the global+ * ignores still leave some scripts unlinted), so only Core and Lab may import+ * them.+ *+ * Reported, by `import`, `export … from`, `import()`, `require()` and+ * `typeof import()` (a dynamic import or require named by a string or by a+ * template literal with no expressions):+ * - `@astryxdesign/core/internal` and any `@astryxdesign/core/internal/*`+ * subpath, which are reserved for Astryx's own packages and sit outside+ * the compatibility promise (spec:AST-017 FR21);+ * - `@astryxdesign/core/gestures`, `@astryxdesign/core/motion` and their+ * subpaths, and any path into `core/src/internal`, `core/src/gestures`,+ * `core/src/motion` or `core/src/touch-support`: Core's source, which a+ * package outside Core and Lab reaches only through public entries+ * (architecture:react-component-runtime INV13).+ *+ * A custom rule rather than a `no-restricted-imports` block, because flat+ * config overrides a same-named rule per file: a repo-wide block would undo+ * the layer blocks that already use `no-restricted-imports`.+ */++const INTERNAL = /^@astryxdesign\/core\/internal(?:\/|$)/;+// Core's internal entries' sources, gesture core, motion and touch support:+// by package path, or by a path into Core's source.+const DEEP = [+ /^@astryxdesign\/core\/(?:gestures|motion)(?:\/|$)/,+ /(?:^|\/)core\/src\/(?:internal|gestures|motion|touch-support)(?:\/|$)/,+];++const rule = {+ meta: {+ type: 'problem',+ docs: {+ description:+ "Disallow importing Core's internal subpaths (@astryxdesign/core/internal/*) outside Core and Lab",+ },+ schema: [],+ messages: {+ internal:+ "'{{source}}' is one of Core's internal subpaths, reserved for Astryx's own packages (Core and Lab) and outside the compatibility promise. Use Core's public API instead.",+ deep: "'{{source}}' reaches into Core's internal, gesture, motion or touch-support source, which is for Core and Lab only (architecture:react-component-runtime INV13). Use Core's public API instead.",+ },+ },+ create(context) {+ function check(node, source) {+ if (typeof source !== 'string') {+ return;+ }+ if (INTERNAL.test(source)) {+ context.report({node, messageId: 'internal', data: {source}});+ } else if (DEEP.some(pattern => pattern.test(source))) {+ context.report({node, messageId: 'deep', data: {source}});+ }+ }+ // A dynamic `import()` or `require()` names its module by a string, or+ // by a template literal with no expressions, which is a string too.+ function checkLiteral(node) {+ if (node == null) {+ return;+ }+ if (node.type === 'Literal') {+ check(node, node.value);+ } else if (+ node.type === 'TemplateLiteral' &&+ node.expressions.length === 0+ ) {+ check(node, node.quasis[0]?.value.cooked);+ }+ }+ return {+ ImportDeclaration(node) {+ check(node.source, node.source.value);+ },+ ExportNamedDeclaration(node) {+ if (node.source != null) {+ check(node.source, node.source.value);+ }+ },+ ExportAllDeclaration(node) {+ check(node.source, node.source.value);+ },+ ImportExpression(node) {+ checkLiteral(node.source);+ },+ // `require('…')` in CommonJS files.+ CallExpression(node) {+ if (+ node.callee.type === 'Identifier' &&+ node.callee.name === 'require' &&+ node.arguments.length > 0+ ) {+ checkLiteral(node.arguments[0]);+ }+ },+ // `typeof import('…')` in TypeScript types. typescript-eslint wraps+ // the specifier in a TSLiteralType.+ TSImportType(node) {+ const argument = node.argument;+ checkLiteral(+ argument != null && argument.type === 'TSLiteralType'+ ? argument.literal+ : argument,+ );+ },+ };+ },+};++export default rule;diff --git a/internal/eslint-plugin-astryx/no-core-internal-import.test.mjs b/internal/eslint-plugin-astryx/no-core-internal-import.test.mjsnew file mode 100644index 0000000000..d9a3f35a60--- /dev/null+++ b/internal/eslint-plugin-astryx/no-core-internal-import.test.mjs@@ -0,0 +1,135 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file no-core-internal-import.test.mjs+ */++import {RuleTester} from 'eslint';+import tseslint from 'typescript-eslint';+import rule from './no-core-internal-import.js';++// typescript-eslint's parser reads the JavaScript cases too, and gives the+// `typeof import('…')` case its TSImportType node.+const ruleTester = new RuleTester({+ languageOptions: {+ parser: tseslint.parser,+ ecmaVersion: 2022,+ sourceType: 'module',+ },+});++const message = source =>+ `'${source}' is one of Core's internal subpaths, reserved for Astryx's own packages (Core and Lab) and outside the compatibility promise. Use Core's public API instead.`;+const deep = source =>+ `'${source}' reaches into Core's internal, gesture, motion or touch-support source, which is for Core and Lab only (architecture:react-component-runtime INV13). Use Core's public API instead.`;++ruleTester.run('no-core-internal-import', rule, {+ valid: [+ // Core's public subpaths stay open.+ {code: `import {List} from '@astryxdesign/core/List';`},+ {code: `import {createReorderable} from '@astryxdesign/core/reorderable';`},+ // A path that only contains the word is not the internal subpath.+ {code: `import {x} from '@astryxdesign/core/internalization';`},+ {code: `import {x} from './internal/reorder';`},+ {code: `const core = require('@astryxdesign/core/List');`},+ // A computed specifier is not a literal subpath.+ {code: `const m = require(name);`},+ {code: `const r = load('@astryxdesign/core/internal/reorder');`},+ {code: `type T = typeof import('@astryxdesign/core/reorderable');`},+ // Names that only start like the deep paths.+ {code: `import {x} from '@astryxdesign/core/motionless';`},+ {code: `import {x} from '@astryxdesign/core/gesturesKit';`},+ {code: `import {x} from '../core/src/gesturesKit/x';`},+ {code: `import {x} from '../core/src/internals/x';`},+ // A code sample in a string is not an import.+ {+ code: "const sample = `import {duration} from '@astryxdesign/core/motion';`;",+ },+ ],+ invalid: [+ {+ code: `import {useReorderSession} from '@astryxdesign/core/internal/reorder';`,+ errors: [{message: message('@astryxdesign/core/internal/reorder')}],+ },+ {+ code: `export {useReorderSession} from '@astryxdesign/core/internal/reorder';`,+ errors: [{message: message('@astryxdesign/core/internal/reorder')}],+ },+ {+ code: `export * from '@astryxdesign/core/internal/touch';`,+ errors: [{message: message('@astryxdesign/core/internal/touch')}],+ },+ {+ code: `const m = import('@astryxdesign/core/internal/reorder');`,+ errors: [{message: message('@astryxdesign/core/internal/reorder')}],+ },+ {+ code: `import '@astryxdesign/core/internal';`,+ errors: [{message: message('@astryxdesign/core/internal')}],+ },+ {+ code: `const touch = require('@astryxdesign/core/internal/touch');`,+ errors: [{message: message('@astryxdesign/core/internal/touch')}],+ },+ {+ code: `type Touch = typeof import('@astryxdesign/core/internal/touch');`,+ errors: [{message: message('@astryxdesign/core/internal/touch')}],+ },+ // The gesture core, motion and touch support, by package path...+ {+ code: `import {GestureArena} from '@astryxdesign/core/gestures';`,+ errors: [{message: deep('@astryxdesign/core/gestures')}],+ },+ {+ code: `import {createAnimator} from '@astryxdesign/core/motion/animator';`,+ errors: [{message: deep('@astryxdesign/core/motion/animator')}],+ },+ {+ code: `export * from '@astryxdesign/core/gestures/PanGestureRecognizer';`,+ errors: [+ {message: deep('@astryxdesign/core/gestures/PanGestureRecognizer')},+ ],+ },+ {+ code: `const m = import('@astryxdesign/core/motion');`,+ errors: [{message: deep('@astryxdesign/core/motion')}],+ },+ {+ code: `const motion = require('@astryxdesign/core/motion');`,+ errors: [{message: deep('@astryxdesign/core/motion')}],+ },+ {+ code: `type G = typeof import('@astryxdesign/core/gestures');`,+ errors: [{message: deep('@astryxdesign/core/gestures')}],+ },+ // ...and by a path into Core's source.+ {+ code: `import {touchModule} from '../../packages/core/src/touch-support/touchModule';`,+ errors: [+ {message: deep('../../packages/core/src/touch-support/touchModule')},+ ],+ },+ {+ code: `import {GestureArena} from '../../../packages/core/src/internal/touch';`,+ errors: [{message: deep('../../../packages/core/src/internal/touch')}],+ },+ // A template literal with no expressions names the module as a string+ // does.+ {+ code: 'const t = import(`@astryxdesign/core/internal/touch`);',+ errors: [{message: message('@astryxdesign/core/internal/touch')}],+ },+ {+ code: 'const t = require(`@astryxdesign/core/internal/touch`);',+ errors: [{message: message('@astryxdesign/core/internal/touch')}],+ },+ {+ code: `const g = require('../core/src/gestures/GestureArena');`,+ errors: [{message: deep('../core/src/gestures/GestureArena')}],+ },+ {+ code: `type M = typeof import('../../core/src/motion/spring');`,+ errors: [{message: deep('../../core/src/motion/spring')}],+ },+ ],+});