@astryxdesign/cli 0.7.2-canary.4932317 → 0.7.2-canary.4a2f608
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/README.md +62 -62
- package/api/discover/_adapter.d.mts +119 -12
- package/api/discover/_adapter.mjs +449 -10
- package/api/discover/_adapter.test.mjs +599 -17
- package/api/discover/_catalog-view.d.mts +16 -5
- package/api/discover/_catalog-view.mjs +32 -8
- package/api/discover/discover.d.mts +11 -0
- package/api/discover/discover.doc.mjs +7 -6
- package/api/discover/discover.mjs +107 -19
- package/api/discover/discover.type.d.mts +18 -7
- package/api/discover/discover.type.mjs +15 -7
- package/api/discover/list/list.d.mts +4 -3
- package/api/discover/list/list.mjs +17 -10
- package/api/doctor/theme-checks.test.mjs +136 -0
- package/api/gap-report/gap-report.d.mts +3 -0
- package/api/gap-report/gap-report.doc.mjs +11 -3
- package/api/gap-report/gap-report.mjs +152 -48
- package/api/gap-report/gap-report.test.mjs +233 -0
- package/api/gap-report/gap-report.type.d.mts +41 -0
- package/api/gap-report/gap-report.type.mjs +18 -0
- package/api/integration/_adapter.d.mts +20 -0
- package/api/integration/_adapter.mjs +26 -0
- package/api/integration/authoring-checks.mjs +34 -3
- package/api/integration/authoring-checks.test.mjs +476 -0
- package/api/integration/authoring-checks.type.d.mts +86 -0
- package/api/integration/authoring-checks.type.mjs +39 -1
- package/api/integration/integrationComponentConflicts.doc.mjs +9 -3
- package/api/integration/integrationDocConflicts.doc.mjs +19 -5
- package/api/integration/pack-check.mjs +292 -7
- package/api/integration/pack-check.test.mjs +341 -0
- package/api/integration/validateIntegration.doc.mjs +3 -1
- package/api/search/search.d.mts +9 -7
- package/api/search/search.mjs +90 -42
- package/api/search/search.test.mjs +11 -0
- package/api/upgrade/_adapter.d.mts +40 -2
- package/api/upgrade/_adapter.mjs +117 -4
- package/api/upgrade/run/cli-behind-core.test.mjs +438 -0
- package/api/upgrade/run/run.mjs +30 -1
- package/api/upgrade/status/status.d.mts +21 -5
- package/api/upgrade/status/status.mjs +45 -8
- package/api/upgrade/upgrade.doc.mjs +5 -3
- package/api/upgrade/upgrade.type.d.mts +23 -0
- package/api/upgrade/upgrade.type.mjs +16 -1
- package/assets/codemods/runner.mjs +7 -1
- package/assets/discover/official-packages.json +367 -0
- package/assets/docs/author-a-theme.doc.mjs +2 -2
- package/assets/docs/browser-support.doc.mjs +1 -1
- package/assets/docs/styling-overview.doc.mjs +1 -1
- package/assets/docs/theme.doc.mjs +1 -1
- package/assets/docs/tree/add-a-topic.doc.mjs +1 -1
- package/assets/docs/tree/check-the-props.doc.mjs +2 -2
- package/assets/docs/tree/check-your-docs.doc.mjs +11 -3
- package/assets/docs/tree/checks.doc.mjs +4 -4
- package/assets/docs/tree/debug-and-gap-reports.doc.mjs +25 -5
- package/assets/docs/tree/layout-spacing.doc.mjs +2 -3
- package/assets/docs/tree/layout.doc.mjs +6 -6
- package/assets/docs/tree/migration-setup.doc.mjs +2 -2
- package/assets/docs/tree/publishing.doc.mjs +3 -3
- package/assets/docs/tree/quick-start.doc.mjs +3 -3
- package/assets/docs/tree/sections-and-placement.doc.mjs +2 -2
- package/assets/docs/tree/template-doc-overview.doc.mjs +1 -1
- package/assets/docs/tree/troubleshooting.doc.mjs +5 -5
- package/assets/docs/tree/upgrading.doc.mjs +1 -1
- package/authoring/discover/discover.doc.mjs +1 -1
- package/authoring/doctypes/component/component.doc.mjs +1 -1
- package/clients/cli/commands/discover.broken-integration.test.mjs +4 -1
- package/clients/cli/commands/discover.doc.mjs +1 -1
- package/clients/cli/commands/discover.mjs +144 -93
- package/clients/cli/commands/discover.no-source.test.mjs +447 -33
- package/clients/cli/commands/discover.sources.test.mjs +20 -1
- package/clients/cli/commands/doctor-integration-components.doc.mjs +3 -1
- package/clients/cli/commands/doctor-integration-docs.doc.mjs +18 -5
- package/clients/cli/commands/doctor-integration-validate.doc.mjs +3 -1
- package/clients/cli/commands/doctor.mjs +60 -3
- package/clients/cli/commands/gap-report.doc.mjs +14 -2
- package/clients/cli/commands/gap-report.mjs +24 -2
- package/clients/cli/commands/gap-report.test.mjs +79 -0
- package/clients/cli/commands/integration-check.test.mjs +103 -0
- package/clients/cli/commands/integration.mjs +2 -3
- package/clients/cli/commands/json-contract.test.mjs +5 -1
- package/clients/cli/commands/upgrade.doc.mjs +14 -0
- package/clients/cli/commands/upgrade.older-cli.test.mjs +160 -0
- package/foundation/discovery/copied-passages.d.mts +216 -0
- package/foundation/discovery/copied-passages.mjs +720 -0
- package/foundation/discovery/copied-passages.test.mjs +124 -0
- package/foundation/discovery/doc-fluff.d.mts +112 -0
- package/foundation/discovery/doc-fluff.mjs +328 -0
- package/foundation/discovery/doc-fluff.test.mjs +142 -0
- package/foundation/discovery/doc-types.d.mts +1 -1
- package/foundation/discovery/doc-types.mjs +3 -3
- package/foundation/discovery/doc-types.test.mjs +48 -0
- package/foundation/discovery/docs-discovery.d.mts +7 -0
- package/foundation/discovery/docs-discovery.mjs +26 -2
- package/foundation/discovery/project-typescript.d.mts +11 -2
- package/foundation/discovery/project-typescript.mjs +32 -4
- package/foundation/discovery/prop-drift.d.mts +1 -1
- package/foundation/discovery/prop-drift.mjs +2 -2
- package/foundation/discovery/prop-drift.test.mjs +55 -2
- package/foundation/discovery/type-text.mjs +1 -1
- package/foundation/discovery/type-text.test.mjs +1 -1
- package/foundation/doc-compiler/doc-loads.test.mjs +14 -2
- package/foundation/env/package-manager.d.mts +32 -0
- package/foundation/env/package-manager.mjs +47 -0
- package/foundation/env/package-manager.test.mjs +194 -7
- package/foundation/fs/module-loader.d.mts +2 -1
- package/foundation/fs/module-loader.mjs +60 -4
- package/foundation/fs/module-loader.test.mjs +194 -0
- package/foundation/response/response-types.doc.mjs +7 -6
- package/package.json +11 -11
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
import {describe, expect, it} from 'vitest';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
PASSAGE_WORDS,
|
|
7
|
+
contentWords,
|
|
8
|
+
passageWords,
|
|
9
|
+
wordSpans,
|
|
10
|
+
proseWords,
|
|
11
|
+
sharedPassages,
|
|
12
|
+
} from './copied-passages.mjs';
|
|
13
|
+
|
|
14
|
+
const words = (/** @type {string} */ text) =>
|
|
15
|
+
passageWords(text).map(word => ({word, section: null, block: 1}));
|
|
16
|
+
|
|
17
|
+
describe('passageWords', () => {
|
|
18
|
+
it('sets aside case, links, and Markdown emphasis', () => {
|
|
19
|
+
expect(
|
|
20
|
+
passageWords('Use **`Button`** for {@link component:Button} actions.'),
|
|
21
|
+
).toEqual(['use', 'button', 'for', 'actions.']);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe('proseWords', () => {
|
|
26
|
+
it('reads prose with its section, and skips code, references, and names', () => {
|
|
27
|
+
const doc = {
|
|
28
|
+
type: 'generic',
|
|
29
|
+
name: 'acme-guide',
|
|
30
|
+
title: 'Acme guide',
|
|
31
|
+
description: 'Intro words.',
|
|
32
|
+
sections: [
|
|
33
|
+
{
|
|
34
|
+
title: 'Setup',
|
|
35
|
+
content: [
|
|
36
|
+
{type: 'prose', text: 'Install it.'},
|
|
37
|
+
{type: 'code', lang: 'sh', code: 'npm install acme'},
|
|
38
|
+
{type: 'reference', ref: 'generic:other'},
|
|
39
|
+
{type: 'list', items: ['First step.']},
|
|
40
|
+
],
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
};
|
|
44
|
+
expect(proseWords(doc)).toEqual([
|
|
45
|
+
{word: 'intro', section: null, block: 1},
|
|
46
|
+
{word: 'words.', section: null, block: 1},
|
|
47
|
+
{word: 'install', section: 'Setup', block: 2},
|
|
48
|
+
{word: 'it.', section: 'Setup', block: 2},
|
|
49
|
+
{word: 'first', section: 'Setup', block: 3},
|
|
50
|
+
{word: 'step.', section: 'Setup', block: 3},
|
|
51
|
+
]);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('sharedPassages', () => {
|
|
56
|
+
const passage = Array.from(
|
|
57
|
+
{length: PASSAGE_WORDS + 5},
|
|
58
|
+
(_, i) => `w${i}`,
|
|
59
|
+
).join(' ');
|
|
60
|
+
|
|
61
|
+
it('finds the longest shared run', () => {
|
|
62
|
+
expect(
|
|
63
|
+
sharedPassages(words(`a b ${passage} c`), words(`x ${passage} y`)),
|
|
64
|
+
).toEqual([{start: 2, length: PASSAGE_WORDS + 5}]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('never runs a passage from one string into the next', () => {
|
|
68
|
+
const half = Math.ceil(PASSAGE_WORDS / 2);
|
|
69
|
+
const first = passage.split(' ').slice(0, half).join(' ');
|
|
70
|
+
const second = passage.split(' ').slice(half, PASSAGE_WORDS).join(' ');
|
|
71
|
+
const split = [
|
|
72
|
+
...passageWords(first).map(word => ({word, section: null, block: 1})),
|
|
73
|
+
...passageWords(second).map(word => ({word, section: null, block: 2})),
|
|
74
|
+
];
|
|
75
|
+
expect(sharedPassages(split, words(`${first} ${second}`))).toEqual([]);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('ignores a run shorter than the passage size', () => {
|
|
79
|
+
const short = passage
|
|
80
|
+
.split(' ')
|
|
81
|
+
.slice(0, PASSAGE_WORDS - 1)
|
|
82
|
+
.join(' ');
|
|
83
|
+
expect(
|
|
84
|
+
sharedPassages(words(`a ${short} b`), words(`c ${short} d`)),
|
|
85
|
+
).toEqual([]);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('contentWords', () => {
|
|
90
|
+
it('counts distinct words that are not function words', () => {
|
|
91
|
+
const run = words('and it is in the way that you can do it for them');
|
|
92
|
+
expect(contentWords(run, 0, run.length)).toBe(1);
|
|
93
|
+
const fact = words('Name each codemod folder after the Core version.');
|
|
94
|
+
expect(contentWords(fact, 0, fact.length)).toBe(5);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('wordSpans', () => {
|
|
99
|
+
it('reads the words passageWords reads, with where each sits as written', () => {
|
|
100
|
+
const text = 'Run `astryx docs`, then {@link generic:theme} *now*.';
|
|
101
|
+
const spans = wordSpans(text);
|
|
102
|
+
expect(spans.map(span => span.word)).toEqual(passageWords(text));
|
|
103
|
+
expect(text.slice(spans[1].start, spans[2].end)).toBe('`astryx docs`');
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe('passageWords and pointers', () => {
|
|
108
|
+
it('reads a link and a written docs command as no words', () => {
|
|
109
|
+
expect(
|
|
110
|
+
passageWords(
|
|
111
|
+
'See {@link generic:theme} or `astryx docs theme` (Share it).',
|
|
112
|
+
),
|
|
113
|
+
).toEqual(['see', 'or', 'share', 'it']);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('counts no punctuation-only token as a word', () => {
|
|
117
|
+
expect(
|
|
118
|
+
passageWords('Set the theme first, see {@link generic:theme}.'),
|
|
119
|
+
).toEqual(['set', 'the', 'theme', 'first', 'see']);
|
|
120
|
+
expect(
|
|
121
|
+
wordSpans('Read it - then {@link generic:theme}.').map(s => s.word),
|
|
122
|
+
).toEqual(['read', 'it', 'then']);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// @generated by scripts/sync-api-types.mjs from the JSDoc in foundation/**/*.mjs.
|
|
2
|
+
// DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A prose string with what is not prose blanked out, length kept: `{@link}`
|
|
6
|
+
* pointers, inline code, and quoted text. A quoted phrase is mentioned, not
|
|
7
|
+
* used ("cut 'in order to'").
|
|
8
|
+
*
|
|
9
|
+
* @param {string} text
|
|
10
|
+
* @returns {string}
|
|
11
|
+
*/
|
|
12
|
+
export function maskProse(text: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Each padding phrase in a text, outside inline code and quotes, in order,
|
|
15
|
+
* with its offset in the text.
|
|
16
|
+
*
|
|
17
|
+
* `thatAfter` is true when the phrase is followed by a "that" a reader cuts
|
|
18
|
+
* with it ("It is worth noting that the cache is shared").
|
|
19
|
+
*
|
|
20
|
+
* @param {string} text
|
|
21
|
+
* @returns {{phrase: string, at: number, thatAfter: boolean}[]}
|
|
22
|
+
*/
|
|
23
|
+
export function fillerAt(text: string): {
|
|
24
|
+
phrase: string;
|
|
25
|
+
at: number;
|
|
26
|
+
thatAfter: boolean;
|
|
27
|
+
}[];
|
|
28
|
+
/**
|
|
29
|
+
* Each padding phrase in a text, outside inline code and quotes, in order.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} text
|
|
32
|
+
* @returns {string[]}
|
|
33
|
+
*/
|
|
34
|
+
export function fillerIn(text: string): string[];
|
|
35
|
+
/**
|
|
36
|
+
* The share of a doc's prose words that are content words.
|
|
37
|
+
*
|
|
38
|
+
* @param {{word: string}[]} words
|
|
39
|
+
* @returns {number}
|
|
40
|
+
*/
|
|
41
|
+
export function density(words: {
|
|
42
|
+
word: string;
|
|
43
|
+
}[]): number;
|
|
44
|
+
/**
|
|
45
|
+
* A number rounded down to `places` decimals, so a share under the floor
|
|
46
|
+
* never reads as the floor.
|
|
47
|
+
*
|
|
48
|
+
* @param {number} value
|
|
49
|
+
* @param {number} places
|
|
50
|
+
* @returns {number}
|
|
51
|
+
*/
|
|
52
|
+
export function floorTo(value: number, places: number): number;
|
|
53
|
+
/**
|
|
54
|
+
* A share as a percent with one decimal, rounded down ("49.5%").
|
|
55
|
+
*
|
|
56
|
+
* @param {number} share
|
|
57
|
+
* @returns {string}
|
|
58
|
+
*/
|
|
59
|
+
export function sharePercent(share: number): string;
|
|
60
|
+
/**
|
|
61
|
+
* The words a topic's density is measured on: its prose, without what the
|
|
62
|
+
* phrase check also leaves unread (links, inline code, quoted text).
|
|
63
|
+
*
|
|
64
|
+
* @param {unknown} doc
|
|
65
|
+
* @returns {{word: string}[]}
|
|
66
|
+
*/
|
|
67
|
+
export function densityWords(doc: unknown): {
|
|
68
|
+
word: string;
|
|
69
|
+
}[];
|
|
70
|
+
/**
|
|
71
|
+
* The median density of Core's own guides of at least
|
|
72
|
+
* {@link DENSITY_MIN_WORDS} words.
|
|
73
|
+
*
|
|
74
|
+
* @returns {Promise<number>}
|
|
75
|
+
*/
|
|
76
|
+
export function coreDensityMedian(): Promise<number>;
|
|
77
|
+
/**
|
|
78
|
+
* Fluff in a package's topics: every padding phrase, with its section and
|
|
79
|
+
* file line, and every topic of at least {@link DENSITY_MIN_WORDS} words
|
|
80
|
+
* whose density is below {@link MIN_DENSITY}.
|
|
81
|
+
*
|
|
82
|
+
* @param {{records: {name: string, path: string}[], guides?: {name: string, ref?: {topicFile?: string}}[]}} discovered
|
|
83
|
+
* the package's docs, as discoverIntegrationDocs returns them
|
|
84
|
+
* @returns {Promise<import('../../api/integration/authoring-checks.type.mjs').DocFluff[]>}
|
|
85
|
+
*/
|
|
86
|
+
export function findFluff(discovered: {
|
|
87
|
+
records: {
|
|
88
|
+
name: string;
|
|
89
|
+
path: string;
|
|
90
|
+
}[];
|
|
91
|
+
guides?: {
|
|
92
|
+
name: string;
|
|
93
|
+
ref?: {
|
|
94
|
+
topicFile?: string;
|
|
95
|
+
};
|
|
96
|
+
}[];
|
|
97
|
+
}): Promise<import("../../api/integration/authoring-checks.type.mjs").DocFluff[]>;
|
|
98
|
+
/**
|
|
99
|
+
* Padding phrases: each can be cut, or replaced by a shorter word, without
|
|
100
|
+
* losing meaning. Only phrases that are padding in every use are listed;
|
|
101
|
+
* words such as "just", "simply" or "really", and phrases such as "whether or
|
|
102
|
+
* not" or "keep in mind", which carry meaning in a correct sentence ("not
|
|
103
|
+
* simply a wrapper", "regardless of whether"), are not.
|
|
104
|
+
*/
|
|
105
|
+
export const FILLER_PHRASES: string[];
|
|
106
|
+
/** A guide shorter than this many prose words is not scored for density. */
|
|
107
|
+
export const DENSITY_MIN_WORDS: 80;
|
|
108
|
+
/**
|
|
109
|
+
* A guide whose share of content words is below this is mostly glue. Core's
|
|
110
|
+
* own guides score from about 0.51 up, with a median near 0.62.
|
|
111
|
+
*/
|
|
112
|
+
export const MIN_DENSITY: 0.5;
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Fluff in docs: padding phrases that add words and no meaning, and guides
|
|
5
|
+
* whose prose is mostly function words. Docs are read by people and agents
|
|
6
|
+
* with a budget, so every word should carry something.
|
|
7
|
+
*
|
|
8
|
+
* Only prose is read (the same strings copied passages compare), and inline
|
|
9
|
+
* code and quoted text are set aside, so a phrase inside a command, or one
|
|
10
|
+
* quoted as an example, never counts.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
|
|
15
|
+
import {builtinGuideFiles} from './docs-discovery.mjs';
|
|
16
|
+
import {
|
|
17
|
+
contentWords,
|
|
18
|
+
fileWords,
|
|
19
|
+
lineAtOffset,
|
|
20
|
+
locateWords,
|
|
21
|
+
loadQuietly,
|
|
22
|
+
proseStrings,
|
|
23
|
+
passageWords,
|
|
24
|
+
textWords,
|
|
25
|
+
} from './copied-passages.mjs';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Padding phrases: each can be cut, or replaced by a shorter word, without
|
|
29
|
+
* losing meaning. Only phrases that are padding in every use are listed;
|
|
30
|
+
* words such as "just", "simply" or "really", and phrases such as "whether or
|
|
31
|
+
* not" or "keep in mind", which carry meaning in a correct sentence ("not
|
|
32
|
+
* simply a wrapper", "regardless of whether"), are not.
|
|
33
|
+
*/
|
|
34
|
+
export const FILLER_PHRASES = [
|
|
35
|
+
"it's important to note",
|
|
36
|
+
'it is important to note',
|
|
37
|
+
'important to note that',
|
|
38
|
+
"it's worth noting",
|
|
39
|
+
'it is worth noting',
|
|
40
|
+
'worth noting that',
|
|
41
|
+
'it should be noted',
|
|
42
|
+
'please note',
|
|
43
|
+
'note that',
|
|
44
|
+
'as mentioned above',
|
|
45
|
+
'as mentioned earlier',
|
|
46
|
+
'as mentioned before',
|
|
47
|
+
'as noted above',
|
|
48
|
+
'as you can see',
|
|
49
|
+
'needless to say',
|
|
50
|
+
'in order to',
|
|
51
|
+
'due to the fact that',
|
|
52
|
+
'for the purpose of',
|
|
53
|
+
'at this point in time',
|
|
54
|
+
'in other words',
|
|
55
|
+
'basically',
|
|
56
|
+
'essentially',
|
|
57
|
+
'obviously',
|
|
58
|
+
];
|
|
59
|
+
|
|
60
|
+
/** What to write instead, where a phrase is not simply cut. */
|
|
61
|
+
const INSTEAD = {
|
|
62
|
+
'in order to': 'to',
|
|
63
|
+
'due to the fact that': 'because',
|
|
64
|
+
'for the purpose of': 'to or for',
|
|
65
|
+
'at this point in time': 'now',
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** A guide shorter than this many prose words is not scored for density. */
|
|
69
|
+
export const DENSITY_MIN_WORDS = 80;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A guide whose share of content words is below this is mostly glue. Core's
|
|
73
|
+
* own guides score from about 0.51 up, with a median near 0.62.
|
|
74
|
+
*/
|
|
75
|
+
export const MIN_DENSITY = 0.5;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @param {string} phrase
|
|
79
|
+
* @returns {RegExp}
|
|
80
|
+
*/
|
|
81
|
+
function phrasePattern(phrase) {
|
|
82
|
+
const escaped = phrase.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
83
|
+
// "note that" is padding as a sentence opener ("Note that X"), not inside
|
|
84
|
+
// one ("a note that explains").
|
|
85
|
+
return phrase === 'note that'
|
|
86
|
+
? new RegExp(`(?:^|[.;:!?]\\s+)${escaped}\\b`, 'gi')
|
|
87
|
+
: // A hyphen joins words ("non-obviously"), so it bounds no phrase.
|
|
88
|
+
new RegExp(`(?<![a-z'-])${escaped}(?![a-z-])`, 'gi');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const PATTERNS = FILLER_PHRASES.map(phrase => ({
|
|
92
|
+
phrase,
|
|
93
|
+
pattern: phrasePattern(phrase),
|
|
94
|
+
}));
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* A prose string with what is not prose blanked out, length kept: `{@link}`
|
|
98
|
+
* pointers, inline code, and quoted text. A quoted phrase is mentioned, not
|
|
99
|
+
* used ("cut 'in order to'").
|
|
100
|
+
*
|
|
101
|
+
* @param {string} text
|
|
102
|
+
* @returns {string}
|
|
103
|
+
*/
|
|
104
|
+
export function maskProse(text) {
|
|
105
|
+
// A quoted phrase is mentioned, not used ("cut 'in order to'"), so quotes
|
|
106
|
+
// are set aside along with inline code. Each mask keeps the text's length,
|
|
107
|
+
// so an offset in the masked text is an offset in the text.
|
|
108
|
+
const blank = (/** @type {string} */ span) => ' '.repeat(span.length);
|
|
109
|
+
return (
|
|
110
|
+
text
|
|
111
|
+
// A pointer names a doc, and its target is no prose ("{@link
|
|
112
|
+
// generic:obviously-fast}").
|
|
113
|
+
.replace(/\{@link\s[^}]*\}/g, blank)
|
|
114
|
+
.replace(/`[^`]*`/g, blank)
|
|
115
|
+
.replace(/"[^"]*"|“[^”]*”|‘[^’]*’/g, blank)
|
|
116
|
+
.replace(/’/g, "'")
|
|
117
|
+
// A single-quoted span opens and closes beside a non-letter, so the
|
|
118
|
+
// apostrophe in "don't" or "it's" opens nothing.
|
|
119
|
+
.replace(/(?<![A-Za-z])'[^']*'(?![A-Za-z])/g, blank)
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Each padding phrase in a text, outside inline code and quotes, in order,
|
|
125
|
+
* with its offset in the text.
|
|
126
|
+
*
|
|
127
|
+
* `thatAfter` is true when the phrase is followed by a "that" a reader cuts
|
|
128
|
+
* with it ("It is worth noting that the cache is shared").
|
|
129
|
+
*
|
|
130
|
+
* @param {string} text
|
|
131
|
+
* @returns {{phrase: string, at: number, thatAfter: boolean}[]}
|
|
132
|
+
*/
|
|
133
|
+
export function fillerAt(text) {
|
|
134
|
+
const prose = maskProse(text);
|
|
135
|
+
/** @type {{phrase: string, at: number, thatAfter: boolean}[]} */
|
|
136
|
+
const found = [];
|
|
137
|
+
for (const {phrase, pattern} of PATTERNS) {
|
|
138
|
+
for (const match of prose.matchAll(pattern)) {
|
|
139
|
+
// A phrase inside a longer listed one ("note that" in "worth noting
|
|
140
|
+
// that") is the longer one's.
|
|
141
|
+
const at = match.index + match[0].toLowerCase().indexOf(phrase);
|
|
142
|
+
if (
|
|
143
|
+
!found.some(each => at >= each.at && at < each.at + each.phrase.length)
|
|
144
|
+
) {
|
|
145
|
+
found.push({
|
|
146
|
+
phrase,
|
|
147
|
+
at,
|
|
148
|
+
// Only a phrase that ends in note or noting leads into a "that"
|
|
149
|
+
// clause the reader cuts with it; "Obviously that works" and "As
|
|
150
|
+
// noted above that flag" keep their "that".
|
|
151
|
+
thatAfter:
|
|
152
|
+
/\bnot(?:e|ed|ing)$/.test(phrase) &&
|
|
153
|
+
!phrase.endsWith('that') &&
|
|
154
|
+
/^\s+that\b/i.test(prose.slice(at + phrase.length)),
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return found.sort((a, b) => a.at - b.at);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Each padding phrase in a text, outside inline code and quotes, in order.
|
|
164
|
+
*
|
|
165
|
+
* @param {string} text
|
|
166
|
+
* @returns {string[]}
|
|
167
|
+
*/
|
|
168
|
+
export function fillerIn(text) {
|
|
169
|
+
return fillerAt(text).map(each => each.phrase);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* The share of a doc's prose words that are content words.
|
|
174
|
+
*
|
|
175
|
+
* @param {{word: string}[]} words
|
|
176
|
+
* @returns {number}
|
|
177
|
+
*/
|
|
178
|
+
export function density(words) {
|
|
179
|
+
if (words.length === 0) return 1;
|
|
180
|
+
let content = 0;
|
|
181
|
+
for (let i = 0; i < words.length; i++) {
|
|
182
|
+
if (contentWords(words, i, 1) === 1) content++;
|
|
183
|
+
}
|
|
184
|
+
return content / words.length;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* A number rounded down to `places` decimals, so a share under the floor
|
|
189
|
+
* never reads as the floor.
|
|
190
|
+
*
|
|
191
|
+
* @param {number} value
|
|
192
|
+
* @param {number} places
|
|
193
|
+
* @returns {number}
|
|
194
|
+
*/
|
|
195
|
+
export function floorTo(value, places) {
|
|
196
|
+
const scale = 10 ** places;
|
|
197
|
+
return Math.floor(value * scale + 1e-9) / scale;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* A share as a percent with one decimal, rounded down ("49.5%").
|
|
202
|
+
*
|
|
203
|
+
* @param {number} share
|
|
204
|
+
* @returns {string}
|
|
205
|
+
*/
|
|
206
|
+
export function sharePercent(share) {
|
|
207
|
+
return `${floorTo(share * 100, 1).toFixed(1)}%`;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* The words a topic's density is measured on: its prose, without what the
|
|
212
|
+
* phrase check also leaves unread (links, inline code, quoted text).
|
|
213
|
+
*
|
|
214
|
+
* @param {unknown} doc
|
|
215
|
+
* @returns {{word: string}[]}
|
|
216
|
+
*/
|
|
217
|
+
export function densityWords(doc) {
|
|
218
|
+
return proseStrings(doc).flatMap(({text}) =>
|
|
219
|
+
passageWords(maskProse(text)).map(word => ({word})),
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* The median density of Core's own guides of at least
|
|
225
|
+
* {@link DENSITY_MIN_WORDS} words.
|
|
226
|
+
*
|
|
227
|
+
* @returns {Promise<number>}
|
|
228
|
+
*/
|
|
229
|
+
export async function coreDensityMedian() {
|
|
230
|
+
// Core's guides are measured the way a package's topics are.
|
|
231
|
+
/** @type {number[]} */
|
|
232
|
+
const scores = [];
|
|
233
|
+
for (const file of builtinGuideFiles()) {
|
|
234
|
+
const doc = await loadQuietly(file);
|
|
235
|
+
if (typeof doc?.name !== 'string') continue;
|
|
236
|
+
const words = densityWords(doc);
|
|
237
|
+
if (words.length >= DENSITY_MIN_WORDS) scores.push(density(words));
|
|
238
|
+
}
|
|
239
|
+
scores.sort((a, b) => a - b);
|
|
240
|
+
return scores.length === 0 ? 0 : scores[Math.floor(scores.length / 2)];
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Fluff in a package's topics: every padding phrase, with its section and
|
|
245
|
+
* file line, and every topic of at least {@link DENSITY_MIN_WORDS} words
|
|
246
|
+
* whose density is below {@link MIN_DENSITY}.
|
|
247
|
+
*
|
|
248
|
+
* @param {{records: {name: string, path: string}[], guides?: {name: string, ref?: {topicFile?: string}}[]}} discovered
|
|
249
|
+
* the package's docs, as discoverIntegrationDocs returns them
|
|
250
|
+
* @returns {Promise<import('../../api/integration/authoring-checks.type.mjs').DocFluff[]>}
|
|
251
|
+
*/
|
|
252
|
+
export async function findFluff(discovered) {
|
|
253
|
+
const topics = [
|
|
254
|
+
...discovered.records.map(record => ({
|
|
255
|
+
name: record.name,
|
|
256
|
+
file: record.path,
|
|
257
|
+
})),
|
|
258
|
+
...(discovered.guides ?? []).map(guide => ({
|
|
259
|
+
name: guide.name,
|
|
260
|
+
file: guide.ref?.topicFile,
|
|
261
|
+
})),
|
|
262
|
+
];
|
|
263
|
+
/** @type {import('../../api/integration/authoring-checks.type.mjs').DocFluff[]} */
|
|
264
|
+
const found = [];
|
|
265
|
+
/** @type {number | null} */
|
|
266
|
+
let median = null;
|
|
267
|
+
for (const {name, file} of topics) {
|
|
268
|
+
const doc = file ? await loadQuietly(file) : null;
|
|
269
|
+
if (!doc || !file) continue;
|
|
270
|
+
/** @type {{word: string, line: number}[]} */
|
|
271
|
+
let source = [];
|
|
272
|
+
try {
|
|
273
|
+
source = fileWords(fs.readFileSync(file, 'utf8'));
|
|
274
|
+
} catch {
|
|
275
|
+
// No line numbers; each finding still names its section.
|
|
276
|
+
}
|
|
277
|
+
// Each string is found by its whole run of words, from where the last
|
|
278
|
+
// one was found, so two strings that open alike keep their own lines.
|
|
279
|
+
let cursor = 0;
|
|
280
|
+
for (const {text, section} of proseStrings(doc)) {
|
|
281
|
+
const words = textWords(text);
|
|
282
|
+
let index = locateWords(source, words, cursor);
|
|
283
|
+
if (index < 0) index = locateWords(source, words);
|
|
284
|
+
if (index >= 0) cursor = index + words.length;
|
|
285
|
+
for (const {phrase, at, thatAfter} of fillerAt(text)) {
|
|
286
|
+
const line = index < 0 ? null : lineAtOffset(source, index, words, at);
|
|
287
|
+
const where = section ? ` (section "${section}")` : '';
|
|
288
|
+
const instead = /** @type {Record<string, string>} */ (INSTEAD)[phrase];
|
|
289
|
+
found.push({
|
|
290
|
+
topic: name,
|
|
291
|
+
kind: 'phrase',
|
|
292
|
+
phrase,
|
|
293
|
+
section,
|
|
294
|
+
file,
|
|
295
|
+
line,
|
|
296
|
+
density: null,
|
|
297
|
+
message:
|
|
298
|
+
`"${name}"${where} pads its prose with "${phrase}"` +
|
|
299
|
+
`${line == null ? '' : ` (line ${line})`}. ` +
|
|
300
|
+
(instead
|
|
301
|
+
? `Write "${instead}" instead.`
|
|
302
|
+
: thatAfter
|
|
303
|
+
? 'Cut it and the "that" after it; the sentence says the same without them.'
|
|
304
|
+
: 'Cut it; the sentence says the same without it.'),
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
const words = densityWords(doc);
|
|
309
|
+
if (words.length < DENSITY_MIN_WORDS) continue;
|
|
310
|
+
const score = density(words);
|
|
311
|
+
if (score >= MIN_DENSITY) continue;
|
|
312
|
+
median ??= await coreDensityMedian();
|
|
313
|
+
found.push({
|
|
314
|
+
topic: name,
|
|
315
|
+
kind: 'density',
|
|
316
|
+
phrase: null,
|
|
317
|
+
section: null,
|
|
318
|
+
file,
|
|
319
|
+
line: null,
|
|
320
|
+
density: floorTo(score, 3),
|
|
321
|
+
message:
|
|
322
|
+
`"${name}" is mostly glue: ${sharePercent(score)} of its ${words.length} prose words carry content, ` +
|
|
323
|
+
`under the ${Math.round(MIN_DENSITY * 100)}% floor (Core's guides have a median of ${Math.round(median * 100)}%). ` +
|
|
324
|
+
'Cut filler and restated context until each sentence states one thing.',
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
return found;
|
|
328
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
import {describe, expect, it} from 'vitest';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
density,
|
|
7
|
+
densityWords,
|
|
8
|
+
fillerAt,
|
|
9
|
+
fillerIn,
|
|
10
|
+
floorTo,
|
|
11
|
+
sharePercent,
|
|
12
|
+
} from './doc-fluff.mjs';
|
|
13
|
+
import {passageWords} from './copied-passages.mjs';
|
|
14
|
+
|
|
15
|
+
const words = (/** @type {string} */ text) =>
|
|
16
|
+
passageWords(text).map(word => ({word}));
|
|
17
|
+
|
|
18
|
+
describe('fillerIn', () => {
|
|
19
|
+
it('finds padding phrases in order, the longest one only', () => {
|
|
20
|
+
expect(
|
|
21
|
+
fillerIn(
|
|
22
|
+
'In order to start, it is worth noting that you basically run it. Note that it builds.',
|
|
23
|
+
),
|
|
24
|
+
).toEqual(['in order to', 'it is worth noting', 'basically', 'note that']);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('leaves inline code, quotes, a noun "note", and ambiguous words alone', () => {
|
|
28
|
+
expect(
|
|
29
|
+
fillerIn(
|
|
30
|
+
'Run `basically --in order to`. Cut "in order to" and “basically”. Add a note that explains it. Copy just this one file; it really is the source.',
|
|
31
|
+
),
|
|
32
|
+
).toEqual([]);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('sets aside single-quoted text, but not an apostrophe', () => {
|
|
36
|
+
expect(
|
|
37
|
+
fillerIn(
|
|
38
|
+
"Avoid ‘basically’ and ‘obviously’. Don't write 'it is worth noting that' in a guide.",
|
|
39
|
+
),
|
|
40
|
+
).toEqual([]);
|
|
41
|
+
expect(fillerIn("Don't pad: it's important to note the order.")).toEqual([
|
|
42
|
+
"it's important to note",
|
|
43
|
+
]);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('lists no phrase that carries meaning in a correct sentence', () => {
|
|
47
|
+
expect(
|
|
48
|
+
fillerIn(
|
|
49
|
+
'It is not simply a wrapper. Every app gets a theme whether or not anyone picks one. Keep in mind the order. Feel free to skip it.',
|
|
50
|
+
),
|
|
51
|
+
).toEqual([]);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('reads a curly apostrophe like a straight one', () => {
|
|
55
|
+
expect(fillerIn('It’s important to note the order.')).toEqual([
|
|
56
|
+
"it's important to note",
|
|
57
|
+
]);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('fillerAt', () => {
|
|
62
|
+
it('reads no phrase inside a link target or a hyphenated word', () => {
|
|
63
|
+
expect(
|
|
64
|
+
fillerIn(
|
|
65
|
+
'Read {@link generic:obviously-fast} before you tune the build.',
|
|
66
|
+
),
|
|
67
|
+
).toEqual([]);
|
|
68
|
+
expect(fillerIn('A non-obviously slow build still passes.')).toEqual([]);
|
|
69
|
+
expect(fillerIn('It is obviously slow.')).toEqual(['obviously']);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('marks a "that" the reader cuts with the phrase', () => {
|
|
73
|
+
expect(
|
|
74
|
+
fillerAt('It is worth noting that the cache is shared.').map(
|
|
75
|
+
each => each.thatAfter,
|
|
76
|
+
),
|
|
77
|
+
).toEqual([true]);
|
|
78
|
+
expect(
|
|
79
|
+
fillerAt('Basically, the cache is shared.').map(each => each.thatAfter),
|
|
80
|
+
).toEqual([false]);
|
|
81
|
+
// Only a note or noting phrase leads a clause the reader cuts.
|
|
82
|
+
expect(
|
|
83
|
+
fillerAt('Obviously that works only in a browser.').map(
|
|
84
|
+
each => each.thatAfter,
|
|
85
|
+
),
|
|
86
|
+
).toEqual([false]);
|
|
87
|
+
expect(
|
|
88
|
+
fillerAt('As noted above that flag is required.').map(
|
|
89
|
+
each => each.thatAfter,
|
|
90
|
+
),
|
|
91
|
+
).toEqual([false]);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('densityWords', () => {
|
|
96
|
+
it('measures prose only, not quoted text, inline code or links', () => {
|
|
97
|
+
const doc = {
|
|
98
|
+
sections: [
|
|
99
|
+
{
|
|
100
|
+
title: 'Overview',
|
|
101
|
+
content: [
|
|
102
|
+
{
|
|
103
|
+
type: 'prose',
|
|
104
|
+
text: 'Theme tokens set colors. Write "it is in the" or `of the` as {@link generic:theme} shows.',
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
};
|
|
110
|
+
expect(densityWords(doc).map(each => each.word)).toEqual([
|
|
111
|
+
'theme',
|
|
112
|
+
'tokens',
|
|
113
|
+
'set',
|
|
114
|
+
'colors.',
|
|
115
|
+
'write',
|
|
116
|
+
'or',
|
|
117
|
+
'as',
|
|
118
|
+
'shows.',
|
|
119
|
+
]);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe('share rounding', () => {
|
|
124
|
+
it('rounds a share under the floor down, never up to it', () => {
|
|
125
|
+
expect(floorTo(0.4995, 3)).toBe(0.499);
|
|
126
|
+
expect(sharePercent(0.4995)).toBe('49.9%');
|
|
127
|
+
expect(sharePercent(0.5)).toBe('50.0%');
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe('density', () => {
|
|
132
|
+
it('scores a padded sentence well below a dense one', () => {
|
|
133
|
+
const padded = words(
|
|
134
|
+
'So that is why it is the one that you would want to use if you can.',
|
|
135
|
+
);
|
|
136
|
+
const dense = words(
|
|
137
|
+
'Name each codemod folder after the Core version whose upgrade runs it.',
|
|
138
|
+
);
|
|
139
|
+
expect(density(padded)).toBeLessThan(0.3);
|
|
140
|
+
expect(density(dense)).toBeGreaterThan(0.6);
|
|
141
|
+
});
|
|
142
|
+
});
|