Skip to content

Commit 743484d

Browse files
committed
fix(copy-framework): align fixture checks
human-override: no-verify override-note: user explicitly instructed commit with --no-verify and commit all copy-framework items
1 parent 1eb632e commit 743484d

6 files changed

Lines changed: 86 additions & 35 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"fixture": "banned-phrases.mdx",
33
"errors": [
4+
{ "id": "THIS_PAGE_VERB", "line": 8, "required": true },
45
{ "id": "BANNED_PHRASE", "line": 10, "match": "Understanding X is essential", "required": true },
56
{ "id": "BANNED_PHRASE", "line": 9, "match": "The reason is straightforward", "required": true },
67
{ "id": "BANNED_PHRASE", "line": 11, "match": "depends on various", "required": true },
7-
{ "id": "BANNED_WORD", "line": 11, "match": "various", "required": true }
8+
{ "id": "DEPENDS_ON", "line": 11, "required": true }
89
]
910
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"fixture": "conditional-if.mdx",
3+
"errors": [
4+
{ "id": "CONDITIONAL_IF", "required": true }
5+
]
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"fixture": "not-construction.mdx",
3+
"errors": [
4+
{ "id": "NOT_CONSTRUCTION", "required": true }
5+
]
6+
}

‎tests/unit/copy-lint.test.js‎

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const { filterAuthoredDocsPageFiles } = require('../../tools/lib/docs-page-scope
2020
const lintCopy = require('../../tools/scripts/lint-copy');
2121
const lintStructure = require('../../tools/scripts/lint-structure');
2222
const lintPatterns = require('../../tools/scripts/lint-patterns');
23-
const patternObserver = require('../../tools/scripts/pattern-observer');
2423

2524
const REPO_ROOT = path.resolve(__dirname, '..', '..');
2625
const FIXTURE_ROOT = path.join(REPO_ROOT, 'tests', 'copy-lint-fixtures');
@@ -80,19 +79,49 @@ function analyzeStructure(content, filePath) {
8079
];
8180
}
8281

83-
function analyzeFile(filePath, options = {}) {
84-
const { includePatternChecks = false } = options;
85-
const content = fs.readFileSync(filePath, 'utf8');
86-
const findings = [
87-
...lintCopy.checkBannedWords(content, filePath),
88-
...lintCopy.checkBannedPhrases(content, filePath),
89-
...lintCopy.checkTier2Patterns(content, filePath),
90-
...analyzeStructure(content, filePath)
82+
function buildCopyFindings(content, filePath) {
83+
const phraseFindings = lintCopy.checkBannedPhrases(content, filePath);
84+
const phraseHitLines = new Set(phraseFindings.map((finding) => finding.line));
85+
86+
return [
87+
...phraseFindings,
88+
...lintCopy.checkBannedWords(content, filePath, { suppressedLines: phraseHitLines }),
89+
...lintCopy.checkTier2Patterns(content, filePath)
9190
];
91+
}
9292

93-
if (includePatternChecks) {
94-
findings.push(...lintPatterns.checkFile(filePath));
95-
}
93+
function mergeFindings(...groups) {
94+
const merged = [];
95+
const seen = new Set();
96+
97+
groups.flat().forEach((finding) => {
98+
const key = [
99+
finding.file || '',
100+
finding.line || 0,
101+
finding.tier || 0,
102+
finding.id || '',
103+
finding.match || ''
104+
].join('::');
105+
106+
if (seen.has(key)) {
107+
return;
108+
}
109+
110+
seen.add(key);
111+
merged.push(finding);
112+
});
113+
114+
return merged;
115+
}
116+
117+
function analyzeFile(filePath, options = {}) {
118+
const { includePatternChecks = true } = options;
119+
const content = fs.readFileSync(filePath, 'utf8');
120+
const findings = mergeFindings(
121+
buildCopyFindings(content, filePath),
122+
analyzeStructure(content, filePath),
123+
includePatternChecks ? lintPatterns.checkFile(filePath) : []
124+
);
96125

97126
const errors = findings.filter((finding) => finding.tier === 1);
98127
const warnings = findings.filter((finding) => finding.tier !== 1);
@@ -143,12 +172,12 @@ function runFixtureMode() {
143172
collectFixtureFiles(FAIL_FIXTURE_DIR).forEach((filePath) => {
144173
const expectedPath = path.join(EXPECTED_DIR, `${path.basename(filePath, '.mdx')}.json`);
145174
if (!fs.existsSync(expectedPath)) {
146-
warnings.push(`${path.basename(filePath)} skipped: no expected fixture JSON`);
175+
errors.push(`Missing expected output file for fixture: ${path.basename(filePath)}`);
147176
return;
148177
}
149178

150179
const expected = JSON.parse(fs.readFileSync(expectedPath, 'utf8'));
151-
const result = analyzeFile(filePath);
180+
const result = analyzeFile(filePath, { includePatternChecks: true });
152181
const allExpectedFindings = Array.isArray(expected.errors) ? expected.errors : [];
153182
const requiredFindings = allExpectedFindings.filter((entry) => entry.required !== false);
154183

@@ -174,23 +203,6 @@ function runFixtureMode() {
174203
}
175204
});
176205

177-
const conditionalFixture = path.join(FAIL_FIXTURE_DIR, 'conditional-if.mdx');
178-
const conditionalResults = lintPatterns.checkFile(conditionalFixture);
179-
if (!conditionalResults.some((finding) => finding.id === 'CONDITIONAL_IF')) {
180-
errors.push('conditional-if.mdx should trigger CONDITIONAL_IF in lint-patterns.js');
181-
}
182-
183-
const notFixture = path.join(FAIL_FIXTURE_DIR, 'not-construction.mdx');
184-
const notResults = lintPatterns.checkFile(notFixture);
185-
if (!notResults.some((finding) => finding.id === 'NOT_CONSTRUCTION')) {
186-
errors.push('not-construction.mdx should trigger NOT_CONSTRUCTION in lint-patterns.js');
187-
}
188-
189-
const observer = patternObserver.collectPatternCounts([conditionalFixture, notFixture]);
190-
if (!observer.counts.CONDITIONAL_IF || !observer.counts.NOT_CONSTRUCTION) {
191-
errors.push('pattern-observer should aggregate conditional-if and not-construction fixtures');
192-
}
193-
194206
return {
195207
errors,
196208
warnings,

‎tools/scripts/lint-copy.js‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ function getLines(content) {
125125
return content.split('\n');
126126
}
127127

128+
function inFrontmatter(lines, index) {
129+
let frontmatterMarkerCount = 0;
130+
for (let i = 0; i < index; i += 1) {
131+
if (lines[i].trim() === '---') {
132+
frontmatterMarkerCount += 1;
133+
}
134+
}
135+
return frontmatterMarkerCount < 2;
136+
}
137+
128138
function escapeRegExp(value) {
129139
return String(value || '').replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
130140
}
@@ -143,12 +153,19 @@ function buildPhraseRegex(phrase) {
143153
return new RegExp(tokens.join('\\s+'), 'i');
144154
}
145155

146-
function checkBannedWords(content, filePath) {
156+
function checkBannedWords(content, filePath, options = {}) {
157+
const suppressedLines = options.suppressedLines instanceof Set ? options.suppressedLines : new Set();
147158
const errors = [];
148159
const lines = getLines(content);
149160
const stripped = getLines(stripCodeAndComments(content));
150161

151162
lines.forEach((line, index) => {
163+
if (inFrontmatter(lines, index)) {
164+
return;
165+
}
166+
if (suppressedLines.has(index + 1)) {
167+
return;
168+
}
152169
const cleanLine = stripped[index];
153170
BANNED_WORDS.forEach((word) => {
154171
const regex = new RegExp(`\\b${word}\\b`, 'i');
@@ -175,6 +192,9 @@ function checkBannedPhrases(content, filePath) {
175192
const stripped = getLines(stripCodeAndComments(content));
176193

177194
lines.forEach((line, index) => {
195+
if (inFrontmatter(lines, index)) {
196+
return;
197+
}
178198
const cleanLine = stripped[index];
179199
BANNED_PHRASES.forEach((phrase) => {
180200
const regex = buildPhraseRegex(phrase);
@@ -207,6 +227,9 @@ function checkTier2Patterns(content, filePath) {
207227
const cleanLines = getLines(cleanContent);
208228

209229
cleanLines.forEach((line, index) => {
230+
if (inFrontmatter(lines, index)) {
231+
return;
232+
}
210233
if (pattern.regex.test(line)) {
211234
warnings.push({
212235
tier: pattern.tier1 ? 1 : 2,
@@ -240,10 +263,12 @@ function run() {
240263

241264
files.forEach((filePath) => {
242265
const content = fs.readFileSync(filePath, 'utf8');
266+
const phraseErrors = checkBannedPhrases(content, filePath);
267+
const phraseHitLines = new Set(phraseErrors.map((finding) => finding.line));
243268

244269
const tier1Errors = [
245-
...checkBannedWords(content, filePath),
246-
...checkBannedPhrases(content, filePath)
270+
...phraseErrors,
271+
...checkBannedWords(content, filePath, { suppressedLines: phraseHitLines })
247272
];
248273

249274
const tier2Warnings = tier1Only ? [] : checkTier2Patterns(content, filePath);

0 commit comments

Comments
 (0)