Skip to content

Commit

Permalink
lint against non-kebab-cased enum values
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkot committed Aug 30, 2023
1 parent 3ad4798 commit 5293b7f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lint/collect-algorithm-diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { warnEmdFailure } from '../utils';
import lintAlgorithmLineStyle from './rules/algorithm-line-style';
import lintAlgorithmStepNumbering from './rules/algorithm-step-numbering';
import lintAlgorithmStepLabels from './rules/algorithm-step-labels';
import lintEnumCasing from './rules/enum-casing';
import lintForEachElement from './rules/for-each-element';
import lintStepAttributes from './rules/step-attributes';
import lintIfElseConsistency from './rules/if-else-consistency';
Expand All @@ -27,6 +28,7 @@ const stepRules: LineRule[] = [
lintAlgorithmLineStyle,
lintAlgorithmStepNumbering,
lintAlgorithmStepLabels,
lintEnumCasing,
lintForEachElement,
lintStepAttributes,
lintIfElseConsistency,
Expand Down
29 changes: 29 additions & 0 deletions src/lint/rules/enum-casing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { Reporter } from '../algorithm-error-reporter-type';
import type { OrderedListItemNode } from 'ecmarkdown';
import { offsetToLineAndColumn } from '../../utils';

const ruleId = 'enum-casing';

/*
Checks that ~enum-values~ are kebab-cased.
*/
export default function (
report: Reporter,
step: OrderedListItemNode,
algorithmSource: string,
) {
for (const item of step.contents) {
if (item.name !== 'tilde' || item.contents.length !== 1 || item.contents[0].name !== 'text') {
continue;
}
const text = item.contents[0];
if (/[A-Z ]/.test(text.contents)) {
const location = offsetToLineAndColumn(algorithmSource, text.location.start.offset);
report({
ruleId,
message: 'enum values should be kebab-cased',
...location,
});
}
}
}
55 changes: 55 additions & 0 deletions test/lint-algorithms.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,61 @@ describe('linting algorithms', () => {
});
});

describe('kebab-case enums', () => {
const ruleId = 'enum-casing';
it('rejects various other casings', async () => {
await assertLint(
positioned`
<emu-alg>
1. Do something with ~${M}aValue~.
</emu-alg>`,
{
ruleId,
nodeType,
message: 'enum values should be kebab-cased',
}
);

await assertLint(
positioned`
<emu-alg>
1. Do something with ~${M}ADifferentValue~.
</emu-alg>`,
{
ruleId,
nodeType,
message: 'enum values should be kebab-cased',
}
);

await assertLint(
positioned`
<emu-alg>
1. Do something with ~${M}a value with spaces~.
</emu-alg>`,
{
ruleId,
nodeType,
message: 'enum values should be kebab-cased',
}
);
});

it('negative', async () => {
await assertLintFree(`
<emu-alg>
1. Do something with ~a-kebab-cased-value~.
</emu-alg>
`);

await assertLintFree(`
<emu-alg>
1. Do something with ~numeric-value-32~.
</emu-alg>
`);
});
});

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

0 comments on commit 5293b7f

Please sign in to comment.