Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint against unknown step attributes #482

Merged
merged 2 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 @@ -47,7 +47,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