Skip to content

Commit

Permalink
Change to improve message
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 11, 2023
1 parent 7c1d205 commit 56e5957
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 64 deletions.
74 changes: 29 additions & 45 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,74 +241,58 @@ function all(tree, file, state) {
}

if (!correct) {
let reason = quotation(actual, '`') + ' is misspelt'
const cached = state.cache.get(actual)
/** @type {ReadonlyArray<string>} */
let expected = []
let suggestions = state.cache.get(actual)

// Suggestions are very slow, so cache them (spelling mistakes other than
// typos often occur multiple times).
if (cached) {
expected = cached
reason = concatPrefixToSuggestions(reason, expected)
} else {
if (!suggestions) {
if (state.count === state.max) {
file.message(
'Too many misspellings; no further spell suggestions are given',
const message = file.info(
'No longer generating suggestions to improve performance',
{
ancestors: [parent, node],
place: node.position,
ruleId: 'overflow',
source: 'retext-spell'
}
)
message.note = 'To keep on suggesting, increase `options.max`.'
}

state.count++
suggestions =
state.count < state.max
? // @ts-expect-error: to do: type nspell.
/** @type {Array<string>} */ (state.checker.suggest(actual))
: []

if (state.count < state.max) {
// @ts-expect-error: to do: type nspell.
expected = state.checker.suggest(actual)
state.count++
state.cache.set(actual, suggestions)
}

if (expected.length > 0) {
reason = concatPrefixToSuggestions(reason, expected)
}
}
let extra = ''

state.cache.set(actual, expected)
if (suggestions.length > 0) {
extra =
', expected for example ' +
quotation([...suggestions], '`').join(', ')
}

const message = file.message(reason, {
place: node.position,
ruleId: actual.toLowerCase().replace(/\W+/, '-'),
source: 'retext-spell'
})
const message = file.message(
'Unexpected unknown word `' + actual + '`' + extra,
{
ancestors: [parent, node],
place: node.position,
ruleId: actual.toLowerCase().replace(/\W+/, '-'),
source: 'retext-spell'
}
)

message.actual = actual
message.expected = [...expected]
message.expected = [...suggestions]
message.url = 'https://github.com/retextjs/retext-spell#readme'
}
})

/**
* Concatenate the formatted suggestions to a given prefix
*
* @param {string} prefix
* Reason.
* @param {ReadonlyArray<string>} suggestions
* Suggestions.
* @returns {string}
* Message.
*/
function concatPrefixToSuggestions(prefix, suggestions) {
return (
prefix +
'; did you mean ' +
// To do: `quotation` should support `ReadonlyArray`.
quotation([...suggestions], '`').join(', ') +
'?'
)
}

/**
* Check if a word is irrelevant.
*
Expand Down
40 changes: 21 additions & 19 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ test('retextSpell', async function (t) {
expected: ['color', 'dolor'],
fatal: false,
line: 1,
message: '`kolor` is misspelt; did you mean `color`, `dolor`?',
message:
'Unexpected unknown word `kolor`, expected for example `color`, `dolor`',
name: '1:1-1:6',
place: {
start: {column: 1, line: 1, offset: 0},
end: {column: 6, line: 1, offset: 5}
},
reason: '`kolor` is misspelt; did you mean `color`, `dolor`?',
reason:
'Unexpected unknown word `kolor`, expected for example `color`, `dolor`',
ruleId: 'kolor',
source: 'retext-spell',
url: 'https://github.com/retextjs/retext-spell#readme'
Expand All @@ -75,7 +77,7 @@ test('retextSpell', async function (t) {
const file = await retext().use(retextSpell, dictionaryEn).process('kolor')

assert.deepEqual(file.messages.map(String), [
'1:1-1:6: `kolor` is misspelt; did you mean `color`, `dolor`?'
'1:1-1:6: Unexpected unknown word `kolor`, expected for example `color`, `dolor`'
])
})

Expand All @@ -85,9 +87,9 @@ test('retextSpell', async function (t) {
.process('kolor and kolor and kolor')

assert.deepEqual(file.messages.map(String), [
'1:1-1:6: `kolor` is misspelt; did you mean `color`, `dolor`?',
'1:11-1:16: `kolor` is misspelt; did you mean `color`, `dolor`?',
'1:21-1:26: `kolor` is misspelt; did you mean `color`, `dolor`?'
'1:1-1:6: Unexpected unknown word `kolor`, expected for example `color`, `dolor`',
'1:11-1:16: Unexpected unknown word `kolor`, expected for example `color`, `dolor`',
'1:21-1:26: Unexpected unknown word `kolor`, expected for example `color`, `dolor`'
])
})

Expand All @@ -109,11 +111,11 @@ test('retextSpell', async function (t) {
.process('Soem useles mispelt documeant')

assert.deepEqual(file.messages.map(String), [
'1:1-1:5: `Soem` is misspelt',
'1:6-1:12: Too many misspellings; no further spell suggestions are given',
'1:6-1:12: `useles` is misspelt',
'1:13-1:20: `mispelt` is misspelt',
'1:21-1:30: `documeant` is misspelt'
'1:1-1:5: Unexpected unknown word `Soem`, expected for example `Seem`, `Stem`, `Sum`, `Poem`, `Some`',
'1:6-1:12: No longer generating suggestions to improve performance',
'1:6-1:12: Unexpected unknown word `useles`',
'1:13-1:20: Unexpected unknown word `mispelt`',
'1:21-1:30: Unexpected unknown word `documeant`'
])
})

Expand All @@ -133,7 +135,7 @@ test('retextSpell', async function (t) {
.process('“kolor”')

assert.deepEqual(file.messages.map(String), [
'1:2-1:7: `kolor` is misspelt; did you mean `color`, `dolor`?'
'1:2-1:7: Unexpected unknown word `kolor`, expected for example `color`, `dolor`'
])
}
)
Expand All @@ -146,7 +148,7 @@ test('retextSpell', async function (t) {
.process('wrongely-spelled-word')

assert.deepEqual(file.messages.map(String), [
'1:1-1:22: `wrongely-spelled-word` is misspelt'
'1:1-1:22: Unexpected unknown word `wrongely-spelled-word`'
])
}
)
Expand Down Expand Up @@ -189,8 +191,8 @@ test('retextSpell', async function (t) {
.process('123456 alpha 3.14')

assert.deepEqual(file.messages.map(String), [
'1:1-1:7: `123456` is misspelt; did you mean `12th456`, `12th3456`, `12th56`?',
'1:14-1:18: `3.14` is misspelt; did you mean `3.14th`?'
'1:1-1:7: Unexpected unknown word `123456`, expected for example `12th456`, `12th3456`, `12th56`',
'1:14-1:18: Unexpected unknown word `3.14`, expected for example `3.14th`'
])
}
)
Expand All @@ -201,7 +203,7 @@ test('retextSpell', async function (t) {
.process('768x1024')

assert.deepEqual(file.messages.map(String), [
'1:1-1:9: `768x1024` is misspelt; did you mean `76th8x1024`, `76thx1024`?'
'1:1-1:9: Unexpected unknown word `768x1024`, expected for example `76th8x1024`, `76thx1024`'
])
})

Expand All @@ -223,8 +225,8 @@ test('retextSpell', async function (t) {
.process('color coloor colour')

assert.deepEqual(file.messages.map(String), [
'1:1-1:6: `color` is misspelt; did you mean `dolor`, `colors`, `colon`, `colour`, `Colo`?',
'1:7-1:13: `coloor` is misspelt; did you mean `colour`?'
'1:1-1:6: Unexpected unknown word `color`, expected for example `dolor`, `colors`, `colon`, `colour`, `Colo`',
'1:7-1:13: Unexpected unknown word `coloor`, expected for example `colour`'
])
})

Expand All @@ -233,7 +235,7 @@ test('retextSpell', async function (t) {
.use(retextSpell, {dictionary: dictionaryEn})
.process('Pages ⚡️')

assert.match(String(file.messages), /`️` is misspelt/)
assert.match(String(file.messages), /Unexpected unknown word `️`,/)
})

await t.test('should integrate w/ `retext-emoji` (2)', async function () {
Expand Down

0 comments on commit 56e5957

Please sign in to comment.