-
Notifications
You must be signed in to change notification settings - Fork 65
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e8f02c3
add lint for header format
bakkot 70f4337
factor location logic out
bakkot a6077eb
match multiple spaces after name
bakkot 3962842
relax restriction on methods
bakkot 77b73ce
add sample comments
bakkot a4630e0
element.tagName
bakkot bfaeba8
move location
bakkot a2ab151
Merge branch 'master' into header-format
bakkot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ] | ||
/^[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]+_)+ $/, | ||
michaelficarra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.