Skip to content

Commit ad525a9

Browse files
committed
feat: improve warning messages
1 parent 2dd21f0 commit ad525a9

21 files changed

+53
-28
lines changed

packages/compat/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { AnsiToHtmlOptions, CodeToHtmlOptions, CodeToHtmlOptionsExtra, High
44
import fs from 'node:fs'
55
import fsp from 'node:fs/promises'
66
import { transformerCompactLineOptions } from '@shikijs/transformers'
7-
import { bundledLanguages, bundledThemes, createHighlighter as getShiki, normalizeTheme, tokenizeAnsiWithTheme } from 'shiki'
7+
import { bundledLanguages, bundledThemes, createHighlighter as getShiki, normalizeTheme, tokenizeAnsiWithTheme, warnDeprecated } from 'shiki'
88
import { ShikiCompatError } from './error'
99

1010
export const BUNDLED_LANGUAGES = bundledLanguages
@@ -35,6 +35,8 @@ export interface ShikiCompatHighlighter {
3535
}
3636

3737
export async function getHighlighter(options: Partial<HighlighterOptions> = {}): Promise<ShikiCompatHighlighter> {
38+
warnDeprecated(`@shikijs/compat is deprecated and will be removed in v3, please migrate to the main shiki package`)
39+
3840
const themes = options.themes || []
3941
const langs = options.langs || []
4042

packages/core/src/textmate/grammar-state.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { GrammarState as GrammarStateInterface, GrammarStateMapKey } from '@shikijs/types'
22
import type { StateStack, StateStackImpl } from '@shikijs/vscode-textmate'
33

4+
import { warnDeprecated } from '@shikijs/core'
45
import { INITIAL } from '@shikijs/vscode-textmate'
56
import { ShikiError } from '../../../types/src/error'
67
import { toArray } from '../utils'
@@ -88,6 +89,7 @@ export class GrammarState implements GrammarStateInterface {
8889
* @deprecated use `getScopes` instead
8990
*/
9091
get scopes(): string[] {
92+
warnDeprecated('GrammarState.scopes is deprecated, use GrammarState.getScopes() instead')
9193
return getScopes(this._stacks[this.theme] as StateStackImpl)
9294
}
9395

packages/core/src/warn.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
1-
let emitDeprecation = false
1+
let _emitDeprecation: DeprecationTarget | boolean = false
2+
let _emitError = false
3+
4+
export type DeprecationTarget = 3
25

36
/**
47
* Enable runtime warning for deprecated APIs, for the future versions of Shiki.
58
*
6-
* Disabled by default, will be enabled in Shiki v2.
9+
* You can pass a major version to only warn for deprecations that will be removed in that version.
710
*
8-
* @experimental The accuracy of the warning messages is not yet guaranteed.
11+
* By default, deprecation warning is set to 3 since Shiki v2.0.0
912
*/
10-
export function enableDeprecationWarnings(value = true): void {
11-
emitDeprecation = value
13+
export function enableDeprecationWarnings(
14+
emitDeprecation: DeprecationTarget | boolean = true,
15+
emitError = false,
16+
): void {
17+
_emitDeprecation = emitDeprecation
18+
_emitError = emitError
1219
}
1320

1421
/**
1522
* @internal
1623
*/
17-
export function warnDeprecated(message: string): void {
18-
if (emitDeprecation)
24+
export function warnDeprecated(message: string, version: DeprecationTarget = 3): void {
25+
if (!_emitDeprecation)
26+
return
27+
if (typeof _emitDeprecation === 'number' && version > _emitDeprecation)
28+
return
29+
30+
if (_emitError) {
31+
throw new Error(`[SHIKI DEPRECATE]: ${message}`)
32+
}
33+
else {
1934
// eslint-disable-next-line no-console
2035
console.trace(`[SHIKI DEPRECATE]: ${message}`)
36+
}
2137
}

packages/engine-javascript/test/compare.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ const cases: Cases[] = [
160160
]
161161

162162
describe('cases', async () => {
163-
await loadWasm(import('@shikijs/core/wasm-inlined'))
163+
await loadWasm(import('@shikijs/engine-oniguruma/wasm-inlined'))
164164

165165
const resolved = await Promise.all(cases.map(async (c) => {
166166
const theme = await c.theme().then(r => r.default)

packages/shiki/test/get-highlighter.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getSingletonHighlighter } from 'shiki'
1+
import { createOnigurumaEngine, getSingletonHighlighter } from 'shiki'
22
import { expect, it } from 'vitest'
33
import { getSingletonHighlighterCore } from '../src/core'
44
import js from '../src/langs/javascript.mjs'
@@ -9,7 +9,7 @@ it('getSingletonHighlighterCore', async () => {
99
const shiki1 = await getSingletonHighlighterCore({
1010
themes: [nord],
1111
langs: [js as any],
12-
loadWasm: import('@shikijs/core/wasm-inlined'),
12+
engine: createOnigurumaEngine(import('@shikijs/engine-oniguruma/wasm-inlined')),
1313
})
1414

1515
expect(shiki1.codeToHtml('console.log("Hi")', { lang: 'javascript', theme: 'nord' }))

packages/transformers/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@
3535
"prepublishOnly": "nr build"
3636
},
3737
"dependencies": {
38-
"shiki": "workspace:*"
38+
"@shikijs/types": "workspace:*"
3939
}
4040
}

packages/transformers/src/shared/notation-transformer.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import type { ShikiTransformer, ShikiTransformerContext } from '@shikijs/core'
12
import type { Element, Text } from 'hast'
2-
import type { ShikiTransformer, ShikiTransformerContext } from 'shiki'
3+
import { warnDeprecated } from '@shikijs/core'
34
import { parseComments, type ParsedComments, v1ClearEndCommentPrefix } from './parse-comments'
45

56
export type MatchAlgorithm = 'v1' | 'v3'
@@ -27,6 +28,10 @@ export function createCommentNotationTransformer(
2728
) => boolean,
2829
matchAlgorithm: MatchAlgorithm = 'v1',
2930
): ShikiTransformer {
31+
if (matchAlgorithm === 'v1') {
32+
warnDeprecated('`matchAlgorithm: "v1"` is deprecated and will be removed in the future. Please explicitly set `matchAlgorithm: "v3"` in the transformer options.', 3)
33+
}
34+
3035
return {
3136
name,
3237
code(code) {

packages/transformers/src/transformers/compact-line-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ShikiTransformer } from 'shiki'
1+
import type { ShikiTransformer } from '@shikijs/types'
22

33
export interface TransformerCompactLineOption {
44
/**

packages/transformers/src/transformers/meta-highlight-word.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ShikiTransformer } from 'shiki'
1+
import type { ShikiTransformer } from '@shikijs/types'
22

33
export function parseMetaHighlightWords(meta: string): string[] {
44
if (!meta)

packages/transformers/src/transformers/meta-highlight.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ShikiTransformer } from 'shiki'
1+
import type { ShikiTransformer } from '@shikijs/types'
22

33
export function parseMetaHighlightString(meta: string): number[] | null {
44
if (!meta)

0 commit comments

Comments
 (0)