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

add lint for header format #205

Merged
merged 8 commits into from
May 21, 2020
Merged
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
113 changes: 113 additions & 0 deletions src/lint/collect-header-diagnostics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import type { LintingError } from './algorithm-error-reporter-type';

import { getLocation, indexWithinElementToTrueLocation } from './utils';

const ruleId = 'header-format';

export function collectHeaderDiagnostics(
dom: any,
headers: { element: Element; contents: string }[]
) {
let lintingErrors: LintingError[] = [];

for (let { element, contents } of headers) {
if (!/\(.*\)$/.test(contents) || / Operator \( `[^`]+` \)$/.test(contents)) {
continue;
}

let name = contents.substring(0, contents.indexOf('('));
let params = contents.substring(contents.indexOf('(') + 1, contents.length - 1);

if (!/[\S] $/.test(name)) {
let { line, column } = indexWithinElementToTrueLocation(
getLocation(dom, element),
contents,
name.length - 1
);
lintingErrors.push({
ruleId,
nodeType: element.tagName,
line,
column,
message: 'expected header to have a single space before the argument list',
});
}

let nameMatches = [
// Runtime Semantics: Foo
/^(Runtime|Static) Semantics: [A-Z][A-Za-z0-9/]*\s*$/,

// Number::foo
/^[A-Z][A-Za-z0-9]*::[a-z][A-Za-z0-9]*\s*$/,

// [[GetOwnProperty]]
/^\[\[[A-Z][A-Za-z0-9]*\]\]\s*$/,

// _NativeError_
/^_[A-Z][A-Za-z0-9]*_\s*$/,

// CreateForInIterator
// Object.fromEntries
// Array.prototype [ @@iterator ]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should support Array.prototype [ %Symbol.iterator% ] as well, since we're moving to that form.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer that we disallow it now and make it legal as part of the PR which changes the format, so we are never in a position where both are accepted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that's in a different repo. There's a currently open PR. How do you expect that to work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd change it here, bump it, release it, and include the change in the upstream PR as part of the process of merging that PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love it but I'll leave it to @ljharb to fight this fight.

/^[A-Za-z][A-Za-z0-9]*(\.[A-Za-z][A-Za-z0-9]*)*( \[ @@[a-z][a-zA-Z]+ \])?\s*$/,

// %ForInIteratorPrototype%.next
// %TypedArray%.prototype [ @@iterator ]
/^%[A-Z][A-Za-z0-9]*%(\.[A-Za-z][A-Za-z0-9]*)*( \[ @@[a-z][a-zA-Z]+ \])?\s*$/,
].some(r => r.test(name));

if (!nameMatches) {
let { line, column } = indexWithinElementToTrueLocation(
getLocation(dom, element),
contents,
0
);
lintingErrors.push({
ruleId,
nodeType: element.tagName,
line,
column,
message: `expected operation to have a name like 'Example', 'Runtime Semantics: Foo', 'Example.prop', etc, but found ${JSON.stringify(
name
)}`,
});
}

let paramsMatches =
params.match(/\[/g)?.length === params.match(/\]/g)?.length &&
[
// Foo ( )
/^ $/,

// Object ( . . . )
/^ \. \. \. $/,

// String.raw ( _template_, ..._substitutions_ )
/^ (_[A-Za-z0-9]+_, )*\.\.\._[A-Za-z0-9]+_ $/,

// Function ( _p1_, _p2_, … , _pn_, _body_ )
/^ (_[A-Za-z0-9]+_, )*… (, _[A-Za-z0-9]+_)+ $/,

// Example ( _foo_ , [ _bar_ ] )
// Example ( [ _foo_ ] )
/^ (\[ )?_[A-Za-z0-9]+_(, _[A-Za-z0-9]+_)*( \[ , _[A-Za-z0-9]+_(, _[A-Za-z0-9]+_)*)*( \])* $/,
].some(r => r.test(params));

if (!paramsMatches) {
let { line, column } = indexWithinElementToTrueLocation(
getLocation(dom, element),
contents,
name.length
);
lintingErrors.push({
ruleId,
nodeType: element.tagName,
line,
column,
message: `expected parameter list to look like '( _a_, [ , _b_ ] )', '( _foo_, _bar_, ..._baz_ )', '( _foo_, … , _bar_ )', or '( . . . )'`,
});
}
}

return lintingErrors;
}
4 changes: 3 additions & 1 deletion src/lint/collect-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Node as EcmarkdownNode } from 'ecmarkdown';
import { getLocation } from './utils';

export function collectNodes(sourceText: string, dom: any, document: Document) {
let headers: { element: Element; contents: string }[] = [];
let mainGrammar: { element: Element; source: string }[] = [];
let sdos: { grammar: Element; alg: Element }[] = [];
let earlyErrors: { grammar: Element; lists: HTMLUListElement[] }[] = [];
Expand All @@ -28,6 +29,7 @@ export function collectNodes(sourceText: string, dom: any, document: Document) {
let first = node.firstElementChild;
if (first !== null && first.nodeName === 'H1') {
let title = first.textContent ?? '';
headers.push({ element: first, contents: title });
if (title.trim() === 'Static Semantics: Early Errors') {
let grammar = null;
let lists: HTMLUListElement[] = [];
Expand Down Expand Up @@ -101,5 +103,5 @@ export function collectNodes(sourceText: string, dom: any, document: Document) {
}
visitCurrentNode();

return { mainGrammar, sdos, earlyErrors, algorithms };
return { mainGrammar, headers, sdos, earlyErrors, algorithms };
}
9 changes: 8 additions & 1 deletion src/lint/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { emit } from 'ecmarkdown';
import { collectNodes } from './collect-nodes';
import { collectGrammarDiagnostics } from './collect-grammar-diagnostics';
import { collectAlgorithmDiagnostics } from './collect-algorithm-diagnostics';
import { collectHeaderDiagnostics } from './collect-header-diagnostics';
import type { Reporter } from './algorithm-error-reporter-type';

/*
Expand All @@ -16,7 +17,11 @@ There's more to do:
https://github.com/tc39/ecmarkup/issues/173
*/
export function lint(report: Reporter, sourceText: string, dom: any, document: Document) {
let { mainGrammar, sdos, earlyErrors, algorithms } = collectNodes(sourceText, dom, document);
let { mainGrammar, headers, sdos, earlyErrors, algorithms } = collectNodes(
sourceText,
dom,
document
);

let { grammar, lintingErrors } = collectGrammarDiagnostics(
dom,
Expand All @@ -28,6 +33,8 @@ export function lint(report: Reporter, sourceText: string, dom: any, document: D

lintingErrors.push(...collectAlgorithmDiagnostics(dom, sourceText, algorithms));

lintingErrors.push(...collectHeaderDiagnostics(dom, headers));

if (lintingErrors.length > 0) {
report(lintingErrors, sourceText);
return;
Expand Down
28 changes: 28 additions & 0 deletions src/lint/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,34 @@ import type {

import { Grammar as GrammarFile, SyntaxKind } from 'grammarkdown';

export function indexWithinElementToTrueLocation(
elementLoc: ReturnType<typeof getLocation>,
string: string,
index: number
) {
let headerLines = string.split('\n');
let headerLine = 0;
let seen = 0;
while (true) {
if (seen + headerLines[headerLine].length >= index) {
break;
}
seen += headerLines[headerLine].length + 1; // +1 for the '\n'
++headerLine;
}
let headerColumn = index - seen;

let line = elementLoc.startTag.line + headerLine;
let column =
headerLine === 0
? elementLoc.startTag.col +
(elementLoc.startTag.endOffset - elementLoc.startTag.startOffset) +
headerColumn
: headerColumn + 1;

return { line, column };
}

export function grammarkdownLocationToTrueLocation(
elementLoc: ReturnType<typeof getLocation>,
gmdLine: number,
Expand Down
112 changes: 111 additions & 1 deletion test/lint.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

let { assertLint, positioned, lintLocationMarker: M } = require('./lint-helpers');
let { assertLint, assertLintFree, positioned, lintLocationMarker: M } = require('./lint-helpers');

describe('linting whole program', function () {
describe('grammar validity', function () {
Expand Down Expand Up @@ -110,4 +110,114 @@ describe('linting whole program', function () {
);
});
});

describe('header format', function () {
it('name format', async function () {
await assertLint(
positioned`
<emu-clause id="foo">
<h1>${M}something: ( )</h1>
`,
{
ruleId: 'header-format',
nodeType: 'H1',
message:
"expected operation to have a name like 'Example', 'Runtime Semantics: Foo', 'Example.prop', etc, but found \"something: \"",
}
);
});

it('spacing', async function () {
await assertLint(
positioned`
<emu-clause id="foo">
<h1>Exampl${M}e( )</h1>
`,
{
ruleId: 'header-format',
nodeType: 'H1',
message: 'expected header to have a single space before the argument list',
}
);

await assertLint(
positioned`
<emu-clause id="foo">
<h1>Example ${M} ( )</h1>
`,
{
ruleId: 'header-format',
nodeType: 'H1',
message: 'expected header to have a single space before the argument list',
}
);
});

it('arg format', async function () {
await assertLint(
positioned`
<emu-clause id="foo">
<h1>Example ${M}(_a_)</h1>
`,
{
ruleId: 'header-format',
nodeType: 'H1',
message:
"expected parameter list to look like '( _a_, [ , _b_ ] )', '( _foo_, _bar_, ..._baz_ )', '( _foo_, … , _bar_ )', or '( . . . )'",
}
);
});

it('legal names', async function () {
await assertLintFree(`
<emu-clause id="foo">
<h1>Example ( )</h1>
</emu-clause>
<emu-clause id="foo">
<h1>Runtime Semantics: Example ( )</h1>
</emu-clause>
<emu-clause id="foo">
<h1>The * Operator ( \`*\` )</h1>
</emu-clause>
<emu-clause id="foo">
<h1>Number::example ( )</h1>
</emu-clause>
<emu-clause id="foo">
<h1>[[Example]] ( )</h1>
</emu-clause>
<emu-clause id="foo">
<h1>_Example_ ( )</h1>
</emu-clause>
<emu-clause id="foo">
<h1>%Foo%.bar [ @@iterator ] ( )</h1>
</emu-clause>
`);
});

it('legal argument lists', async function () {
await assertLintFree(`
<emu-clause id="foo">
<h1>Example ( )</h1>
</emu-clause>
<emu-clause id="foo">
<h1>Example ( _foo_ )</h1>
</emu-clause>
<emu-clause id="foo">
<h1>Example ( [ _foo_ ] )</h1>
</emu-clause>
<emu-clause id="foo">
<h1>Date ( _year_, _month_ [ , _date_ [ , _hours_ [ , _minutes_ [ , _seconds_ [ , _ms_ ] ] ] ] ] )</h1>
</emu-clause>
<emu-clause id="foo">
<h1>Object ( . . . )</h1>
</emu-clause>
<emu-clause id="foo">
<h1>String.raw ( _template_, ..._substitutions_ )</h1>
</emu-clause>
<emu-clause id="foo">
<h1>Function ( _p1_, _p2_, &hellip; , _pn_, _body_ )</h1>
</emu-clause>
`);
});
});
});