From d6250fe576f4f2a676bf5848a5bcf39a64734da5 Mon Sep 17 00:00:00 2001 From: Anurag Date: Wed, 16 Sep 2020 12:09:29 +0530 Subject: [PATCH 1/4] feat: added key generation script --- package.json | 3 + scripts/build/keys.js | 8 + scripts/utils.js | 383 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 394 insertions(+) create mode 100644 scripts/build/keys.js create mode 100644 scripts/utils.js diff --git a/package.json b/package.json index 3271c8c52..94b4a444e 100644 --- a/package.json +++ b/package.json @@ -66,6 +66,7 @@ "@typescript-eslint/parser": "4.1.0", "babel-eslint": "10.1.0", "babel-loader": "8.1.0", + "chalk": "^4.1.0", "eslint": "7.8.1", "eslint-config-prettier": "6.11.0", "eslint-config-react-app": "5.2.1", @@ -82,6 +83,7 @@ "jest-in-case": "^1.0.2", "jest-matcher-utils": "26.4.2", "lint-staged": "10.3.0", + "lodash": "^4.17.20", "prettier": "2.1.1", "react": "16.13.1", "react-dom": "16.13.1", @@ -91,6 +93,7 @@ "react-transition-group": "4.4.1", "sort-package-json": "1.44.0", "ts-jest": "26.3.0", + "ts-morph": "^8.1.0", "typescript": "4.0.2" } } diff --git a/scripts/build/keys.js b/scripts/build/keys.js new file mode 100644 index 000000000..072836426 --- /dev/null +++ b/scripts/build/keys.js @@ -0,0 +1,8 @@ +#!/usr/bin/env node +const { hasTSConfig, makeKeys } = require("./utils"); + +const cwd = process.cwd(); + +if (hasTSConfig(cwd)) { + makeKeys(cwd); +} diff --git a/scripts/utils.js b/scripts/utils.js new file mode 100644 index 000000000..433fb8201 --- /dev/null +++ b/scripts/utils.js @@ -0,0 +1,383 @@ +const { join, dirname, basename } = require("path"); +const { toUpper, snakeCase, isEqual } = require("lodash"); +const prettier = require("prettier"); +const { + readdirSync, + writeFileSync, + lstatSync, + existsSync, +} = require("fs-extra"); +const { Project } = require("ts-morph"); +const chalk = require("chalk"); + +function log(...args) { + console.log(...args); +} + +/** + * Converts ./path/to/file.js to ./path/to + * @param {string} dir + */ +function resolveDir(dir) { + if (!/\.(t|j)s$/.test(dir)) { + return dir; + } + return dirname(dir); +} + +/** + * @param {string} rootPath + */ +function getPackage(rootPath) { + return require(join(rootPath, "package.json")); +} + +/** + * @param {string} path + */ +function removeExt(path) { + return path.replace(/\.[^.]+$/, ""); +} + +/** + * @param {string} path + */ +function isDirectory(path) { + return lstatSync(path).isDirectory(); +} + +/** + * @param {string} rootPath + */ +function getSourcePath(rootPath) { + return join(rootPath, "src"); +} + +/** + * Ensure that paths are consistent across Windows and non-Windows platforms. + * @param {string} filePath + */ +function normalizePath(filePath) { + return filePath.replace(/\\/g, "/"); +} + +/** + * Filters out files starting with __ + * Includes directories and TS/JS files. + * @param {string} rootPath + * @param {string} filename + */ +function isPublicModule(rootPath, filename) { + const isPrivate = /^__/.test(filename); + if (isPrivate) { + return false; + } + if (isDirectory(join(rootPath, filename))) { + return true; + } + return /\.(j|t)sx?$/.test(filename); +} + +/** + * Returns { index: "path/to/index", moduleName: "path/to/moduleName" } + * @param {string} rootPath + * @param {string} prefix + */ +function getPublicFiles(rootPath, prefix = "") { + return readdirSync(rootPath) + .filter(filename => isPublicModule(rootPath, filename)) + .sort() // Ensure consistent order across platforms + .reduce((acc, filename) => { + const path = join(rootPath, filename); + const childFiles = + isDirectory(path) && getPublicFiles(path, join(prefix, filename)); + return { + ...(childFiles || { + [removeExt(normalizePath(join(prefix, filename)))]: normalizePath( + path, + ), + }), + ...acc, + }; + }, {}); +} + +/** + * Returns the same as getPublicFiles, but grouped by modules. + * Like { "path/to/moduleName": ["path/to/moduleName/file1", "path/to/moduleName/file2"] } + * @param {string} rootPath + */ +function getPublicFilesByModules(rootPath) { + const publicFiles = getPublicFiles(rootPath); + return Object.values(publicFiles).reduce((acc, path) => { + const moduleName = dirname(path); + acc[moduleName] = [...(acc[moduleName] || []), path]; + return acc; + }, {}); +} + +/** + * @param {string} rootPath + */ +function hasTSConfig(rootPath) { + return existsSync(join(rootPath, "tsconfig.json")); +} + +/** + * @param {import("ts-morph").Node} node + */ +function getEscapedName(node) { + const symbol = node.getSymbol(); + return symbol && symbol.getEscapedName(); +} + +/** + * @param {import("ts-morph").Node} node + */ +function isStateReturnDeclaration(node) { + const kindName = node.getKindName(); + const escapedName = getEscapedName(node); + return ( + kindName === "TypeAliasDeclaration" && /.+StateReturn$/.test(escapedName) + ); +} + +/** + * @param {import("ts-morph").Node} node + */ +function isOptionsDeclaration(node) { + const kindName = node.getKindName(); + const escapedName = getEscapedName(node); + return kindName === "TypeAliasDeclaration" && /.+Options$/.test(escapedName); +} + +/** + * @param {import("ts-morph").Node} node + */ +function getModuleName(node) { + return getEscapedName(node) + .replace("unstable_", "") + .replace(/^(.+)InitialState$/, "use$1State") + .replace(/^(.+)StateReturn$/, "$1State") + .replace("Options", ""); +} + +/** + * @param {import("ts-morph").Symbol} symbol + */ +function getDeclaration(symbol) { + const declarations = symbol.getDeclarations(); + return declarations[0]; +} + +/** + * @param {import("ts-morph").Symbol} symbol + */ +function getJsDocs(symbol) { + const jsDocs = getDeclaration(symbol).getJsDocs(); + return jsDocs[jsDocs.length - 1]; +} + +/** + * @param {import("ts-morph").Symbol} prop + * @returns {string[]} + */ +function getTagNames(prop) { + const jsDocs = getJsDocs(prop); + if (!jsDocs) return []; + // Object.getOwnPropertyNames(Object.getPrototypeOf(jsDocs)); + return jsDocs.getTags().map(tag => tag.getKindName()); +} + +/** + * @param {import("ts-morph").Node} node + * @param {boolean} includePrivate + */ +function getProps(node, includePrivate) { + const props = node.getType().getProperties(); + if (includePrivate) { + return props; + } + return props.filter(prop => !getTagNames(prop).includes("JSDocPrivateTag")); +} + +/** + * @param {import("ts-morph").Node} node + * @param {boolean} includePrivate + */ +function getPropsNames(node, includePrivate) { + return getProps(node, includePrivate).map(prop => prop.getEscapedName()); +} + +/** + * @param {import("ts-morph").SourceFile[]} sourceFiles + */ +function sortSourceFiles(sourceFiles) { + return sourceFiles.sort((a, b) => { + const aName = a.getBaseNameWithoutExtension(); + const bName = b.getBaseNameWithoutExtension(); + if (/State/.test(aName)) return -1; + if (/State/.test(bName) || aName > bName) return 1; + if (aName < bName) return -1; + return 0; + }); +} +/** + * @param {import("ts-morph").Node} node + * @return {import("ts-morph").Node|null} + */ +function getLiteralNode(node) { + if (node.getKindName() === "TypeLiteral") { + return node; + } + const children = node.getChildren(); + for (const child of children) { + const result = getLiteralNode(child); + if (result) { + return result; + } + } + return null; +} + +/** + * @param {string} moduleName + */ +function getKeysName(moduleName) { + return `${toUpper(snakeCase(moduleName))}_KEYS`; +} + +/** + * @param {any[]} a + * @param {any[]} b + */ +function isSubsetOf(a, b) { + return a.length && b.length && a.every(item => b.includes(item)); +} + +/** + * @param {Object} object + */ +function sortStateSets(object) { + return Object.entries(object) + .sort(([aKey, aValue], [bKey, bValue]) => { + if (aKey.endsWith("State") && bKey.endsWith("State")) { + if (isSubsetOf(aValue, bValue)) return -1; + if (isSubsetOf(bValue, aValue)) return 1; + } + return 0; + }) + .reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}); +} + +/** + * @param {Object} object + */ +function replaceSubsetInObject(object) { + const finalObj = {}; + Object.entries(object).forEach(([key, array]) => { + const refs = Object.entries(finalObj) + .filter(([, items]) => isSubsetOf(items, array)) + .map(([k]) => k); + + finalObj[key] = [ + ...refs.map(ref => `...${getKeysName(ref)}`), + ...array.filter(item => !refs.some(ref => object[ref].includes(item))), + ]; + }); + if (!isEqual(object, finalObj)) { + return replaceSubsetInObject(finalObj); + } + return finalObj; +} + +/** + * @param {string} acc + * @param {[string, string[]]} entry + */ +function reduceKeys(acc, [moduleName, array]) { + const declaration = `const ${getKeysName(moduleName)}`; + const value = `${JSON.stringify(array)} as const` + // "...FOO_KEYS" -> ...FOO_KEYS (without quotes) + .replace(/"([.A-Z_]+)"/g, "$1") + // [...FOO_KEYS] as const -> FOO_KEYS + .replace(/\[\.\.\.([A-Z_]+)\] as const/g, "$1"); + + const finalString = `${declaration} = ${value};\n`; + + if (!moduleName.endsWith("State")) { + return `${acc}export ${finalString}`; + } + return `${acc}${finalString}`; +} + +/** + * Create __keys.json files + * @param {string} rootPath + */ +function makeKeys(rootPath) { + const pkg = getPackage(rootPath); + + const filesByModules = getPublicFilesByModules(getSourcePath(rootPath)); + const project = new Project({ + tsConfigFilePath: join(rootPath, "tsconfig.json"), + addFilesFromTsConfig: false, + }); + const created = []; + + console.log(filesByModules); + Object.entries(filesByModules).forEach(([modulePath, paths]) => { + const sourceFiles = project.addSourceFilesAtPaths(paths); + const keys = {}; + const stateKeys = []; + + sortSourceFiles(sourceFiles).forEach(sourceFile => { + sourceFile.forEachChild(node => { + if (isStateReturnDeclaration(node) || isOptionsDeclaration(node)) { + const literalNode = isOptionsDeclaration(node) + ? getLiteralNode(node) + : node; + const props = literalNode ? getPropsNames(literalNode, true) : []; + if (isStateReturnDeclaration(node)) { + for (const prop of props) { + if (!stateKeys.includes(prop)) { + stateKeys.push(prop); + } + } + keys[getModuleName(node)] = props; + } else { + keys[getModuleName(node)] = [...stateKeys, ...props]; + } + } + }); + }); + + if (!Object.keys(keys).length) return; + + const normalizedKeys = replaceSubsetInObject(sortStateSets(keys)); + const contents = Object.entries(normalizedKeys).reduce(reduceKeys, ""); + created.push(chalk.bold(chalk.green(basename(modulePath)))); + + writeFileSync( + join(modulePath, "__keys.ts"), + prettier.format(`// Automatically generated\n${contents}`, { + parser: "babel-ts", + }), + ); + }); + + if (created.length) { + log( + [ + "", + `Generated keys in ${chalk.bold(pkg.name)}:`, + `${created.join(", ")}`, + ].join("\n"), + ); + } +} + +module.exports = { + hasTSConfig, + makeKeys, +}; From de953162a0128d533ff1749d0132aac2727af46c Mon Sep 17 00:00:00 2001 From: Anurag Date: Wed, 16 Sep 2020 12:12:35 +0530 Subject: [PATCH 2/4] chore: fix path & remove console log --- package.json | 1 + scripts/build/keys.js | 2 +- scripts/utils.js | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 94b4a444e..65244be2c 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "build-storybook": "build-storybook", "commit": "gacp", "format": "prettier --write \"./**/*.{js,ts,css,less,json,md,html,yml,yaml,pcss,jsx,tsx}\"", + "keys": "node scripts/build/keys", "lint": "eslint . --ext .tsx,.ts,.jsx,.js --fix", "lint:package": "sort-package-json", "storybook": "start-storybook -p 6006", diff --git a/scripts/build/keys.js b/scripts/build/keys.js index 072836426..47ac4287b 100644 --- a/scripts/build/keys.js +++ b/scripts/build/keys.js @@ -1,5 +1,5 @@ #!/usr/bin/env node -const { hasTSConfig, makeKeys } = require("./utils"); +const { hasTSConfig, makeKeys } = require("../utils"); const cwd = process.cwd(); diff --git a/scripts/utils.js b/scripts/utils.js index 433fb8201..53a571358 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -325,7 +325,6 @@ function makeKeys(rootPath) { }); const created = []; - console.log(filesByModules); Object.entries(filesByModules).forEach(([modulePath, paths]) => { const sourceFiles = project.addSourceFilesAtPaths(paths); const keys = {}; From 9fdfe9ec425cde1b3cd7307ffcf46101bbf35cd1 Mon Sep 17 00:00:00 2001 From: Anurag Date: Wed, 16 Sep 2020 12:20:30 +0530 Subject: [PATCH 3/4] chore: generated keys --- src/accordion/__keys.ts | 23 +++++++------ src/breadcrumbs/BreadcrumbLink.ts | 3 +- src/breadcrumbs/__keys.ts | 3 ++ src/button/__keys.ts | 3 ++ src/link/Link.ts | 4 ++- src/link/__keys.ts | 3 ++ src/meter/Meter.ts | 2 +- src/meter/__key.ts | 12 ------- src/meter/__keys.ts | 12 +++++++ src/number-input/NumberInput.ts | 4 +-- src/number-input/__keys.ts | 32 +++++++++---------- .../createNumberInputButtonsHook.ts | 4 +-- src/pagination/__keys.ts | 16 +++++----- src/progress/__keys.ts | 7 ++-- src/select/SelectInput.ts | 4 +-- src/select/SelectMenu.ts | 4 +-- src/select/SelectOption.ts | 4 +-- src/select/SelectTrigger.ts | 4 +-- src/select/__keys.ts | 31 +++++++----------- src/slider/__keys.ts | 15 +++++---- src/toast/__keys.ts | 10 ++++++ 21 files changed, 109 insertions(+), 91 deletions(-) create mode 100644 src/breadcrumbs/__keys.ts create mode 100644 src/button/__keys.ts create mode 100644 src/link/__keys.ts delete mode 100644 src/meter/__key.ts create mode 100644 src/meter/__keys.ts create mode 100644 src/toast/__keys.ts diff --git a/src/accordion/__keys.ts b/src/accordion/__keys.ts index 656ebdd28..165021458 100644 --- a/src/accordion/__keys.ts +++ b/src/accordion/__keys.ts @@ -1,27 +1,26 @@ -export const ACCORDION_STATE_KEYS = [ +// Automatically generated +const ACCORDION_STATE_KEYS = [ "baseId", "unstable_idCountRef", "setBaseId", + "allowMultiple", + "loop", + "allowToggle", + "defaultActiveId", + "manual", "items", - "registerItem", + "activeItems", "buttons", + "registerItem", "registerButton", - "panels", "registerPanel", - "activeItems", "addActiveItem", "removeActiveItem", - "allowMultiple", - "allowToggle", - "defaultActiveId", - "manual", - "loop", "next", "prev", "first", "last", ] as const; - export const ACCORDION_ITEM_KEYS = ACCORDION_STATE_KEYS; -export const ACCORDION_TRIGGER_KEYS = ACCORDION_STATE_KEYS; -export const ACCORDION_PANEL_KEYS = ACCORDION_STATE_KEYS; +export const ACCORDION_PANEL_KEYS = ACCORDION_ITEM_KEYS; +export const ACCORDION_TRIGGER_KEYS = ACCORDION_PANEL_KEYS; diff --git a/src/breadcrumbs/BreadcrumbLink.ts b/src/breadcrumbs/BreadcrumbLink.ts index 062a3e518..c21eb7b90 100644 --- a/src/breadcrumbs/BreadcrumbLink.ts +++ b/src/breadcrumbs/BreadcrumbLink.ts @@ -1,6 +1,7 @@ import { createComponent, createHook } from "reakit-system"; import { LinkHTMLProps, LinkOptions, useLink } from "../link"; +import { BREADCRUMB_LINK_KEYS } from "./__keys"; export type BreadcrumbLinkOptions = LinkOptions & { isCurrent?: boolean; @@ -17,7 +18,7 @@ export const useBreadcrumbLink = createHook< >({ name: "BreadcrumbLink", compose: useLink, - keys: ["isCurrent"], + keys: BREADCRUMB_LINK_KEYS, useOptions(options) { return { disabled: options.disabled || options.isCurrent, ...options }; diff --git a/src/breadcrumbs/__keys.ts b/src/breadcrumbs/__keys.ts new file mode 100644 index 000000000..a554edec2 --- /dev/null +++ b/src/breadcrumbs/__keys.ts @@ -0,0 +1,3 @@ +// Automatically generated +export const BREADCRUMB_LINK_KEYS = ["isCurrent"] as const; +export const BREADCRUMBS_KEYS = [] as const; diff --git a/src/button/__keys.ts b/src/button/__keys.ts new file mode 100644 index 000000000..6a1a165fc --- /dev/null +++ b/src/button/__keys.ts @@ -0,0 +1,3 @@ +// Automatically generated +export const ARIA_BUTTON_KEYS = [] as const; +export const ARIA_TOGGLE_BUTTON_KEYS = [] as const; diff --git a/src/link/Link.ts b/src/link/Link.ts index 7583b88fb..fdf94df06 100644 --- a/src/link/Link.ts +++ b/src/link/Link.ts @@ -4,6 +4,8 @@ import { useWarning } from "reakit-warning"; import { createComponent, createHook } from "reakit-system"; import { ClickableHTMLProps, ClickableOptions, useClickable } from "reakit"; +import { LINK_KEYS } from "./__keys"; + export type LinkOptions = ClickableOptions & { isExternal?: boolean; }; @@ -15,7 +17,7 @@ export type LinkProps = LinkOptions & LinkHTMLProps; export const useLink = createHook({ name: "Link", compose: useClickable, - keys: ["isExternal"], + keys: LINK_KEYS, useOptions(options) { return { unstable_clickOnSpace: false, ...options }; diff --git a/src/link/__keys.ts b/src/link/__keys.ts new file mode 100644 index 000000000..f195408d0 --- /dev/null +++ b/src/link/__keys.ts @@ -0,0 +1,3 @@ +// Automatically generated +export const ARIA_LINK_KEYS = [] as const; +export const LINK_KEYS = ["isExternal"] as const; diff --git a/src/meter/Meter.ts b/src/meter/Meter.ts index 357980813..0652880a2 100644 --- a/src/meter/Meter.ts +++ b/src/meter/Meter.ts @@ -1,7 +1,7 @@ import { BoxHTMLProps, BoxOptions, useBox } from "reakit"; import { createHook, createComponent } from "reakit-system"; -import { METER_KEYS } from "./__key"; +import { METER_KEYS } from "./__keys"; import { MeterStateReturn } from "./MeterState"; import { isFunction } from "@chakra-ui/utils"; diff --git a/src/meter/__key.ts b/src/meter/__key.ts deleted file mode 100644 index db4cf822d..000000000 --- a/src/meter/__key.ts +++ /dev/null @@ -1,12 +0,0 @@ -const METER_STATE_KEYS = [ - "value", - "min", - "max", - "percent", - "low", - "high", - "optimum", - "status", -]; - -export const METER_KEYS = METER_STATE_KEYS; diff --git a/src/meter/__keys.ts b/src/meter/__keys.ts new file mode 100644 index 000000000..6586b081b --- /dev/null +++ b/src/meter/__keys.ts @@ -0,0 +1,12 @@ +// Automatically generated +const METER_STATE_KEYS = [ + "value", + "low", + "high", + "optimum", + "min", + "max", + "status", + "percent", +] as const; +export const METER_KEYS = [...METER_STATE_KEYS, "getAriaValueText"] as const; diff --git a/src/number-input/NumberInput.ts b/src/number-input/NumberInput.ts index 8bc17d7a9..2e2c66558 100644 --- a/src/number-input/NumberInput.ts +++ b/src/number-input/NumberInput.ts @@ -19,7 +19,7 @@ import { useCallback, } from "react"; -import { NUMBERINPUT_KEYS } from "./__keys"; +import { NUMBER_INPUT_KEYS } from "./__keys"; import { NumberInputStateReturn } from "./NumberInputState"; import { isFloatingPointNumericCharacter, @@ -68,7 +68,7 @@ export const useNumberInput = createHook< >({ name: "NumberInput", compose: useInput, - keys: NUMBERINPUT_KEYS, + keys: NUMBER_INPUT_KEYS, useProps(options, htmlProps) { const { diff --git a/src/number-input/__keys.ts b/src/number-input/__keys.ts index 2489d8197..ae742c9b0 100644 --- a/src/number-input/__keys.ts +++ b/src/number-input/__keys.ts @@ -1,4 +1,14 @@ -const NUMBERINPUT_STATE_KEYS = [ +// Automatically generated +const NUMBER_INPUT_STATE_KEYS = [ + "spinner", + "isOutOfRange", + "precision", + "value", + "valueAsNumber", + "update", + "reset", + "clamp", + "cast", "keepWithinRange", "clampValueOnBlur", "min", @@ -11,23 +21,13 @@ const NUMBERINPUT_STATE_KEYS = [ "focusInput", "isFocused", "setFocused", - "value", - "valueAsNumber", - "isAtMax", - "isAtMin", - "isOutOfRange", - "precision", "increment", "decrement", - "update", - "reset", - "cast", - "clamp", - "spinner", + "isAtMin", + "isAtMax", ] as const; - -export const NUMBERINPUT_KEYS = [ - ...NUMBERINPUT_STATE_KEYS, +export const NUMBER_INPUT_KEYS = [ + ...NUMBER_INPUT_STATE_KEYS, "getAriaValueText", ] as const; -export const NUMBERINPUT_BUTTON_KEYS = NUMBERINPUT_STATE_KEYS; +export const NUMBER_INPUT_BUTTON_KEYS = NUMBER_INPUT_STATE_KEYS; diff --git a/src/number-input/createNumberInputButtonsHook.ts b/src/number-input/createNumberInputButtonsHook.ts index d3fbc1ddc..dc9ffdbbc 100644 --- a/src/number-input/createNumberInputButtonsHook.ts +++ b/src/number-input/createNumberInputButtonsHook.ts @@ -2,7 +2,7 @@ import { createHook } from "reakit-system"; import { ButtonHTMLProps, ButtonOptions, useButton } from "reakit/Button"; import { useSpinButton } from "./__utils"; -import { NUMBERINPUT_BUTTON_KEYS } from "./__keys"; +import { NUMBER_INPUT_BUTTON_KEYS } from "./__keys"; import { NumberInputStateReturn } from "./NumberInputState"; export type NumberInputButtonOptions = ButtonOptions & @@ -23,7 +23,7 @@ export const createNumberInputButtonsHook = ( return createHook({ name: `NumberInput`, compose: useButton, - keys: NUMBERINPUT_BUTTON_KEYS, + keys: NUMBER_INPUT_BUTTON_KEYS, useProps(options, htmlProps) { return useSpinButton(options, htmlProps, type); diff --git a/src/pagination/__keys.ts b/src/pagination/__keys.ts index 68bc36797..e98625564 100644 --- a/src/pagination/__keys.ts +++ b/src/pagination/__keys.ts @@ -1,18 +1,18 @@ -export const PAGINATION_STATE_KEYS = [ +// Automatically generated +const PAGINATION_STATE_KEYS = [ + "pages", + "currentPage", + "isAtMax", + "isAtMin", "next", "prev", "move", "first", "last", - "pages", - "currentPage", - "isAtMin", - "isAtMax", ] as const; - export const PAGINATION_KEYS = PAGINATION_STATE_KEYS; export const PAGINATION_BUTTON_KEYS = [ - ...PAGINATION_STATE_KEYS, + ...PAGINATION_KEYS, "goto", "getAriaLabel", -]; +] as const; diff --git a/src/progress/__keys.ts b/src/progress/__keys.ts index 9b7ca2060..cfe0671a8 100644 --- a/src/progress/__keys.ts +++ b/src/progress/__keys.ts @@ -1,3 +1,4 @@ +// Automatically generated const PROGRESS_STATE_KEYS = [ "value", "isIndeterminate", @@ -5,5 +6,7 @@ const PROGRESS_STATE_KEYS = [ "max", "percent", ] as const; - -export const PROGRESS_KEYS = [...PROGRESS_STATE_KEYS, "getAriaValueText"]; +export const PROGRESS_KEYS = [ + ...PROGRESS_STATE_KEYS, + "getAriaValueText", +] as const; diff --git a/src/select/SelectInput.ts b/src/select/SelectInput.ts index 0eb85b3f7..fc5f6af3c 100644 --- a/src/select/SelectInput.ts +++ b/src/select/SelectInput.ts @@ -10,7 +10,7 @@ import { PopoverDisclosureOptions, } from "reakit"; -import { SELECT_KEYS } from "./__keys"; +import { SELECT_INPUT_KEYS } from "./__keys"; import { SelectStateReturn } from "./SelectState"; export type SelectInputOptions = CompositeOptions & @@ -30,7 +30,7 @@ export type SelectInputOptions = CompositeOptions & const useSelectInput = createHook({ name: "SelectInput", compose: [useComposite, usePopoverDisclosure], - keys: SELECT_KEYS, + keys: SELECT_INPUT_KEYS, useProps( { diff --git a/src/select/SelectMenu.ts b/src/select/SelectMenu.ts index ca0babbef..4edae4a65 100644 --- a/src/select/SelectMenu.ts +++ b/src/select/SelectMenu.ts @@ -9,7 +9,7 @@ import { CompositeHTMLProps, } from "reakit/Composite"; -import { SELECT_KEYS } from "./__keys"; +import { SELECT_MENU_KEYS } from "./__keys"; import { usePortalShortcut } from "./__utils"; import { SelectStateReturn } from "./SelectState"; @@ -31,7 +31,7 @@ export type SelectMenuHTMLProps = CompositeHTMLProps & PopoverHTMLProps; const useSelectMenu = createHook({ name: "SelectMenu", compose: [useComposite, usePopover], - keys: SELECT_KEYS, + keys: SELECT_MENU_KEYS, useProps( { visible, move, values, currentId, selected, isCombobox }, diff --git a/src/select/SelectOption.ts b/src/select/SelectOption.ts index 5d3b02104..df802f696 100644 --- a/src/select/SelectOption.ts +++ b/src/select/SelectOption.ts @@ -7,7 +7,7 @@ import { CompositeItemOptions, } from "reakit/Composite"; -import { SELECT_KEYS } from "./__keys"; +import { SELECT_ITEM_KEYS } from "./__keys"; import { SelectStateReturn } from "./SelectState"; export type SelectItemOptions = CompositeItemOptions & @@ -20,7 +20,7 @@ export type SelectItemHTMLProp = CompositeItemHTMLProps; const useSelectOption = createHook({ name: "SelectOption", compose: useCompositeItem, - keys: ["value", ...SELECT_KEYS], + keys: SELECT_ITEM_KEYS, useProps( { selected, value, setSelected, disabled }, { onClick: htmlOnClick, onMouseMove: htmlOnMouseMove, ...htmlProps }, diff --git a/src/select/SelectTrigger.ts b/src/select/SelectTrigger.ts index 1ab2358d3..843200c3a 100644 --- a/src/select/SelectTrigger.ts +++ b/src/select/SelectTrigger.ts @@ -10,7 +10,7 @@ import { } from "reakit/Popover"; import { SelectStateReturn } from "./SelectState"; -import { SELECT_KEYS } from "./__keys"; +import { SELECT_TRIGGER_KEYS } from "./__keys"; export type SelectTriggerOptions = PopoverDisclosureOptions & Pick< @@ -31,7 +31,7 @@ const useSelectTrigger = createHook< >({ name: "SelectTrigger", compose: [usePopoverDisclosure], - keys: SELECT_KEYS, + keys: SELECT_TRIGGER_KEYS, useProps(options, { ref: htmlRef, onKeyDown: htmlOnKeyDown, ...htmlProps }) { const ref = React.useRef(null); diff --git a/src/select/__keys.ts b/src/select/__keys.ts index beb4c964b..3ed869fb2 100644 --- a/src/select/__keys.ts +++ b/src/select/__keys.ts @@ -1,4 +1,5 @@ -const COMPOSITE_STATE_KEYS = [ +// Automatically generated +const SELECT_STATE_KEYS = [ "baseId", "unstable_idCountRef", "setBaseId", @@ -33,15 +34,9 @@ const COMPOSITE_STATE_KEYS = [ "setWrap", "reset", "unstable_setHasActiveWidget", -] as const; - -const POPOVER_STATE_KEYS = [ - "baseId", - "unstable_idCountRef", "visible", "animated", "animating", - "setBaseId", "show", "hide", "toggle", @@ -60,20 +55,18 @@ const POPOVER_STATE_KEYS = [ "unstable_update", "placement", "place", -] as const; - -export const SELECT_STATE_KEYS = [ - ...COMPOSITE_STATE_KEYS, - ...POPOVER_STATE_KEYS, - "isPlaceholder", - "isCombobox", "allowMultiselect", "selected", - "setSelected", - "removeSelected", + "isPlaceholder", "inputValue", - "setInputValue", + "isCombobox", "values", + "setInputValue", + "removeSelected", + "setSelected", ] as const; - -export const SELECT_KEYS = SELECT_STATE_KEYS; +export const SELECT_KEYS = [...SELECT_STATE_KEYS, "onChange"] as const; +export const SELECT_INPUT_KEYS = SELECT_STATE_KEYS; +export const SELECT_MENU_KEYS = SELECT_INPUT_KEYS; +export const SELECT_ITEM_KEYS = [...SELECT_MENU_KEYS, "value"] as const; +export const SELECT_TRIGGER_KEYS = SELECT_MENU_KEYS; diff --git a/src/slider/__keys.ts b/src/slider/__keys.ts index 861737b2f..364ab2307 100644 --- a/src/slider/__keys.ts +++ b/src/slider/__keys.ts @@ -1,16 +1,17 @@ -export const SLIDER_STATE_KEYS = [ +// Automatically generated +const SLIDER_STATE_KEYS = [ "actions", - "handlers", - "refs", "state", + "refs", + "handlers", "styles", ] as const; - export const SLIDER_KEYS = SLIDER_STATE_KEYS; -export const SLIDER_TRACK_KEYS = [...SLIDER_KEYS, "id"] as const; export const SLIDER_FILLED_TRACK_KEYS = SLIDER_KEYS; +export const SLIDER_INPUT_KEYS = SLIDER_FILLED_TRACK_KEYS; export const SLIDER_THUMB_KEYS = [ - ...SLIDER_TRACK_KEYS, + ...SLIDER_INPUT_KEYS, + "id", "getAriaValueText", ] as const; -export const SLIDER_INPUT_KEYS = SLIDER_KEYS; +export const SLIDER_TRACK_KEYS = [...SLIDER_INPUT_KEYS, "id"] as const; diff --git a/src/toast/__keys.ts b/src/toast/__keys.ts new file mode 100644 index 000000000..333a5284f --- /dev/null +++ b/src/toast/__keys.ts @@ -0,0 +1,10 @@ +// Automatically generated +const TOAST_STATE_KEYS = [ + "setToasts", + "getToastToRender", + "toasts", + "toggle", + "show", + "hide", + "remove", +] as const; From bcb3a8d5560b060bd07db6d0d5686b52a55a5701 Mon Sep 17 00:00:00 2001 From: Anurag Date: Wed, 16 Sep 2020 12:22:48 +0530 Subject: [PATCH 4/4] chore: added reference comment --- scripts/utils.js | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/scripts/utils.js b/scripts/utils.js index 53a571358..8def29b71 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -1,3 +1,4 @@ +// Taken from: https://github.com/reakit/reakit/blob/6f6d1ad9177156cafa4adf9c987a43c79a1bbb90/scripts/build/utils.js const { join, dirname, basename } = require("path"); const { toUpper, snakeCase, isEqual } = require("lodash"); const prettier = require("prettier"); @@ -14,17 +15,6 @@ function log(...args) { console.log(...args); } -/** - * Converts ./path/to/file.js to ./path/to - * @param {string} dir - */ -function resolveDir(dir) { - if (!/\.(t|j)s$/.test(dir)) { - return dir; - } - return dirname(dir); -} - /** * @param {string} rootPath */