Skip to content

Commit

Permalink
Add strict to tsconfig.json
Browse files Browse the repository at this point in the history
wooorm committed Jul 22, 2021
1 parent 0334df6 commit 7d6751a
Showing 3 changed files with 44 additions and 6 deletions.
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ const own = {}.hasOwnProperty
/**
* @param {Node} tree
* @param {PhrasesList|PhrasesMap} phrases
* @param {Handler} [handler]
* @param {Handler} handler
* @param {AllowApostrophes|SearchOptions} [options=false]
*/
export function search(tree, phrases, handler, options) {
@@ -49,7 +49,7 @@ export function search(tree, phrases, handler, options) {
throw new TypeError('Expected object for phrases')
}

if ('length' in phrases) {
if (Array.isArray(phrases)) {
while (++index < phrases.length) {
handlePhrase(phrases[index])
}
@@ -112,7 +112,11 @@ export function search(tree, phrases, handler, options) {
* @type {Visitor}
*/
function visitor(node, position, parent) {
if (!config.allowLiterals && isLiteral(parent, position)) {
if (
!parent ||
position === null ||
(!config.allowLiterals && isLiteral(parent, position))
) {
return
}

37 changes: 35 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -80,7 +80,7 @@ const tree = {
test('search(tree, patterns, handle)', (t) => {
t.throws(
() => {
// @ts-ignore runtime.
// @ts-expect-error runtime.
search()
},
/Error: Expected node/,
@@ -89,7 +89,7 @@ test('search(tree, patterns, handle)', (t) => {

t.throws(
() => {
// @ts-ignore runtime.
// @ts-expect-error runtime.
search(tree)
},
/Error: Expected object for phrases/,
@@ -150,6 +150,7 @@ test('search(tree, patterns, handle)', (t) => {
})

t.doesNotThrow(() => {
// @ts-expect-error: hush.
search(tree, ['or that'])
}, 'shouldn’t include non-word and non-white-space nodes')

@@ -174,156 +175,188 @@ test('search(tree, patterns, handle)', (t) => {
* is provided the tree contains “hell” but not “he’ll”
* or “he'll”. */
t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['hell'])
}, 'should find non-apostrophe words when `allowApostrophes` is absent')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['he’ll'])
}, 'should find smart apostrophe words when `allowApostrophes` is absent')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ["he'll"])
}, 'should find dumb apostrophe words when `allowApostrophes` is absent')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['hell'], null, true)
}, 'should find non-apostrophe words when `allowApostrophes` is true')

t.doesNotThrow(() => {
// @ts-expect-error: hush.
search(tree, ['he’ll'], null, true)
}, 'shouldn’t find smart apostrophe words when `allowApostrophes` is true')

t.doesNotThrow(() => {
// @ts-expect-error: hush.
search(tree, ["he'll"], null, true)
}, 'shouldn’t find dumb apostrophe words when `allowApostrophes` is true')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['hell'], null, false)
}, 'should find non-apostrophe words when `allowApostrophes` is false')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['he’ll'], null, false)
}, 'should find smart apostrophe words when `allowApostrophes` is false')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ["he'll"], null, false)
}, 'should find dumb apostrophe words when `allowApostrophes` is false')

/* The tree contains “selfservice” but not “self-service” */

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['selfservice'])
}, 'should find non-dash words when `allowDashes` is absent and `allowApostrophes` is absent')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['self-service'])
}, 'should find dash words when `allowDashes` is absent and `allowApostrophes` is absent')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['selfservice'], null, false)
}, 'should find non-dash words when `allowDashes` is absent and `allowApostrophes` is false')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['self-service'], null, false)
}, 'should find dash words when `allowDashes` is absent and `allowApostrophes` is false')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['selfservice'], null, true)
}, 'should find non-dash words when `allowDashes` is absent and `allowApostrophes` is true')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['self-service'], null, true)
}, 'should find dash words when `allowDashes` is absent and `allowApostrophes` is true')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['selfservice'], null, {allowDashes: true})
}, 'should find non-dash words when `allowDashes` is true')

t.doesNotThrow(() => {
// @ts-expect-error: hush.
search(tree, ['self-service'], null, {allowDashes: true})
}, 'shouldn’t find dash words when `allowDashes` is true')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['selfservice'], null, {allowDashes: false})
}, 'should find non-dash words when `allowDashes` is false')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['self-service'], null, {allowDashes: false})
}, 'should find dash words when `allowDashes` is false')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['selfservice'], null, {
allowApostrophes: false,
allowDashes: true
})
}, 'should find non-dash words when `allowDashes` is true and `allowApostrophes` is false')

t.doesNotThrow(() => {
// @ts-expect-error: hush.
search(tree, ['self-service'], null, {
allowApostrophes: false,
allowDashes: true
})
}, 'shouldn’t find dash words when `allowDashes` is true and `allowApostrophes` is false')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['selfservice'], null, {
allowApostrophes: false,
allowDashes: false
})
}, 'should find non-dash words when `allowDashes` is false and `allowApostrophes` is false')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['self-service'], null, {
allowApostrophes: false,
allowDashes: false
})
}, 'should find dash words when `allowDashes` is false and `allowApostrophes` is false')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['selfservice'], null, {
allowApostrophes: true,
allowDashes: true
})
}, 'should find non-dash words when `allowDashes` is true and `allowApostrophes` is true')

t.doesNotThrow(() => {
// @ts-expect-error: hush.
search(tree, ['self-service'], null, {
allowApostrophes: true,
allowDashes: true
})
}, 'shouldn’t find dash words when `allowDashes` is true and `allowApostrophes` is true')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['selfservice'], null, {
allowApostrophes: true,
allowDashes: false
})
}, 'should find non-dash words when `allowDashes` is false and `allowApostrophes` is true')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['self-service'], null, {
allowApostrophes: true,
allowDashes: false
})
}, 'should find dash words when `allowDashes` is false and `allowApostrophes` is true')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['this * selfservice'])
}, 'should support wild cards (#1)')

t.doesNotThrow(() => {
// @ts-expect-error: hush.
search(tree, ['that * selfservice'])
}, 'should support wild cards (#2)')

t.throws(() => {
// @ts-expect-error: hush.
search(tree, ['* selfservice'])
}, 'should support wild cards (#3)')

t.doesNotThrow(() => {
// @ts-expect-error: hush.
search(tree, ['* zelfzervice'])
}, 'should support wild cards (#4)')

t.doesNotThrow(() => {
// @ts-expect-error: hush.
search(tree, ['mellow'])
}, 'shouldn’t find literals by default')

3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
"skipLibCheck": true,
"strict": true
}
}

0 comments on commit 7d6751a

Please sign in to comment.