Skip to content

Commit

Permalink
lint against unknown step attributes (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkot authored Sep 8, 2022
1 parent f8ce2b7 commit e0f1502
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 13 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"command-line-args": "^5.2.0",
"command-line-usage": "^6.1.1",
"dedent-js": "^1.0.1",
"ecmarkdown": "^7.1.0",
"ecmarkdown": "^7.2.0",
"eslint-formatter-codeframe": "^7.32.1",
"fast-glob": "^3.2.7",
"grammarkdown": "^3.2.0",
Expand Down
2 changes: 2 additions & 0 deletions src/lint/collect-algorithm-diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import lintAlgorithmLineStyle from './rules/algorithm-line-style';
import lintAlgorithmStepNumbering from './rules/algorithm-step-numbering';
import lintAlgorithmStepLabels from './rules/algorithm-step-labels';
import lintForEachElement from './rules/for-each-element';
import lintStepAttributes from './rules/step-attributes';

const algorithmRules = [
lintAlgorithmLineStyle,
lintAlgorithmStepNumbering,
lintAlgorithmStepLabels,
lintForEachElement,
lintStepAttributes,
];

function composeObservers(...observers: Observer[]): Observer {
Expand Down
10 changes: 5 additions & 5 deletions src/lint/rules/algorithm-step-labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ export default function (report: Reporter, node: Element, algorithmSource: strin
const idAttr = node.attrs.find(({ key }) => key === 'id');
if (idAttr != null && !/^step-/.test(idAttr.value)) {
const itemSource = algorithmSource.slice(
node.location.start.offset,
node.location.end.offset
idAttr.location.start.offset,
idAttr.location.end.offset
);
const offset = itemSource.match(/^\s*\d+\. \[ *id *= *"/)![0].length;
const offset = itemSource.match(/^id *= *"/)![0].length;
report({
ruleId,
line: node.location.start.line,
column: node.location.start.column + offset,
line: idAttr.location.start.line,
column: idAttr.location.start.column + offset,
message: `step labels should start with "step-"`,
});
}
Expand Down
29 changes: 29 additions & 0 deletions src/lint/rules/step-attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { Node as EcmarkdownNode, Observer } from 'ecmarkdown';
import type { Reporter } from '../algorithm-error-reporter-type';

const ruleId = 'unknown-step-attribute';

const KNOWN_ATTRIBUTES = ['id', 'fence-effects'];

/*
Checks for unknown attributes on steps.
*/
export default function (report: Reporter): Observer {
return {
enter(node: EcmarkdownNode) {
if (node.name !== 'ordered-list-item') {
return;
}
for (const attr of node.attrs) {
if (!KNOWN_ATTRIBUTES.includes(attr.key)) {
report({
ruleId,
message: `unknown step attribute ${JSON.stringify(attr.key)}`,
line: attr.location.start.line,
column: attr.location.start.column,
});
}
}
},
};
}
16 changes: 16 additions & 0 deletions test/lint-algorithms.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,22 @@ describe('linting algorithms', () => {
});
});

describe('step attributes', () => {
const ruleId = 'unknown-step-attribute';
it('rejects unknown attributes', async () => {
await assertLint(
positioned`<emu-alg>
1. [id="step-id",${M}unknown="foo"] Step.
</emu-alg>`,
{
ruleId,
nodeType,
message: 'unknown step attribute "unknown"',
}
);
});
});

describe('for each element', () => {
const ruleId = 'for-each-element';
it('rejects loops without types', async () => {
Expand Down

0 comments on commit e0f1502

Please sign in to comment.