-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
175 additions
and
19 deletions.
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
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,60 @@ | ||
import type { LintingError } from './algorithm-error-reporter-type'; | ||
|
||
import { offsetToLineAndColumn } from './utils'; | ||
|
||
let ruleId = 'spelling'; | ||
|
||
// Note that these will be composed, so cannot contain backreferences | ||
let matchers = [ | ||
{ | ||
pattern: /\*this\* object/giu, | ||
message: 'Prefer "the *this* value"', | ||
}, | ||
{ | ||
pattern: /1's complement/giu, | ||
message: 'Prefer "one\'s complement"', | ||
}, | ||
{ | ||
pattern: /2's complement/giu, | ||
message: 'Prefer "two\'s complement"', | ||
}, | ||
{ | ||
pattern: /\*0\*/gu, | ||
message: 'The Number value 0 should be written "*+0*", to unambiguously exclude "*-0*"', | ||
}, | ||
{ | ||
pattern: /behavior/giu, | ||
message: 'ECMA-262 uses UK spelling ("behaviour")', | ||
}, | ||
]; | ||
|
||
export function collectSpellingDiagnostics(sourceText: string) { | ||
let composed = new RegExp(matchers.map(m => `(?:${m.pattern.source})`).join('|'), 'u'); | ||
|
||
// The usual case will be to have no errors, so we have a fast path for that case. | ||
// We only fall back to slower individual tests if there is at least one error. | ||
if (composed.test(sourceText)) { | ||
let errors: LintingError[] = []; | ||
for (let { pattern, message } of matchers) { | ||
let match = pattern.exec(sourceText); | ||
while (match !== null) { | ||
let { line, column } = offsetToLineAndColumn(sourceText, match.index); | ||
errors.push({ | ||
ruleId, | ||
nodeType: 'text', | ||
line, | ||
column, | ||
message, | ||
}); | ||
match = pattern.exec(sourceText); | ||
} | ||
} | ||
if (errors.length === 0) { | ||
throw new Error( | ||
'Ecmarkup has a bug: the spell checker reported an error, but could find one. Please report this at https://github.com/tc39/ecmarkup/issues/new.' | ||
); | ||
} | ||
return errors; | ||
} | ||
return []; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
'use strict'; | ||
|
||
let { assertLint, assertLintFree, positioned, lintLocationMarker: M } = require('./lint-helpers'); | ||
|
||
describe('spelling', function () { | ||
it('*this* object', async function () { | ||
await assertLint( | ||
positioned` | ||
<p>If the ${M}*this* object ...</p> | ||
`, | ||
{ | ||
ruleId: 'spelling', | ||
nodeType: 'text', | ||
message: 'Prefer "the *this* value"', | ||
} | ||
); | ||
}); | ||
|
||
it("1's complement", async function () { | ||
await assertLint( | ||
positioned` | ||
<p>It returns the ${M}1's complement of _x_.</p> | ||
`, | ||
{ | ||
ruleId: 'spelling', | ||
nodeType: 'text', | ||
message: 'Prefer "one\'s complement"', | ||
} | ||
); | ||
}); | ||
|
||
it("2's complement", async function () { | ||
await assertLint( | ||
positioned` | ||
<p>BigInts act as ${M}2's complement binary strings</p> | ||
`, | ||
{ | ||
ruleId: 'spelling', | ||
nodeType: 'text', | ||
message: 'Prefer "two\'s complement"', | ||
} | ||
); | ||
}); | ||
|
||
it('*0*', async function () { | ||
await assertLint( | ||
positioned` | ||
<emu-alg>1. If _x_ is ${M}*0*, then foo.</emu-alg> | ||
`, | ||
{ | ||
ruleId: 'spelling', | ||
nodeType: 'text', | ||
message: 'The Number value 0 should be written "*+0*", to unambiguously exclude "*-0*"', | ||
} | ||
); | ||
}); | ||
|
||
it('behavior', async function () { | ||
await assertLint( | ||
positioned` | ||
<p>Most hosts will be able to simply define HostGetImportMetaProperties, and leave HostFinalizeImportMeta with its default ${M}behavior.</p> | ||
`, | ||
{ | ||
ruleId: 'spelling', | ||
nodeType: 'text', | ||
message: 'ECMA-262 uses UK spelling ("behaviour")', | ||
} | ||
); | ||
}); | ||
|
||
it('negative', async function () { | ||
await assertLintFree(` | ||
<p> | ||
the *this* value | ||
one's complement | ||
two's complement | ||
*+0* | ||
*-0* | ||
behaviour | ||
</p> | ||
`); | ||
}); | ||
}); |