From 0f27a20d02f81d67862409fb0a19c5abcb685a6e Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 20 Jan 2025 17:17:10 +0800 Subject: [PATCH] feat: improve warning messages --- packages/core/src/textmate/grammar-state.ts | 2 +- .../engine-javascript/test/compare.test.ts | 8 ++++---- .../test/wasm1.test.ts | 14 ++++++++------ .../test/wasm2.test.ts | 14 ++++++++------ .../test/wasm3.test.ts | 12 +++++++----- .../test/wasm4.test.ts | 12 +++++++----- .../test/wasm5.test.ts | 12 +++++++----- .../test/wasm6.test.ts | 12 +++++++----- packages/shiki/test/core.test.ts | 15 ++++++++------- .../src/shared/notation-transformer.ts | 7 ++++--- packages/transformers/test/fixtures.test.ts | 18 +++++++++--------- .../test/fixtures/focus/empty-line-comment.js | 2 +- .../fixtures/highlight-word/mutliple-words.js | 2 +- .../fixtures/highlight/empty-line-comment.js | 2 +- 14 files changed, 73 insertions(+), 59 deletions(-) rename packages/{shiki => engine-oniguruma}/test/wasm1.test.ts (69%) rename packages/{shiki => engine-oniguruma}/test/wasm2.test.ts (70%) rename packages/{shiki => engine-oniguruma}/test/wasm3.test.ts (62%) rename packages/{shiki => engine-oniguruma}/test/wasm4.test.ts (60%) rename packages/{shiki => engine-oniguruma}/test/wasm5.test.ts (65%) rename packages/{shiki => engine-oniguruma}/test/wasm6.test.ts (63%) diff --git a/packages/core/src/textmate/grammar-state.ts b/packages/core/src/textmate/grammar-state.ts index f84318d25..8e25b663c 100644 --- a/packages/core/src/textmate/grammar-state.ts +++ b/packages/core/src/textmate/grammar-state.ts @@ -107,7 +107,7 @@ export class GrammarState implements GrammarStateInterface { lang: this.lang, theme: this.theme, themes: this.themes, - scopes: this.scopes, + scopes: this.getScopes(), } } } diff --git a/packages/engine-javascript/test/compare.test.ts b/packages/engine-javascript/test/compare.test.ts index 5c5bfadfd..d3097820b 100644 --- a/packages/engine-javascript/test/compare.test.ts +++ b/packages/engine-javascript/test/compare.test.ts @@ -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' @@ -176,7 +176,7 @@ 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({ @@ -184,12 +184,12 @@ describe('cases', async () => { }), ) - 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, diff --git a/packages/shiki/test/wasm1.test.ts b/packages/engine-oniguruma/test/wasm1.test.ts similarity index 69% rename from packages/shiki/test/wasm1.test.ts rename to packages/engine-oniguruma/test/wasm1.test.ts index d1a2e1d98..7366e3fe9 100644 --- a/packages/shiki/test/wasm1.test.ts +++ b/packages/engine-oniguruma/test/wasm1.test.ts @@ -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' })) diff --git a/packages/shiki/test/wasm2.test.ts b/packages/engine-oniguruma/test/wasm2.test.ts similarity index 70% rename from packages/shiki/test/wasm2.test.ts rename to packages/engine-oniguruma/test/wasm2.test.ts index 6fae2f83c..a6d2ea591 100644 --- a/packages/shiki/test/wasm2.test.ts +++ b/packages/engine-oniguruma/test/wasm2.test.ts @@ -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' })) diff --git a/packages/shiki/test/wasm3.test.ts b/packages/engine-oniguruma/test/wasm3.test.ts similarity index 62% rename from packages/shiki/test/wasm3.test.ts rename to packages/engine-oniguruma/test/wasm3.test.ts index 40f7af134..2d3e5ec8c 100644 --- a/packages/shiki/test/wasm3.test.ts +++ b/packages/engine-oniguruma/test/wasm3.test.ts @@ -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' })) diff --git a/packages/shiki/test/wasm4.test.ts b/packages/engine-oniguruma/test/wasm4.test.ts similarity index 60% rename from packages/shiki/test/wasm4.test.ts rename to packages/engine-oniguruma/test/wasm4.test.ts index afd53cb7a..cafe23c3c 100644 --- a/packages/shiki/test/wasm4.test.ts +++ b/packages/engine-oniguruma/test/wasm4.test.ts @@ -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' })) diff --git a/packages/shiki/test/wasm5.test.ts b/packages/engine-oniguruma/test/wasm5.test.ts similarity index 65% rename from packages/shiki/test/wasm5.test.ts rename to packages/engine-oniguruma/test/wasm5.test.ts index adddf51e3..51bcad597 100644 --- a/packages/shiki/test/wasm5.test.ts +++ b/packages/engine-oniguruma/test/wasm5.test.ts @@ -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' })) diff --git a/packages/shiki/test/wasm6.test.ts b/packages/engine-oniguruma/test/wasm6.test.ts similarity index 63% rename from packages/shiki/test/wasm6.test.ts rename to packages/engine-oniguruma/test/wasm6.test.ts index 7429ca01e..8cc02cc55 100644 --- a/packages/shiki/test/wasm6.test.ts +++ b/packages/engine-oniguruma/test/wasm6.test.ts @@ -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' })) diff --git a/packages/shiki/test/core.test.ts b/packages/shiki/test/core.test.ts index 5f531c1a2..742442637 100644 --- a/packages/shiki/test/core.test.ts +++ b/packages/shiki/test/core.test.ts @@ -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' @@ -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' })) @@ -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')) @@ -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 }) diff --git a/packages/transformers/src/shared/notation-transformer.ts b/packages/transformers/src/shared/notation-transformer.ts index 46d7fad06..7e265f939 100644 --- a/packages/transformers/src/shared/notation-transformer.ts +++ b/packages/transformers/src/shared/notation-transformer.ts @@ -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 { diff --git a/packages/transformers/test/fixtures.test.ts b/packages/transformers/test/fixtures.test.ts index ffd20191e..dc36cbeef 100644 --- a/packages/transformers/test/fixtures.test.ts +++ b/packages/transformers/test/fixtures.test.ts @@ -62,7 +62,7 @@ suite( 'diff', import.meta.glob('./fixtures/diff/*.*', { query: '?raw', import: 'default', eager: true }), [ - transformerNotationDiff(), + transformerNotationDiff({ matchAlgorithm: 'v3' }), transformerRemoveLineBreak(), ], code => `${code} @@ -83,7 +83,7 @@ suite( 'focus', import.meta.glob('./fixtures/focus/*.*', { query: '?raw', import: 'default', eager: true }), [ - transformerNotationFocus(), + transformerNotationFocus({ matchAlgorithm: 'v3' }), transformerRemoveLineBreak(), ], code => `${code} @@ -99,7 +99,7 @@ suite( 'highlight', import.meta.glob('./fixtures/highlight/*.*', { query: '?raw', import: 'default', eager: true }), [ - transformerNotationHighlight(), + transformerNotationHighlight({ matchAlgorithm: 'v3' }), transformerRemoveLineBreak(), ], code => `${code} @@ -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} @@ -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} @@ -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, diff --git a/packages/transformers/test/fixtures/focus/empty-line-comment.js b/packages/transformers/test/fixtures/focus/empty-line-comment.js index c9547605e..923d2ef59 100644 --- a/packages/transformers/test/fixtures/focus/empty-line-comment.js +++ b/packages/transformers/test/fixtures/focus/empty-line-comment.js @@ -1,5 +1,5 @@ export function transformerNotationFocus( - // [!code focus:5] + // [!code focus:4] options = {}, ) { const { diff --git a/packages/transformers/test/fixtures/highlight-word/mutliple-words.js b/packages/transformers/test/fixtures/highlight-word/mutliple-words.js index a80513068..c9c04b1c1 100644 --- a/packages/transformers/test/fixtures/highlight-word/mutliple-words.js +++ b/packages/transformers/test/fixtures/highlight-word/mutliple-words.js @@ -1,5 +1,5 @@ export function transformerNotationFocus( - // [!code word:options:3] + // [!code word:options:2] options = {}, // [!code word:log] ) { const options = 'options' diff --git a/packages/transformers/test/fixtures/highlight/empty-line-comment.js b/packages/transformers/test/fixtures/highlight/empty-line-comment.js index 6ca3a5240..3c71214e8 100644 --- a/packages/transformers/test/fixtures/highlight/empty-line-comment.js +++ b/packages/transformers/test/fixtures/highlight/empty-line-comment.js @@ -1,5 +1,5 @@ export function transformerNotationFocus( - // [!code highlight:5] + // [!code highlight:4] options = {}, ) { const {