Skip to content

Commit

Permalink
distinguish between attribute errors and attribute value errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkot committed Jan 9, 2021
1 parent 48e6800 commit aad188d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class Builder {
if (nodeId !== null) {
if (spec.nodeIds.has(nodeId)) {
spec.warn({
type: 'attr',
type: 'attr-value',
attr: 'id',
ruleId: 'duplicate-id',
message: `<${node.tagName.toLowerCase()}> has duplicate id ${JSON.stringify(nodeId)}`,
Expand Down
2 changes: 1 addition & 1 deletion src/Note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default class Note extends Builder {
label = "Editor's Note";
} else {
this.spec.warn({
type: 'attr',
type: 'attr-value',
attr: 'type',
ruleId: 'invalid-note',
message: `unknown note type ${JSON.stringify(this.type)}`,
Expand Down
21 changes: 19 additions & 2 deletions src/Spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ export type Warning =
ruleId: string;
message: string;
}
| {
type: 'attr-value';
node: Element;
attr: string;
ruleId: string;
message: string;
}
| {
type: 'contents';
node: Text | Element;
Expand Down Expand Up @@ -158,6 +165,16 @@ function wrapWarn(source: string, spec: Spec, warn: (err: EcmarkupError) => void
}
break;
case 'attr': {
let loc = spec.locate(e.node);
if (loc) {
file = loc.file;
source = loc.source;
({ line, column } = utils.attrLocation(source, loc, e.attr));
}
nodeType = e.node.tagName.toLowerCase();
break;
}
case 'attr-value': {
let loc = spec.locate(e.node);
if (loc) {
file = loc.file;
Expand Down Expand Up @@ -924,15 +941,15 @@ export default class Spec {
let targetEntry = this.biblio.byId(target);
if (targetEntry == null) {
this.warn({
type: 'attr',
type: 'attr-value',
attr: 'replaces-step',
ruleId: 'invalid-replacement',
message: `could not find step ${JSON.stringify(target)}`,
node: element,
});
} else if (targetEntry.type !== 'step') {
this.warn({
type: 'attr',
type: 'attr-value',
attr: 'replaces-step',
ruleId: 'invalid-replacement',
message: `expected algorithm to replace a step, not a ${targetEntry.type}`,
Expand Down
8 changes: 4 additions & 4 deletions src/Xref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default class Xref extends Builder {
if (href) {
if (href[0] !== '#') {
spec.warn({
type: 'attr',
type: 'attr-value',
attr: 'href',
ruleId: 'invalid-xref',
message: `xref to anything other than a fragment id is not supported (is ${JSON.stringify(
Expand All @@ -94,7 +94,7 @@ export default class Xref extends Builder {
this.entry = spec.biblio.byId(id);
if (!this.entry) {
spec.warn({
type: 'attr',
type: 'attr-value',
attr: 'href',
ruleId: 'xref-not-found',
message: `can't find clause, production, note or example with id ${JSON.stringify(id)}`,
Expand Down Expand Up @@ -148,7 +148,7 @@ export default class Xref extends Builder {
let namespaceSuffix =
namespace === '<no location>' ? '' : ` in namespace ${JSON.stringify(namespace)}`;
spec.warn({
type: 'attr',
type: 'attr-value',
attr: 'aoid',
ruleId: 'xref-not-found',
message: `can't find abstract op with aoid ${JSON.stringify(aoid)}` + namespaceSuffix,
Expand Down Expand Up @@ -262,7 +262,7 @@ function buildStepLink(spec: Spec, xref: Element, entry: Biblio.StepBiblioEntry)
let applicable = bullets[Math.min(i, 5)];
if (s > applicable.length) {
spec.warn({
type: 'attr',
type: 'attr-value',
ruleId: 'high-step-number',
message: `ecmarkup does not know how to deal with step numbers as high as ${s}; if you need this, open an issue on ecmarkup`,
node: xref,
Expand Down
13 changes: 13 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,19 @@ export function offsetToLineAndColumn(string: string, offset: number) {
return { line: line + 1, column: column + 1 };
}

export function attrLocation(
source: string | undefined,
loc: MarkupData.ElementLocation,
attr: string
) {
let attrLoc = loc.startTag.attrs[attr];
if (attrLoc == null) {
return { line: loc.startTag.line, column: loc.startTag.col };
} else {
return { line: attrLoc.line, column: attrLoc.col };
}
}

export function attrValueLocation(
source: string | undefined,
loc: MarkupData.ElementLocation,
Expand Down
2 changes: 1 addition & 1 deletion test/lint-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('tags', () => {
it('oldid', async () => {
await assertLint(
positioned`
<emu-clause id="foo" oldid="${M}bar">
<emu-clause id="foo" ${M}oldid="bar">
<h1>Example</h1>
</emu-clause>
`,
Expand Down

0 comments on commit aad188d

Please sign in to comment.