Skip to content

Commit

Permalink
Add strict types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 8, 2021
1 parent adf903e commit b97764e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 35 deletions.
38 changes: 21 additions & 17 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@
const ctx =
typeof globalThis === 'object'
? globalThis
: // @ts-ignore
: // @ts-expect-error
typeof self === 'object'
? // @ts-ignore
? // @ts-expect-error
self
: // @ts-ignore
: // @ts-expect-error
typeof window === 'object'
? // @ts-ignore
? // @ts-expect-error
window
: typeof global === 'object'
? global
Expand All @@ -55,7 +55,7 @@ const ctx =

const restore = capture()

// @ts-ignore Types are incorrect.
// @ts-expect-error Types are incorrect.
ctx.Prism = {manual: true, disableWorkerMessageHandler: true}

/* eslint-disable import/first */
Expand All @@ -64,6 +64,7 @@ ctx.Prism = {manual: true, disableWorkerMessageHandler: true}
// The wrapped non-leaky grammars are loaded instead of Prism’s originals.
import {h} from 'hastscript'
import {parseEntities} from 'parse-entities'
// @ts-expect-error: untyped.
import Prism from 'prismjs/components/prism-core.js'

/* eslint-enable import/first */
Expand All @@ -78,6 +79,7 @@ function Refractor() {}
Refractor.prototype = Prism

/** @type {Refractor} */
// @ts-expect-error: TS is wrong.
export const refractor = new Refractor()

// Create.
Expand All @@ -87,9 +89,9 @@ refractor.alias = alias
refractor.registered = registered
refractor.listLanguages = listLanguages

// @ts-ignore Overwrite Prism.
// @ts-expect-error Overwrite Prism.
refractor.util.encode = encode
// @ts-ignore Overwrite Prism.
// @ts-expect-error Overwrite Prism.
refractor.Token.stringify = stringify

/**
Expand Down Expand Up @@ -123,11 +125,12 @@ function register(syntax) {
function alias(name, alias) {
const languages = refractor.languages
/** @type {Object.<string, string|Array.<string>>} */
let map
let map = {}

if (typeof name === 'string') {
map = {}
map[name] = alias
if (alias) {
map[name] = alias
}
} else {
map = name
}
Expand Down Expand Up @@ -163,15 +166,13 @@ function highlight(value, nameOrGrammar) {

/** @type {Grammar} */
let grammar
/** @type {string} */
/** @type {string|undefined} */
let name

// `name` is a grammar object.
if (nameOrGrammar && typeof nameOrGrammar === 'object') {
grammar = nameOrGrammar
name = null
} else {
// @ts-ignore Assume string.
name = nameOrGrammar

if (typeof name !== 'string') {
Expand Down Expand Up @@ -252,7 +253,7 @@ function stringify(value, language) {
value[index] !== null &&
value[index] !== undefined
) {
// @ts-ignore Assume no sub-arrays.
// @ts-expect-error Assume no sub-arrays.
result.push(stringify(value[index], language))
}
}
Expand All @@ -276,10 +277,10 @@ function stringify(value, language) {
)
}

// @ts-ignore Prism.
// @ts-expect-error Prism.
refractor.hooks.run('wrap', env)

// @ts-ignore Hush, it’s fine.
// @ts-expect-error Hush, it’s fine.
return h(
env.tag + '.' + env.classes.join('.'),
attributes(env.attributes),
Expand Down Expand Up @@ -317,6 +318,7 @@ function attributes(attrs) {
* @returns {() => void}
*/
function capture() {
/** @type {boolean|undefined} */
let defined = 'Prism' in ctx
/* c8 ignore next */
let current = defined ? ctx.Prism : undefined
Expand All @@ -329,9 +331,11 @@ function capture() {
function restore() {
/* istanbul ignore else - Clean leaks after Prism. */
if (defined) {
// @ts-expect-error: hush.
ctx.Prism = current
/* c8 ignore next 3 */
/* c8 ignore next 4 */
} else {
// @ts-expect-error: hush.
delete ctx.Prism
}

Expand Down
1 change: 1 addition & 0 deletions script/data.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'node:fs'
import path from 'node:path'
// @ts-expect-error: untyped.
import getLoader from 'prismjs/dependencies.js'

/** @type {{languages: Object.<string, unknown>}} */
Expand Down
22 changes: 10 additions & 12 deletions script/languages.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,16 @@ const components = JSON.parse(

const prefix = 'refractor-'

async.map(all, generate, done)

/**
* @param {Error} error
* @param {Array.<undefined>} [results]
*/
function done(error, results) {
async.map(all, generate, (error, results) => {
bail(error)
console.log(chalk.green('✓') + ' wrote ' + results.length + ' languages')
}
console.log(
chalk.green('✓') + ' wrote ' + (results || []).length + ' languages'
)
})

/**
* @param {string} name
* @param {(error: Error) => void} callback
* @param {(error: Error|null) => void} callback
*/
function generate(name, callback) {
const id = toId(name)
Expand All @@ -56,6 +52,8 @@ function generate(name, callback) {
typeof info.alias === 'string' ? [info.alias] : info.alias || []
).sort(alphaSort())

/** @type {string} */
// @ts-expect-error: TS is wrong.
const doc = babel.transformSync(String(buf), {
plugins: [fixWrapHook]
}).code
Expand Down Expand Up @@ -124,7 +122,7 @@ function fixWrapHook() {
callee.matchesPattern('Prism.highlight') &&
path.get('left').matchesPattern('env.content')
) {
// @ts-ignore Mutate.
// @ts-expect-error Mutate.
path.get('left').node.ignoreValueSuffix = true
}
}
Expand All @@ -135,7 +133,7 @@ function fixWrapHook() {
enter(path) {
if (
this.inWrapHook &&
// @ts-ignore Mutate.
// @ts-expect-error Mutate.
!path.node.ignoreValueSuffix &&
path.matchesPattern('env.content')
) {
Expand Down
1 change: 1 addition & 0 deletions script/to-id.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-expect-error: untyped.
import isKeyword from 'is-keyword'

/**
Expand Down
12 changes: 7 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @typedef {import('../lib/core.js').Syntax} Syntax
* @typedef {import('hast').Node} Node
* @typedef {import('prismjs')} Prism
*/

import fs from 'node:fs'
Expand All @@ -14,7 +15,7 @@ import {refractor} from '../lib/all.js'
test('.highlight(value, language)', (t) => {
t.throws(
() => {
// @ts-ignore runtime.
// @ts-expect-error runtime.
refractor.highlight()
},
/ Expected `string` for `value`, got `undefined`/,
Expand All @@ -23,7 +24,7 @@ test('.highlight(value, language)', (t) => {

t.throws(
() => {
// @ts-ignore runtime.
// @ts-expect-error runtime.
refractor.highlight('')
},
/Expected `string` for `name`, got `undefined`/,
Expand All @@ -32,7 +33,7 @@ test('.highlight(value, language)', (t) => {

t.throws(
() => {
// @ts-ignore runtime.
// @ts-expect-error runtime.
refractor.highlight(true, 'js')
},
/Expected `string` for `value`, got `true`/,
Expand Down Expand Up @@ -65,7 +66,7 @@ test('.highlight(value, language)', (t) => {
test('.register(grammar)', (t) => {
t.throws(
() => {
// @ts-ignore runtime.
// @ts-expect-error runtime.
refractor.register()
},
/Expected `function` for `syntax`, got `undefined`/,
Expand All @@ -78,7 +79,7 @@ test('.register(grammar)', (t) => {
test('.registered(language)', (t) => {
t.throws(
() => {
// @ts-ignore runtime.
// @ts-expect-error runtime.
refractor.registered()
},
/Expected `string` for `language`, got `undefined`/,
Expand Down Expand Up @@ -197,6 +198,7 @@ test('listLanguages', (t) => {
'should return a list of registered languages'
)

// @ts-expect-error: hush.
refractor.register(grammar)

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

0 comments on commit b97764e

Please sign in to comment.