Skip to content

Commit

Permalink
feat: improve warning messages
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 20, 2025
1 parent 708e3f2 commit 0f27a20
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 59 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/textmate/grammar-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class GrammarState implements GrammarStateInterface {
lang: this.lang,
theme: this.theme,
themes: this.themes,
scopes: this.scopes,
scopes: this.getScopes(),
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/engine-javascript/test/compare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Execution } from './types'

import { hash as createHash } from 'ohash'
import { describe, expect, it } from 'vitest'
import { createWasmOnigEngine, loadWasm } from '../../engine-oniguruma/src'
import { createOnigurumaEngine, loadWasm } from '../../engine-oniguruma/src'
import { createHighlighterCore } from '../../shiki/src/core'
import { createJavaScriptRegexEngine } from '../src/engine-compile'

Expand Down Expand Up @@ -176,20 +176,20 @@ describe('cases', async () => {
const run = c.c.skip ? it.skip : it
run(c.c.name, async () => {
const engineWasm = createEngineWrapper(
await createWasmOnigEngine(),
await createOnigurumaEngine(),
)
const engineJs = createEngineWrapper(
createJavaScriptRegexEngine({
forgiving: true,
}),
)

const shiki1 = await createHighlighterCore({
using shiki1 = await createHighlighterCore({
langs: c.lang,
themes: [c.theme],
engine: engineWasm,
})
const shiki2 = await createHighlighterCore({
using shiki2 = await createHighlighterCore({
langs: c.lang,
themes: [c.theme],
engine: engineJs,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { createHighlighterCore } from '@shikijs/core'
import js from '@shikijs/langs/js'
import nord from '@shikijs/themes/nord'
import { expect, it } from 'vitest'

// eslint-disable-next-line antfu/no-import-dist
import { wasmBinary } from '../../engine-oniguruma/dist/wasm-inlined.mjs'
import { createHighlighterCore } from '../src/core'
import js from '../src/langs/javascript.mjs'
import nord from '../src/themes/nord.mjs'
import { wasmBinary } from '../dist/wasm-inlined.mjs'
import { createOnigurumaEngine } from '../src/index'

it('wasm', async () => {
const shiki = await createHighlighterCore({
themes: [nord],
langs: [js as any],
loadWasm: {
engine: createOnigurumaEngine({
instantiator: obj => WebAssembly.instantiate(wasmBinary, obj),
},
}),
})

expect(shiki.codeToHtml('1 + 1', { lang: 'javascript', theme: 'nord' }))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { createHighlighterCore } from '@shikijs/core'
import js from '@shikijs/langs/js'
import nord from '@shikijs/themes/nord'
import { createOnigurumaEngine } from 'shiki'
import { expect, it } from 'vitest'

// eslint-disable-next-line antfu/no-import-dist
import { wasmBinary } from '../../engine-oniguruma/dist/wasm-inlined.mjs'
import { createHighlighterCore } from '../src/core'
import js from '../src/langs/javascript.mjs'
import nord from '../src/themes/nord.mjs'
import { wasmBinary } from '../dist/wasm-inlined.mjs'

it('wasm', async () => {
const shiki = await createHighlighterCore({
themes: [nord],
langs: [js as any],
loadWasm: {
engine: createOnigurumaEngine({
default: obj => WebAssembly.instantiate(wasmBinary, obj).then(r => r.instance.exports),
},
}),
})

expect(shiki.codeToHtml('1 + 1', { lang: 'javascript', theme: 'nord' }))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { createHighlighterCore } from '@shikijs/core'
import js from '@shikijs/langs/js'
import nord from '@shikijs/themes/nord'
import { expect, it } from 'vitest'

// eslint-disable-next-line antfu/no-import-dist
import { wasmBinary } from '../../engine-oniguruma/dist/wasm-inlined.mjs'
import { createHighlighterCore } from '../src/core'
import js from '../src/langs/javascript.mjs'
import nord from '../src/themes/nord.mjs'
import { wasmBinary } from '../dist/wasm-inlined.mjs'
import { createOnigurumaEngine } from '../src/index'

it('wasm', async () => {
const shiki = await createHighlighterCore({
themes: [nord],
langs: [js as any],
loadWasm: obj => WebAssembly.instantiate(wasmBinary, obj).then(r => r.instance),
engine: createOnigurumaEngine(obj => WebAssembly.instantiate(wasmBinary, obj).then(r => r.instance)),
})

expect(shiki.codeToHtml('1 + 1', { lang: 'javascript', theme: 'nord' }))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { createHighlighterCore } from '@shikijs/core'
import js from '@shikijs/langs/js'
import nord from '@shikijs/themes/nord'
import { expect, it } from 'vitest'

// eslint-disable-next-line antfu/no-import-dist
import { wasmBinary } from '../../engine-oniguruma/dist/wasm-inlined.mjs'
import { createHighlighterCore } from '../src/core'
import js from '../src/langs/javascript.mjs'
import nord from '../src/themes/nord.mjs'
import { wasmBinary } from '../dist/wasm-inlined.mjs'
import { createOnigurumaEngine } from '../src/index'

it('wasm', async () => {
const shiki = await createHighlighterCore({
themes: [nord],
langs: [js as any],
loadWasm: Promise.resolve().then(() => obj => WebAssembly.instantiate(wasmBinary, obj).then(r => r.instance)),
engine: createOnigurumaEngine(Promise.resolve().then(() => obj => WebAssembly.instantiate(wasmBinary, obj).then(r => r.instance))),
})

expect(shiki.codeToHtml('1 + 1', { lang: 'javascript', theme: 'nord' }))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { createHighlighterCore } from '@shikijs/core'
import js from '@shikijs/langs/js'
import nord from '@shikijs/themes/nord'
import { expect, it } from 'vitest'

// eslint-disable-next-line antfu/no-import-dist
import { wasmBinary } from '../../engine-oniguruma/dist/wasm-inlined.mjs'
import { createHighlighterCore } from '../src/core'
import js from '../src/langs/javascript.mjs'
import nord from '../src/themes/nord.mjs'
import { wasmBinary } from '../dist/wasm-inlined.mjs'
import { createOnigurumaEngine } from '../src/index'

it('loadWasm: WebAssembly.Module', async () => {
const shiki = await createHighlighterCore({
themes: [nord],
langs: [js as any],
loadWasm: WebAssembly.compile(wasmBinary) as any,
engine: createOnigurumaEngine(WebAssembly.compile(wasmBinary) as any),
})

expect(shiki.codeToHtml('1 + 1', { lang: 'javascript', theme: 'nord' }))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { createHighlighterCore } from '@shikijs/core'
import js from '@shikijs/langs/js'
import nord from '@shikijs/themes/nord'
import { expect, it } from 'vitest'

// eslint-disable-next-line antfu/no-import-dist
import { wasmBinary } from '../../engine-oniguruma/dist/wasm-inlined.mjs'
import { createHighlighterCore } from '../src/core'
import js from '../src/langs/javascript.mjs'
import nord from '../src/themes/nord.mjs'
import { wasmBinary } from '../dist/wasm-inlined.mjs'
import { createOnigurumaEngine } from '../src/index'

it('loadWasm: { default: WebAssembly.Module }', async () => {
const shiki = await createHighlighterCore({
themes: [nord],
langs: [js as any],
loadWasm: Promise.resolve({ default: await WebAssembly.compile(wasmBinary) }) as any,
engine: createOnigurumaEngine(Promise.resolve({ default: await WebAssembly.compile(wasmBinary) }) as any),
})

expect(shiki.codeToHtml('1 + 1', { lang: 'javascript', theme: 'nord' }))
Expand Down
15 changes: 8 additions & 7 deletions packages/shiki/test/core.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createOnigurumaEngine } from '@shikijs/engine-oniguruma'
import { describe, expect, it } from 'vitest'

// eslint-disable-next-line antfu/no-import-dist
import { wasmBinary } from '../../engine-oniguruma/dist/wasm-inlined.mjs'

import { createHighlighterCore } from '../src/core'
import js from '../src/langs/javascript.mjs'
import ts from '../src/langs/typescript.mjs'
Expand All @@ -13,9 +14,9 @@ describe('should', () => {
using shiki = await createHighlighterCore({
themes: [nord],
langs: [js],
loadWasm: {
engine: createOnigurumaEngine({
instantiator: obj => WebAssembly.instantiate(wasmBinary, obj),
},
}),
})

expect(shiki.codeToHtml('console.log("Hi")', { lang: 'javascript', theme: 'nord' }))
Expand All @@ -29,10 +30,10 @@ describe('should', () => {
js,
import('../src/langs/c.mjs'),
],
loadWasm: {
engine: createOnigurumaEngine({
// https://github.com/WebAssembly/esm-integration/tree/main/proposals/esm-integration
instantiator: obj => WebAssembly.instantiate(wasmBinary, obj).then(r => r.instance.exports),
},
}),
})

await shiki.loadLanguage(() => import('../src/langs/python.mjs'))
Expand Down Expand Up @@ -151,9 +152,9 @@ describe('errors', () => {
using shiki = await createHighlighterCore({
themes: [nord],
langs: [js as any],
loadWasm: {
engine: createOnigurumaEngine({
instantiator: obj => WebAssembly.instantiate(wasmBinary, obj),
},
}),
})

const code = shiki.codeToHtml('console.log("Hi")', { lang: 'javascript', theme: mtp })
Expand Down
7 changes: 4 additions & 3 deletions packages/transformers/src/shared/notation-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ export function createCommentNotationTransformer(
lines: Element[],
index: number
) => boolean,
matchAlgorithm: MatchAlgorithm = 'v1',
matchAlgorithm: MatchAlgorithm | undefined,
): ShikiTransformer {
if (matchAlgorithm === 'v1') {
warnDeprecated('`matchAlgorithm: "v1"` is deprecated and will be removed in the future. Please explicitly set `matchAlgorithm: "v3"` in the transformer options.', 3)
if (matchAlgorithm == null) {
matchAlgorithm = 'v1'
warnDeprecated('The default `matchAlgorithm: "v1"` is deprecated and will be removed in the future. Please explicitly set `matchAlgorithm: "v3"` in the transformer options.', 3)
}

return {
Expand Down
18 changes: 9 additions & 9 deletions packages/transformers/test/fixtures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ suite(
'diff',
import.meta.glob('./fixtures/diff/*.*', { query: '?raw', import: 'default', eager: true }),
[
transformerNotationDiff(),
transformerNotationDiff({ matchAlgorithm: 'v3' }),
transformerRemoveLineBreak(),
],
code => `${code}
Expand All @@ -83,7 +83,7 @@ suite(
'focus',
import.meta.glob('./fixtures/focus/*.*', { query: '?raw', import: 'default', eager: true }),
[
transformerNotationFocus(),
transformerNotationFocus({ matchAlgorithm: 'v3' }),
transformerRemoveLineBreak(),
],
code => `${code}
Expand All @@ -99,7 +99,7 @@ suite(
'highlight',
import.meta.glob('./fixtures/highlight/*.*', { query: '?raw', import: 'default', eager: true }),
[
transformerNotationHighlight(),
transformerNotationHighlight({ matchAlgorithm: 'v3' }),
transformerRemoveLineBreak(),
],
code => `${code}
Expand All @@ -115,7 +115,7 @@ suite(
'highlight-word',
import.meta.glob('./fixtures/highlight-word/*.*', { query: '?raw', import: 'default', eager: true }),
[
transformerNotationWordHighlight(),
transformerNotationWordHighlight({ matchAlgorithm: 'v3' }),
transformerRemoveLineBreak(),
],
code => `${code}
Expand All @@ -131,7 +131,7 @@ suite(
'error-level',
import.meta.glob('./fixtures/error-level/*.*', { query: '?raw', import: 'default', eager: true }),
[
transformerNotationErrorLevel(),
transformerNotationErrorLevel({ matchAlgorithm: 'v3' }),
transformerRemoveLineBreak(),
],
code => `${code}
Expand Down Expand Up @@ -178,10 +178,10 @@ suite(
'all',
import.meta.glob('./fixtures/all/*.*', { query: '?raw', import: 'default', eager: true }),
[
transformerNotationDiff(),
transformerNotationFocus(),
transformerNotationHighlight(),
transformerNotationErrorLevel(),
transformerNotationDiff({ matchAlgorithm: 'v3' }),
transformerNotationFocus({ matchAlgorithm: 'v3' }),
transformerNotationHighlight({ matchAlgorithm: 'v3' }),
transformerNotationErrorLevel({ matchAlgorithm: 'v3' }),
transformerCompactLineOptions([
{
line: 2,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0f27a20

Please sign in to comment.