@nyxon/nasim-mcp 0.0.0-stage → 0.14.0
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/LICENSE +21 -0
- package/README.md +69 -2
- package/lib/bin.d.ts +2 -0
- package/lib/bin.js +9 -0
- package/lib/check.d.ts +10 -0
- package/lib/check.js +319 -0
- package/lib/find.d.ts +15 -0
- package/lib/find.js +86 -0
- package/lib/manifest.d.ts +76 -0
- package/lib/manifest.js +5 -0
- package/lib/server.d.ts +4 -0
- package/lib/server.js +198 -0
- package/lib/text.d.ts +8 -0
- package/lib/text.js +45 -0
- package/manifest.json +14143 -0
- package/package.json +38 -4
- package/skill/SKILL.md +133 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 NYXON
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,70 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @nyxon/nasim-mcp
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An [MCP](https://modelcontextprotocol.io) server for [Nasim](https://nasim.nyxon.tech)
|
|
4
|
+
(`@nyxon/nasim`), the React UI library for right-to-left, Persian-first dashboards on HeroUI 3 and
|
|
5
|
+
React Aria. It lets a coding agent choose the right part for a job, read its page (examples' code,
|
|
6
|
+
props table, rules, accessibility), and check a screen it wrote against Nasim's rules.
|
|
7
|
+
|
|
8
|
+
It answers from the documentation bundled into this package when it was built, for the version it
|
|
9
|
+
names: no network request, no file read outside the package, nothing written. It is published
|
|
10
|
+
with each release of `@nyxon/nasim` after 0.12.0, under the same version number, and answers for
|
|
11
|
+
that version only: install the one that matches your `@nyxon/nasim`. There is none for 0.12.0 or
|
|
12
|
+
earlier.
|
|
13
|
+
|
|
14
|
+
## Add it to your agent
|
|
15
|
+
|
|
16
|
+
Claude Code:
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
claude mcp add nasim -- npx -y @nyxon/nasim-mcp
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Cursor (`.cursor/mcp.json`), Claude Code (`.mcp.json`) and other clients that take this shape:
|
|
23
|
+
|
|
24
|
+
```json
|
|
25
|
+
{ "mcpServers": { "nasim": { "command": "npx", "args": ["-y", "@nyxon/nasim-mcp"] } } }
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
VS Code (`.vscode/mcp.json`):
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{ "servers": { "nasim": { "type": "stdio", "command": "npx", "args": ["-y", "@nyxon/nasim-mcp"] } } }
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`bunx @nyxon/nasim-mcp` works in place of `npx -y` too. It needs Node 20 or later.
|
|
35
|
+
|
|
36
|
+
## Tools
|
|
37
|
+
|
|
38
|
+
| Tool | What it does |
|
|
39
|
+
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
40
|
+
| `list_parts` | Every documented part: kind, Server Component or not, group, one line, its job and import line; by group or kind. Names the exports with no page. |
|
|
41
|
+
| `get_part` | A part's whole page as Markdown (examples' code, props table, rules, accessibility), or one section of it. |
|
|
42
|
+
| `search_docs` | Parts and guides ranked by words, English or Persian, with where each word matched. |
|
|
43
|
+
| `find_part` | Which part for a job («a status on each row», «confirm before deleting»), with the import line and why. Plain word matching, no model. |
|
|
44
|
+
| `get_guide` | A guide: installation, setup, rules, agents. |
|
|
45
|
+
| `get_rules` | The rules every screen keeps, in a few lines. |
|
|
46
|
+
| `check_code` | A file checked against the rules, each breach with its line and fix: HeroUI or React Aria imports, unknown names or props, hex or `dark:` classes, words in JSX. |
|
|
47
|
+
|
|
48
|
+
`check_code` reports only what the text shows for sure; it does not check layout, logic or
|
|
49
|
+
accessibility. A part without a page yet is named by `list_parts`, but this server has no props or
|
|
50
|
+
examples for it.
|
|
51
|
+
|
|
52
|
+
## A skill for Claude Code
|
|
53
|
+
|
|
54
|
+
The package carries a skill made from the same docs: the rules, the table of parts by job, and where
|
|
55
|
+
to read more. Put it where Claude Code finds it:
|
|
56
|
+
|
|
57
|
+
```sh
|
|
58
|
+
mkdir -p .claude/skills/nasim && npx -y @nyxon/nasim-mcp skill > .claude/skills/nasim/SKILL.md
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
In PowerShell, write it as UTF-8: `npx -y @nyxon/nasim-mcp skill | Set-Content -Encoding utf8 .claude/skills/nasim/SKILL.md`.
|
|
62
|
+
|
|
63
|
+
## Without MCP
|
|
64
|
+
|
|
65
|
+
<https://nasim.nyxon.tech/llms.txt> lists every page as Markdown, and
|
|
66
|
+
<https://nasim.nyxon.tech/llms-full.txt> is all of them in one file.
|
|
67
|
+
|
|
68
|
+
## Licence
|
|
69
|
+
|
|
70
|
+
MIT, as `@nyxon/nasim`.
|
package/lib/bin.d.ts
ADDED
package/lib/bin.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
4
|
+
import { createServer } from "./server.js";
|
|
5
|
+
// `nasim-mcp skill` prints the skill of this version, for `> .claude/skills/nasim/SKILL.md`.
|
|
6
|
+
if (process.argv[2] === 'skill')
|
|
7
|
+
process.stdout.write(readFileSync(new URL('../skill/SKILL.md', import.meta.url), 'utf8'));
|
|
8
|
+
else
|
|
9
|
+
await createServer().connect(new StdioServerTransport());
|
package/lib/check.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Manifest } from './manifest.ts';
|
|
2
|
+
export type Rule = 'import' | 'name' | 'prop' | 'colour' | 'copy' | 'parse';
|
|
3
|
+
export interface Finding {
|
|
4
|
+
line: number;
|
|
5
|
+
rule: Rule;
|
|
6
|
+
message: string;
|
|
7
|
+
fix: string;
|
|
8
|
+
}
|
|
9
|
+
/** What in `code` breaks Nasim's rules, by line. Only what can be told from the text for sure. */
|
|
10
|
+
export declare function checkCode(manifest: Manifest, code: string, path?: string): Finding[];
|
package/lib/check.js
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import { parse } from '@babel/parser';
|
|
2
|
+
import { nearest } from "./text.js";
|
|
3
|
+
const PACKAGE = '@nyxon/nasim';
|
|
4
|
+
// The modules a screen never imports (root tools/check.ts enforces the same in this workspace).
|
|
5
|
+
const BEHIND_NASIM = /^(?:@heroui\/|react-aria-components$|react-aria(?:\/|$)|@react-aria\/|@react-stately\/)/;
|
|
6
|
+
// Attributes whose value a person reads or hears. Anything else (`id`, `href`, `variant`) is data.
|
|
7
|
+
const USER_FACING = new Set([
|
|
8
|
+
'aria-label',
|
|
9
|
+
'aria-description',
|
|
10
|
+
'aria-placeholder',
|
|
11
|
+
'aria-roledescription',
|
|
12
|
+
'aria-valuetext',
|
|
13
|
+
'placeholder',
|
|
14
|
+
'title',
|
|
15
|
+
'alt',
|
|
16
|
+
'label',
|
|
17
|
+
'description',
|
|
18
|
+
]);
|
|
19
|
+
const USER_FACING_SUFFIX = /[a-z](?:Label|Text|Title|Message|Description|Placeholder|Heading)$/;
|
|
20
|
+
// Elements whose text is code or markup, not copy.
|
|
21
|
+
const VERBATIM = new Set(['code', 'pre', 'kbd', 'samp', 'style', 'script']);
|
|
22
|
+
// Functions whose string arguments are class names.
|
|
23
|
+
const CLASS_FUNCTIONS = new Set([
|
|
24
|
+
'tv',
|
|
25
|
+
'cn',
|
|
26
|
+
'cx',
|
|
27
|
+
'clsx',
|
|
28
|
+
'twMerge',
|
|
29
|
+
'twJoin',
|
|
30
|
+
]);
|
|
31
|
+
const HAS_LETTER = /\p{L}/u;
|
|
32
|
+
const DARK = /(?<![\w-])dark:/;
|
|
33
|
+
const HEX = /#[0-9a-fA-F]{3,8}(?![0-9a-zA-Z])/;
|
|
34
|
+
const SKIP_KEYS = new Set([
|
|
35
|
+
'loc',
|
|
36
|
+
'start',
|
|
37
|
+
'end',
|
|
38
|
+
'extra',
|
|
39
|
+
'leadingComments',
|
|
40
|
+
'trailingComments',
|
|
41
|
+
'innerComments',
|
|
42
|
+
]);
|
|
43
|
+
function isNode(value) {
|
|
44
|
+
return (typeof value === 'object' &&
|
|
45
|
+
value !== null &&
|
|
46
|
+
typeof value.type === 'string');
|
|
47
|
+
}
|
|
48
|
+
function walk(node, visit) {
|
|
49
|
+
const parents = [];
|
|
50
|
+
const step = (current) => {
|
|
51
|
+
visit(current, parents);
|
|
52
|
+
parents.push(current);
|
|
53
|
+
for (const [key, value] of Object.entries(current)) {
|
|
54
|
+
if (SKIP_KEYS.has(key))
|
|
55
|
+
continue;
|
|
56
|
+
for (const child of Array.isArray(value) ? value : [value])
|
|
57
|
+
if (isNode(child))
|
|
58
|
+
step(child);
|
|
59
|
+
}
|
|
60
|
+
parents.pop();
|
|
61
|
+
};
|
|
62
|
+
step(node);
|
|
63
|
+
}
|
|
64
|
+
const lineOf = (node) => node.loc?.start.line ?? 0;
|
|
65
|
+
// The line of an offset inside a string node that may span lines.
|
|
66
|
+
const lineAt = (node, text, index) => lineOf(node) + (text.slice(0, index).match(/\n/g)?.length ?? 0);
|
|
67
|
+
const short = (text) => {
|
|
68
|
+
const flat = text.trim().replace(/\s+/g, ' ');
|
|
69
|
+
return flat.length > 40 ? `${flat.slice(0, 40)}…` : flat;
|
|
70
|
+
};
|
|
71
|
+
function plugins(path) {
|
|
72
|
+
if (path && /\.(?:c|m)?ts$/.test(path))
|
|
73
|
+
return ['typescript'];
|
|
74
|
+
if (path && /\.(?:c|m)?jsx?$/.test(path))
|
|
75
|
+
return ['jsx'];
|
|
76
|
+
return ['typescript', 'jsx'];
|
|
77
|
+
}
|
|
78
|
+
// The written text of a string node, or of a template without what its `${}` put in: the words
|
|
79
|
+
// that did not come from the strings (`حذف ${row.name}` → «حذف »).
|
|
80
|
+
function literalText(node) {
|
|
81
|
+
if (!node)
|
|
82
|
+
return;
|
|
83
|
+
if (node.type === 'StringLiteral')
|
|
84
|
+
return node.value;
|
|
85
|
+
if (node.type === 'TemplateLiteral')
|
|
86
|
+
return node.quasis.map((quasi) => quasi.value.cooked ?? '').join(' ');
|
|
87
|
+
if (node.type === 'JSXExpressionContainer')
|
|
88
|
+
return node.expression.type === 'JSXEmptyExpression'
|
|
89
|
+
? undefined
|
|
90
|
+
: literalText(node.expression);
|
|
91
|
+
}
|
|
92
|
+
function importName(node) {
|
|
93
|
+
if (node.type === 'ImportSpecifier')
|
|
94
|
+
return node.imported.type === 'Identifier'
|
|
95
|
+
? node.imported.name
|
|
96
|
+
: node.imported.value;
|
|
97
|
+
if (node.type === 'ImportDefaultSpecifier')
|
|
98
|
+
return 'default';
|
|
99
|
+
return '*';
|
|
100
|
+
}
|
|
101
|
+
// An import or re-export of UI that Nasim keeps behind itself. `*` and `default` name no part.
|
|
102
|
+
function boundaryFinding(line, from, names, exports) {
|
|
103
|
+
const named = names.filter((name) => name !== '*' && name !== 'default');
|
|
104
|
+
const inNasim = named.filter((name) => exports.has(name));
|
|
105
|
+
const missing = named.filter((name) => !exports.has(name));
|
|
106
|
+
return {
|
|
107
|
+
line,
|
|
108
|
+
rule: 'import',
|
|
109
|
+
message: `A screen imports UI from ${PACKAGE} only, never from ${from}.`,
|
|
110
|
+
fix: [
|
|
111
|
+
inNasim.length
|
|
112
|
+
? `import { ${inNasim.join(', ')} } from '${PACKAGE}';`
|
|
113
|
+
: '',
|
|
114
|
+
missing.length
|
|
115
|
+
? `${PACKAGE} has no ${missing.join(', ')}: use the part for the job (find_part), or add it to Nasim.`
|
|
116
|
+
: '',
|
|
117
|
+
]
|
|
118
|
+
.filter(Boolean)
|
|
119
|
+
.join(' ') ||
|
|
120
|
+
`Import each part by name from '${PACKAGE}' (list_parts shows them).`,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/** `export { X } from '…'` and `export * from '…'`: a re-export crosses the same boundary. */
|
|
124
|
+
function checkReExport(node, exports, findings) {
|
|
125
|
+
const from = node.source?.value;
|
|
126
|
+
if (!from || !BEHIND_NASIM.test(from))
|
|
127
|
+
return;
|
|
128
|
+
const names = node.type === 'ExportAllDeclaration'
|
|
129
|
+
? ['*']
|
|
130
|
+
: node.specifiers.map((specifier) => specifier.type === 'ExportSpecifier' ? specifier.local.name : '*');
|
|
131
|
+
findings.push(boundaryFinding(lineOf(node), from, names, exports));
|
|
132
|
+
}
|
|
133
|
+
function checkImport(node, manifest, exports, findings) {
|
|
134
|
+
const from = node.source.value;
|
|
135
|
+
const names = node.specifiers.map(importName);
|
|
136
|
+
if (BEHIND_NASIM.test(from) && names.length)
|
|
137
|
+
findings.push(boundaryFinding(lineOf(node), from, names, exports));
|
|
138
|
+
// A subpath entry (`@nyxon/nasim/list`) has exports of its own, which `exports` does not hold.
|
|
139
|
+
if (from !== PACKAGE)
|
|
140
|
+
return;
|
|
141
|
+
for (const specifier of node.specifiers) {
|
|
142
|
+
const name = importName(specifier);
|
|
143
|
+
if (specifier.type !== 'ImportSpecifier' || exports.has(name))
|
|
144
|
+
continue;
|
|
145
|
+
const elsewhere = manifest.parts.find((part) => part.entry && part.names.includes(name));
|
|
146
|
+
const close = nearest(name, manifest.exports);
|
|
147
|
+
findings.push({
|
|
148
|
+
line: lineOf(specifier),
|
|
149
|
+
rule: 'name',
|
|
150
|
+
message: `${PACKAGE} exports no ${name}.`,
|
|
151
|
+
fix: elsewhere
|
|
152
|
+
? `It is in ${PACKAGE}/${elsewhere.entry}: ${elsewhere.importLine}`
|
|
153
|
+
: close
|
|
154
|
+
? `Did you mean ${close}? Check it with get_part or list_parts.`
|
|
155
|
+
: 'list_parts shows what Nasim has; find_part finds the part for a job.',
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function checkProps(element, part, accepts, findings) {
|
|
160
|
+
for (const attribute of element.attributes) {
|
|
161
|
+
if (attribute.type !== 'JSXAttribute')
|
|
162
|
+
continue;
|
|
163
|
+
if (attribute.name.type !== 'JSXIdentifier')
|
|
164
|
+
continue;
|
|
165
|
+
const name = attribute.name.name;
|
|
166
|
+
// TypeScript lets any hyphenated attribute (`data-*`, `aria-*`) through; React adds key and ref.
|
|
167
|
+
if (name.includes('-') || name === 'key' || name === 'ref')
|
|
168
|
+
continue;
|
|
169
|
+
if (accepts.includes(name))
|
|
170
|
+
continue;
|
|
171
|
+
const close = nearest(name, accepts);
|
|
172
|
+
findings.push({
|
|
173
|
+
line: lineOf(attribute),
|
|
174
|
+
rule: 'prop',
|
|
175
|
+
message: `${part} has no prop \`${name}\`.`,
|
|
176
|
+
fix: close
|
|
177
|
+
? `Did you mean \`${close}\`? get_part ${part} lists its props.`
|
|
178
|
+
: `get_part ${part} lists its props.`,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function checkClasses(node, findings) {
|
|
183
|
+
walk(node, (inner) => {
|
|
184
|
+
const pieces = [];
|
|
185
|
+
if (inner.type === 'StringLiteral')
|
|
186
|
+
pieces.push({ text: inner.value, at: inner });
|
|
187
|
+
if (inner.type === 'TemplateElement')
|
|
188
|
+
pieces.push({ text: inner.value.raw, at: inner });
|
|
189
|
+
for (const { text, at } of pieces) {
|
|
190
|
+
const dark = DARK.exec(text);
|
|
191
|
+
if (dark)
|
|
192
|
+
findings.push({
|
|
193
|
+
line: lineAt(at, text, dark.index),
|
|
194
|
+
rule: 'colour',
|
|
195
|
+
message: "A `dark:` utility: the theme is the tokens' job.",
|
|
196
|
+
fix: 'Drop it and use a token (`bg-surface`, `text-muted`, `border-border`), which switches with the theme.',
|
|
197
|
+
});
|
|
198
|
+
for (const token of text.split(/\s+/)) {
|
|
199
|
+
const hex = HEX.exec(token);
|
|
200
|
+
// A hex colour in a class can only be an arbitrary value (`bg-[#0f0]`).
|
|
201
|
+
if (!hex || !token.slice(0, hex.index).includes('['))
|
|
202
|
+
continue;
|
|
203
|
+
findings.push({
|
|
204
|
+
line: lineAt(at, text, text.indexOf(token)),
|
|
205
|
+
rule: 'colour',
|
|
206
|
+
message: `A hex colour in a class (\`${token}\`): it follows neither the theme nor the product's palette.`,
|
|
207
|
+
fix: 'Use a token: `bg-surface`, `text-muted`, `border-border`, `bg-accent`, or a soft pair such as `bg-accent-soft text-accent-soft-foreground`.',
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
function copyFinding(line, text, where) {
|
|
214
|
+
return {
|
|
215
|
+
line,
|
|
216
|
+
rule: 'copy',
|
|
217
|
+
message: `Words written in the JSX${where} («${short(text)}»).`,
|
|
218
|
+
fix: "Move them to the screen's strings module and pass them as a prop or child: `label={t.orders.title}`.",
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
function checkAttributeCopy(attribute, findings) {
|
|
222
|
+
if (attribute.name.type !== 'JSXIdentifier')
|
|
223
|
+
return;
|
|
224
|
+
const name = attribute.name.name;
|
|
225
|
+
if (!USER_FACING.has(name) && !USER_FACING_SUFFIX.test(name))
|
|
226
|
+
return;
|
|
227
|
+
const text = literalText(attribute.value);
|
|
228
|
+
if (text && HAS_LETTER.test(text))
|
|
229
|
+
findings.push(copyFinding(lineOf(attribute), text, `, in \`${name}\``));
|
|
230
|
+
}
|
|
231
|
+
const elementName = (node) => node?.type === 'JSXElement' &&
|
|
232
|
+
node.openingElement.name.type === 'JSXIdentifier'
|
|
233
|
+
? node.openingElement.name.name
|
|
234
|
+
: undefined;
|
|
235
|
+
/** What in `code` breaks Nasim's rules, by line. Only what can be told from the text for sure. */
|
|
236
|
+
export function checkCode(manifest, code, path) {
|
|
237
|
+
let ast;
|
|
238
|
+
try {
|
|
239
|
+
ast = parse(code, {
|
|
240
|
+
sourceType: 'module',
|
|
241
|
+
plugins: plugins(path),
|
|
242
|
+
errorRecovery: true,
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
catch (error) {
|
|
246
|
+
const at = error.loc?.line ?? 1;
|
|
247
|
+
return [
|
|
248
|
+
{
|
|
249
|
+
line: at,
|
|
250
|
+
rule: 'parse',
|
|
251
|
+
message: `The code does not parse: ${error.message}`,
|
|
252
|
+
fix: 'Send the whole file, as TSX (or name its path so .ts is read without JSX).',
|
|
253
|
+
},
|
|
254
|
+
];
|
|
255
|
+
}
|
|
256
|
+
const exports = new Set(manifest.exports);
|
|
257
|
+
const findings = [];
|
|
258
|
+
// Local name → the Nasim export it is, so an aliased import is still checked.
|
|
259
|
+
const local = new Map();
|
|
260
|
+
const accepts = new Map();
|
|
261
|
+
for (const part of manifest.parts)
|
|
262
|
+
for (const [name, props] of Object.entries(part.accepts))
|
|
263
|
+
accepts.set(name, props);
|
|
264
|
+
walk(ast, (node, parents) => {
|
|
265
|
+
if (node.type === 'ImportDeclaration') {
|
|
266
|
+
checkImport(node, manifest, exports, findings);
|
|
267
|
+
if (node.source.value === PACKAGE)
|
|
268
|
+
for (const specifier of node.specifiers)
|
|
269
|
+
if (specifier.type === 'ImportSpecifier')
|
|
270
|
+
local.set(specifier.local.name, importName(specifier));
|
|
271
|
+
}
|
|
272
|
+
if (node.type === 'ExportNamedDeclaration' ||
|
|
273
|
+
node.type === 'ExportAllDeclaration')
|
|
274
|
+
checkReExport(node, exports, findings);
|
|
275
|
+
if (node.type === 'JSXAttribute') {
|
|
276
|
+
const name = node.name.type === 'JSXIdentifier' ? node.name.name : '';
|
|
277
|
+
if ((name === 'className' || name === 'class') && node.value)
|
|
278
|
+
checkClasses(node.value, findings);
|
|
279
|
+
checkAttributeCopy(node, findings);
|
|
280
|
+
}
|
|
281
|
+
if (node.type === 'CallExpression' &&
|
|
282
|
+
node.callee.type === 'Identifier' &&
|
|
283
|
+
CLASS_FUNCTIONS.has(node.callee.name))
|
|
284
|
+
for (const argument of node.arguments)
|
|
285
|
+
checkClasses(argument, findings);
|
|
286
|
+
const parent = parents.at(-1);
|
|
287
|
+
const inVerbatim = parents.some((p) => VERBATIM.has(elementName(p) ?? ''));
|
|
288
|
+
if (node.type === 'JSXText' && HAS_LETTER.test(node.value) && !inVerbatim) {
|
|
289
|
+
const first = node.value.search(/\S/);
|
|
290
|
+
findings.push(copyFinding(lineAt(node, node.value, first), node.value, ''));
|
|
291
|
+
}
|
|
292
|
+
if (node.type === 'JSXExpressionContainer' &&
|
|
293
|
+
(parent?.type === 'JSXElement' || parent?.type === 'JSXFragment') &&
|
|
294
|
+
!inVerbatim) {
|
|
295
|
+
const text = literalText(node);
|
|
296
|
+
if (text && HAS_LETTER.test(text))
|
|
297
|
+
findings.push(copyFinding(lineOf(node), text, ''));
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
// Props are checked once every import is known: an element may come before its import.
|
|
301
|
+
walk(ast, (node) => {
|
|
302
|
+
if (node.type !== 'JSXOpeningElement' || node.name.type !== 'JSXIdentifier')
|
|
303
|
+
return;
|
|
304
|
+
const name = local.get(node.name.name);
|
|
305
|
+
const props = name ? accepts.get(name) : undefined;
|
|
306
|
+
// ponytail: a props type that is a union lists only the props every branch shares; no
|
|
307
|
+
// documented part has one today. If one does, record each branch's props in the manifest.
|
|
308
|
+
if (name && props)
|
|
309
|
+
checkProps(node, name, props, findings);
|
|
310
|
+
});
|
|
311
|
+
// `className={cn('…')}` is read as a class and as a call: one finding, not two.
|
|
312
|
+
const seen = new Set();
|
|
313
|
+
return findings
|
|
314
|
+
.filter((finding) => {
|
|
315
|
+
const key = `${finding.line}:${finding.rule}:${finding.message}`;
|
|
316
|
+
return !seen.has(key) && seen.add(key);
|
|
317
|
+
})
|
|
318
|
+
.sort((a, b) => a.line - b.line);
|
|
319
|
+
}
|
package/lib/find.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Manifest } from './manifest.ts';
|
|
2
|
+
export interface Hit {
|
|
3
|
+
kind: 'part' | 'guide';
|
|
4
|
+
name: string;
|
|
5
|
+
score: number;
|
|
6
|
+
reasons: string[];
|
|
7
|
+
}
|
|
8
|
+
export declare const queryWords: (query: string) => string[];
|
|
9
|
+
/** Parts and guides ranked by the query's words in their names, keywords, lines and pages. */
|
|
10
|
+
export declare function search(manifest: Manifest, query: string, limit: number): Hit[];
|
|
11
|
+
/**
|
|
12
|
+
* The parts whose job, keywords, name or line share words with what a screen needs. Plain word
|
|
13
|
+
* matching, no model: a part whose page does not use the words is not found.
|
|
14
|
+
*/
|
|
15
|
+
export declare function findParts(manifest: Manifest, need: string, limit: number): Hit[];
|
package/lib/find.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { words } from "./text.js";
|
|
2
|
+
// Words that say nothing about which part a screen needs.
|
|
3
|
+
const STOP = new Set(('a an the to of for and or with in on at by from as is are be it its this that these those ' +
|
|
4
|
+
'i we you my our need needs want wants show shows showing screen page view use using some ' +
|
|
5
|
+
'one can should must where when what which who how into about before after each every' +
|
|
6
|
+
' و با به از در برای که را یک این آن تا هم یا روی').split(' '));
|
|
7
|
+
// A query word matches a word of the text when they are equal, or when one starts the other
|
|
8
|
+
// and the shorter has at least four letters («copy» finds «copied», «dialog» finds «dialogs»).
|
|
9
|
+
function matches(query, text) {
|
|
10
|
+
return text.some((word) => word === query ||
|
|
11
|
+
(Math.min(word.length, query.length) >= 4 &&
|
|
12
|
+
(word.startsWith(query) || query.startsWith(word))));
|
|
13
|
+
}
|
|
14
|
+
function score(query, fields) {
|
|
15
|
+
let total = 0;
|
|
16
|
+
const reasons = [];
|
|
17
|
+
for (const field of fields) {
|
|
18
|
+
const text = words(field.text);
|
|
19
|
+
const found = query.filter((word) => matches(word, text));
|
|
20
|
+
if (!found.length)
|
|
21
|
+
continue;
|
|
22
|
+
total += found.length * field.weight;
|
|
23
|
+
reasons.push(`${field.name}: ${found.join(', ')}`);
|
|
24
|
+
}
|
|
25
|
+
return { total, reasons };
|
|
26
|
+
}
|
|
27
|
+
function partFields(part, withBody) {
|
|
28
|
+
return [
|
|
29
|
+
{ name: 'name', weight: 6, text: part.names.join(' ') },
|
|
30
|
+
{ name: 'job', weight: 5, text: part.job },
|
|
31
|
+
{ name: 'keywords', weight: 4, text: part.keywords.join(' ') },
|
|
32
|
+
{ name: 'description', weight: 3, text: part.description },
|
|
33
|
+
...(withBody ? [{ name: 'page', weight: 1, text: part.markdown }] : []),
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
function guideFields(guide) {
|
|
37
|
+
return [
|
|
38
|
+
{ name: 'title', weight: 6, text: guide.title },
|
|
39
|
+
{ name: 'keywords', weight: 4, text: guide.keywords.join(' ') },
|
|
40
|
+
{ name: 'description', weight: 3, text: guide.description },
|
|
41
|
+
{ name: 'page', weight: 1, text: guide.markdown },
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
// A one-letter word is the tail of «record's» or a stray key, never what a screen needs.
|
|
45
|
+
export const queryWords = (query) => [...new Set(words(query))].filter((word) => word.length > 1 && !STOP.has(word));
|
|
46
|
+
function rank(hits, limit) {
|
|
47
|
+
return hits
|
|
48
|
+
.filter((hit) => hit.score > 0)
|
|
49
|
+
.sort((a, b) => b.score - a.score || a.name.localeCompare(b.name))
|
|
50
|
+
.slice(0, limit);
|
|
51
|
+
}
|
|
52
|
+
/** Parts and guides ranked by the query's words in their names, keywords, lines and pages. */
|
|
53
|
+
export function search(manifest, query, limit) {
|
|
54
|
+
const query_ = queryWords(query);
|
|
55
|
+
return rank([
|
|
56
|
+
...manifest.parts.map((part) => {
|
|
57
|
+
const { total, reasons } = score(query_, partFields(part, true));
|
|
58
|
+
return {
|
|
59
|
+
kind: 'part',
|
|
60
|
+
name: part.name,
|
|
61
|
+
score: total,
|
|
62
|
+
reasons,
|
|
63
|
+
};
|
|
64
|
+
}),
|
|
65
|
+
...manifest.guides.map((guide) => {
|
|
66
|
+
const { total, reasons } = score(query_, guideFields(guide));
|
|
67
|
+
return {
|
|
68
|
+
kind: 'guide',
|
|
69
|
+
name: guide.slug,
|
|
70
|
+
score: total,
|
|
71
|
+
reasons,
|
|
72
|
+
};
|
|
73
|
+
}),
|
|
74
|
+
], limit);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The parts whose job, keywords, name or line share words with what a screen needs. Plain word
|
|
78
|
+
* matching, no model: a part whose page does not use the words is not found.
|
|
79
|
+
*/
|
|
80
|
+
export function findParts(manifest, need, limit) {
|
|
81
|
+
const query = queryWords(need);
|
|
82
|
+
return rank(manifest.parts.map((part) => {
|
|
83
|
+
const { total, reasons } = score(query, partFields(part, false));
|
|
84
|
+
return { kind: 'part', name: part.name, score: total, reasons };
|
|
85
|
+
}), limit);
|
|
86
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
export interface Prop {
|
|
2
|
+
name: string;
|
|
3
|
+
type: string;
|
|
4
|
+
required: boolean;
|
|
5
|
+
default?: string;
|
|
6
|
+
description: string;
|
|
7
|
+
}
|
|
8
|
+
/** One `<Props>` table of a page: the props it explains, as the page shows them. */
|
|
9
|
+
export interface PropsTable {
|
|
10
|
+
name: string;
|
|
11
|
+
props: Prop[];
|
|
12
|
+
}
|
|
13
|
+
export interface Example {
|
|
14
|
+
/** `status-chip/basic`: the file `libs/nasim/examples/<name>.tsx`. */
|
|
15
|
+
name: string;
|
|
16
|
+
title: string;
|
|
17
|
+
code: string;
|
|
18
|
+
}
|
|
19
|
+
export interface Part {
|
|
20
|
+
/** The part the page is about (its frontmatter `part`). */
|
|
21
|
+
name: string;
|
|
22
|
+
/** Every name the page's import line brings in: a page may document several parts. */
|
|
23
|
+
names: string[];
|
|
24
|
+
title: string;
|
|
25
|
+
group: string;
|
|
26
|
+
description: string;
|
|
27
|
+
kind: string;
|
|
28
|
+
server: boolean;
|
|
29
|
+
since: string;
|
|
30
|
+
status: string;
|
|
31
|
+
source: string;
|
|
32
|
+
job: string;
|
|
33
|
+
keywords: string[];
|
|
34
|
+
related: string[];
|
|
35
|
+
/**
|
|
36
|
+
* A part of a subpath entry (`list`): it is imported from `@nyxon/nasim/list`, and its names are
|
|
37
|
+
* not in `exports`, which is the main entry's. Absent for a part of the main entry.
|
|
38
|
+
*/
|
|
39
|
+
entry?: string;
|
|
40
|
+
importLine: string;
|
|
41
|
+
url: string;
|
|
42
|
+
markdownUrl: string;
|
|
43
|
+
tables: PropsTable[];
|
|
44
|
+
/**
|
|
45
|
+
* Every prop each documented component accepts, by component, not only the ones a table
|
|
46
|
+
* explains: what `check_code` holds a JSX element to.
|
|
47
|
+
*/
|
|
48
|
+
accepts: Record<string, string[]>;
|
|
49
|
+
examples: Example[];
|
|
50
|
+
rules: string;
|
|
51
|
+
accessibility: string;
|
|
52
|
+
markdown: string;
|
|
53
|
+
}
|
|
54
|
+
export interface Guide {
|
|
55
|
+
slug: string;
|
|
56
|
+
title: string;
|
|
57
|
+
description: string;
|
|
58
|
+
keywords: string[];
|
|
59
|
+
url: string;
|
|
60
|
+
markdownUrl: string;
|
|
61
|
+
markdown: string;
|
|
62
|
+
}
|
|
63
|
+
export interface Manifest {
|
|
64
|
+
package: string;
|
|
65
|
+
/** The version the docs describe, as the docs site labels it («unreleased, after 0.12.0»). */
|
|
66
|
+
version: string;
|
|
67
|
+
site: string;
|
|
68
|
+
llms: string;
|
|
69
|
+
rules: string[];
|
|
70
|
+
/** Every name `@nyxon/nasim` exports, documented or not. */
|
|
71
|
+
exports: string[];
|
|
72
|
+
/** The exports that exist at run time (components, hooks, helpers), not types only. */
|
|
73
|
+
values: string[];
|
|
74
|
+
parts: Part[];
|
|
75
|
+
guides: Guide[];
|
|
76
|
+
}
|
package/lib/manifest.js
ADDED
package/lib/server.d.ts
ADDED