Skip to content

Commit

Permalink
lint: require use of labeled steps
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkot committed Jun 17, 2020
1 parent 5690549 commit 36fe377
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
4 changes: 4 additions & 0 deletions src/lint/collect-spelling-diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ let matchers = [
pattern: /\r/gu,
message: 'Only Unix-style (LF) linebreaks are allowed',
},
{
pattern: /(?<=\b[Ss]teps? )\d/gu,
message: 'Prefer using labeled steps and <emu-xref> tags over hardcoding step numbers',
},
];

export function collectSpellingDiagnostics(sourceText: string) {
Expand Down
19 changes: 2 additions & 17 deletions src/lint/rules/algorithm-step-numbering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,17 @@ import type { LintingError } from '../algorithm-error-reporter-type';
const ruleId = 'algorithm-step-numbering';

/*
Checks that step numbers are all `1`, with the exception of top-level lists whose first item is not `1`.
Checks that step numbers are all `1`.
*/
export default function (
report: (e: LintingError) => void,
node: Element,
algorithmSource: string
): Observer {
const nodeType = node.tagName;
let depth = -1;
let topLevelIsOne = false;
return {
enter(node: EcmarkdownNode) {
if (node.name === 'ol') {
++depth;
if (depth === 0) {
topLevelIsOne = node.start === 1;
}
} else if (node.name === 'ordered-list-item') {
if (depth === 0 && !topLevelIsOne) {
return;
}
if (node.name === 'ordered-list-item') {
let itemSource = algorithmSource.slice(
node.location!.start.offset,
node.location!.end.offset
Expand All @@ -41,10 +31,5 @@ export default function (
}
}
},
exit(node: EcmarkdownNode) {
if (node.name === 'ol') {
--depth;
}
},
};
}
19 changes: 15 additions & 4 deletions test/lint-algorithms.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ describe('linting algorithms', function () {
const ruleId = 'algorithm-step-numbering';

it('simple', async function () {
await assertLint(
positioned`<emu-alg>
${M}2. Step.
1. Step.
</emu-alg>`,
{
ruleId,
nodeType,
message: 'expected step number to be "1." (found "2.")',
}
);
await assertLint(
positioned`<emu-alg>
1. Step.
Expand Down Expand Up @@ -161,7 +172,7 @@ describe('linting algorithms', function () {

await assertLint(
positioned`<emu-alg>
2. Step:
1. Step:
${M}2. Substep.
</emu-alg>`,
{
Expand Down Expand Up @@ -202,9 +213,9 @@ describe('linting algorithms', function () {
it('negative', async function () {
await assertLintFree(`
<emu-alg>
2. Step.
3. Step.
40. Step.
1. Step.
1. Step.
1. Step.
</emu-alg>
`);
});
Expand Down
18 changes: 18 additions & 0 deletions test/lint-spelling.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ windows:${M}\r
);
});

it('step numbers', async function () {
await assertLint(
positioned`
Something about step ${M}1.
`,
{
ruleId: 'spelling',
nodeType: 'text',
message: 'Prefer using labeled steps and <emu-xref> tags over hardcoding step numbers',
}
);
});

it('negative', async function () {
await assertLintFree(`
<p>
Expand All @@ -188,6 +201,11 @@ windows:${M}\r
<emu-clause id="example">
<h1>Example</h1>
</emu-clause>
<emu-alg>
1. [label="example-label"] Foo.
</emu-alg>
Something about step <emu-xref href="#step-example-label"></emu-xref>.
`);
});
});

0 comments on commit 36fe377

Please sign in to comment.