Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spec/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ <h1>Options</h1>
<tr><td>`--assets-dir`</td><td>`assetsDir`</td><td>Directory in which to place assets when using `--assets=external`. Defaults to "assets".</td></tr>
<tr><td>`--lint-spec`</td><td>`lintSpec`</td><td>Enforce some style and correctness checks.</td></tr>
<tr><td>`--error-formatter`</td><td></td><td>The <a href="https://eslint.org/docs/user-guide/formatters/">eslint formatter</a> to be used for printing warnings and errors when using `--verbose`. Either the name of a built-in eslint formatter or the package name of an installed eslint compatible formatter.</td></tr>
<tr><td>`--max-clause-depth N`</td><td></td><td>Warn when clauses exceed a nesting depth of N.</td></tr>
<tr><td>`--strict`</td><td></td><td>Exit with an error if there are warnings. Cannot be used with `--watch`.</td></tr>
<tr><td>`--multipage`</td><td></td><td>Emit a distinct page for each top-level clause.</td></tr>
</table>
Expand Down
6 changes: 6 additions & 0 deletions src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ export const options = [
description:
'The formatter for warnings and errors; either a path prefixed with "." or "./", or package name, of an installed eslint compatible formatter (default: eslint-formatter-codeframe)',
},
{
name: 'max-clause-depth',
type: Number,
description:
'The maximum nesting depth for clauses; exceeding this will cause a warning. Defaults to no limit.',
},
{
name: 'multipage',
type: Boolean,
Expand Down
12 changes: 11 additions & 1 deletion src/clauseNums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default function iterator(spec: Spec): ClauseNumberIterator {
const ids: (string | number[])[] = [];
let inAnnex = false;
let currentLevel = 0;
let hasWarnedForExcessNesting = false;

return {
next(clauseStack: Clause[], node: HTMLElement) {
Expand All @@ -26,10 +27,19 @@ export default function iterator(spec: Spec): ClauseNumberIterator {
spec.warn({
type: 'node',
node,
ruleId: 'skipped-caluse',
ruleId: 'skipped-clause',
message: 'clause is being numbered without numbering its parent clause',
});
}
if (!hasWarnedForExcessNesting && level + 1 > (spec.opts.maxClauseDepth ?? Infinity)) {
spec.warn({
type: 'node',
node,
ruleId: 'max-clause-depth',
message: `clause exceeds maximum nesting depth of ${spec.opts.maxClauseDepth}`,
});
hasWarnedForExcessNesting = true;
}

const nextNum = annex ? nextAnnexNum : nextClauseNum;

Expand Down
3 changes: 3 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ const build = debounce(async function build() {
if (args['mark-effects']) {
opts.markEffects = true;
}
if (args['max-clause-depth']) {
opts.maxClauseDepth = args['max-clause-depth'];
}
if (args['no-toc'] != null) {
opts.toc = !args['no-toc'];
}
Expand Down
1 change: 1 addition & 0 deletions src/ecmarkup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface Options {
copyright?: boolean;
date?: Date;
location?: string;
maxClauseDepth?: number;
multipage?: boolean;
extraBiblios?: ExportedBiblio[];
contributors?: string;
Expand Down
45 changes: 45 additions & 0 deletions test/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1237,4 +1237,49 @@ ${M} </pre>
`);
});
});

describe('max clause depth', () => {
it('max depth', async () => {
await assertError(
positioned`
<emu-clause id="one">
<h1>One</h1>
${M}<emu-clause id="two">
<h1>Two</h1>
</emu-clause>
<emu-clause id="two-again">
<h1>Not warned</h1>
</emu-clause>
</emu-clause>
`,
{
ruleId: 'max-clause-depth',
nodeType: 'emu-clause',
message: 'clause exceeds maximum nesting depth of 1',
},
{
maxClauseDepth: 1,
},
);
});

it('negative', async () => {
await assertErrorFree(
`
<emu-clause id="one">
<h1>One</h1>
<emu-clause id="two">
<h1>Two</h1>
</emu-clause>
<emu-clause id="two-again">
<h1>Not warned</h1>
</emu-clause>
</emu-clause>
`,
{
maxClauseDepth: 2,
},
);
});
});
});
Loading