diff --git a/packages/vue-component-meta/src/index.ts b/packages/vue-component-meta/src/index.ts index c123391d7..413ee82c4 100644 --- a/packages/vue-component-meta/src/index.ts +++ b/packages/vue-component-meta/src/index.ts @@ -19,13 +19,15 @@ export * from './types'; export type ComponentMetaChecker = ReturnType; +const windowsPathReg = /\\/g; + export function createComponentMetaCheckerByJsonConfig( root: string, json: any, checkerOptions: MetaCheckerOptions = {}, ts: typeof import('typescript/lib/tsserverlibrary') = require('typescript'), ) { - const rootPath = (root as path.OsPath).replace(/\\/g, '/') as path.PosixPath; + const rootPath = (root as path.OsPath).replace(windowsPathReg, '/') as path.PosixPath; return createComponentMetaCheckerWorker( () => vue.createParsedCommandLineByJson(ts, ts.sys, root, json), checkerOptions, @@ -40,7 +42,7 @@ export function createComponentMetaChecker( checkerOptions: MetaCheckerOptions = {}, ts: typeof import('typescript/lib/tsserverlibrary') = require('typescript'), ) { - const tsconfig = (tsconfigPath as path.OsPath).replace(/\\/g, '/') as path.PosixPath; + const tsconfig = (tsconfigPath as path.OsPath).replace(windowsPathReg, '/') as path.PosixPath; return createComponentMetaCheckerWorker( () => vue.createParsedCommandLine(ts, ts.sys, tsconfigPath), checkerOptions, @@ -63,7 +65,7 @@ function createComponentMetaCheckerWorker( */ let parsedCommandLine = loadParsedCommandLine(); - let fileNames = (parsedCommandLine.fileNames as path.OsPath[]).map(path => path.replace(/\\/g, '/') as path.PosixPath); + let fileNames = (parsedCommandLine.fileNames as path.OsPath[]).map(path => path.replace(windowsPathReg, '/') as path.PosixPath); let projectVersion = 0; const scriptSnapshots = new Map(); @@ -87,18 +89,18 @@ function createComponentMetaCheckerWorker( return { ...baseCreate(_host, vue.resolveVueCompilerOptions(parsedCommandLine.vueOptions), checkerOptions, globalComponentName, ts), updateFile(fileName: string, text: string) { - fileName = (fileName as path.OsPath).replace(/\\/g, '/') as path.PosixPath; + fileName = (fileName as path.OsPath).replace(windowsPathReg, '/') as path.PosixPath; scriptSnapshots.set(fileName, ts.ScriptSnapshot.fromString(text)); projectVersion++; }, deleteFile(fileName: string) { - fileName = (fileName as path.OsPath).replace(/\\/g, '/') as path.PosixPath; + fileName = (fileName as path.OsPath).replace(windowsPathReg, '/') as path.PosixPath; fileNames = fileNames.filter(f => f !== fileName); projectVersion++; }, reload() { parsedCommandLine = loadParsedCommandLine(); - fileNames = (parsedCommandLine.fileNames as path.OsPath[]).map(path => path.replace(/\\/g, '/') as path.PosixPath); + fileNames = (parsedCommandLine.fileNames as path.OsPath[]).map(path => path.replace(windowsPathReg, '/') as path.PosixPath); this.clearCache(); }, clearCache() { diff --git a/packages/vue-language-core/src/generators/template.ts b/packages/vue-language-core/src/generators/template.ts index 8c99c5c84..99b2ac620 100644 --- a/packages/vue-language-core/src/generators/template.ts +++ b/packages/vue-language-core/src/generators/template.ts @@ -42,7 +42,8 @@ const formatBrackets = { curly: ['0 +', '+ 0;'] as [string, string], event: ['() => ', ';'] as [string, string], }; -const validTsVar = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/; +const validTsVarReg = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/; +const colonReg = /:/g; // @ts-ignore const transformContext: CompilerDOM.TransformContext = { onError: () => { }, @@ -177,7 +178,7 @@ export function generate( } function toCanonicalComponentName(tagText: string) { - return validTsVar.test(tagText) ? tagText : capitalize(camelize(tagText.replace(/:/g, '-'))); + return validTsVarReg.test(tagText) ? tagText : capitalize(camelize(tagText.replace(colonReg, '-'))); } function getPossibleOriginalComponentName(tagText: string) { @@ -676,7 +677,7 @@ export function generate( else { codes.push(`let ${var_originalComponent}!: `); for (const componentName of getPossibleOriginalComponentName(tag)) { - codes.push(`'${componentName}' extends keyof typeof __VLS_ctx ? typeof __VLS_ctx${validTsVar.test(componentName) ? `.${componentName}` : `['${componentName}']`} : `); + codes.push(`'${componentName}' extends keyof typeof __VLS_ctx ? typeof __VLS_ctx${validTsVarReg.test(componentName) ? `.${componentName}` : `['${componentName}']`} : `); } codes.push(`typeof __VLS_resolvedLocalAndGlobalComponents['${toCanonicalComponentName(tag)}'];\n`); } @@ -1776,7 +1777,7 @@ export function generate( function createObjectPropertyCode(a: Code, astHolder?: any): Code[] { const aStr = typeof a === 'string' ? a : a[0]; - if (validTsVar.test(aStr)) { + if (validTsVarReg.test(aStr)) { return [a]; } else if (aStr.startsWith('[') && aStr.endsWith(']') && astHolder) { @@ -1861,7 +1862,7 @@ export function generate( function createPropertyAccessCode(a: Code, astHolder?: any): Code[] { const aStr = typeof a === 'string' ? a : a[0]; - if (!compilerOptions.noPropertyAccessFromIndexSignature && validTsVar.test(aStr)) { + if (!compilerOptions.noPropertyAccessFromIndexSignature && validTsVarReg.test(aStr)) { return ['.', a]; } else if (aStr.startsWith('[') && aStr.endsWith(']')) { diff --git a/packages/vue-language-core/src/plugins/file-html.ts b/packages/vue-language-core/src/plugins/file-html.ts index e86b8a49b..9688672d3 100644 --- a/packages/vue-language-core/src/plugins/file-html.ts +++ b/packages/vue-language-core/src/plugins/file-html.ts @@ -1,6 +1,9 @@ import type { SFCParseResult } from '@vue/compiler-sfc'; import { VueLanguagePlugin } from '../types'; +const sfcBlockReg = /\<(script|style)\b([\s\S]*?)\>([\s\S]*?)\<\/\1\>/g; +const langReg = /\blang\s*=\s*(['\"]?)(\S*)\b\1/; + const plugin: VueLanguagePlugin = () => { return { @@ -29,9 +32,6 @@ const plugin: VueLanguagePlugin = () => { let templateContent = content; - const sfcBlockReg = /\<(script|style)\b([\s\S]*?)\>([\s\S]*?)\<\/\1\>/g; - const langReg = /\blang\s*=\s*(['\"]?)(\S*)\b\1/; - for (const match of content.matchAll(sfcBlockReg)) { const matchText = match[0]; diff --git a/packages/vue-language-core/src/plugins/file-md.ts b/packages/vue-language-core/src/plugins/file-md.ts index e8ec93045..d43cfb4e1 100644 --- a/packages/vue-language-core/src/plugins/file-md.ts +++ b/packages/vue-language-core/src/plugins/file-md.ts @@ -3,6 +3,13 @@ import type { SFCBlock } from '@vue/compiler-sfc'; import { VueLanguagePlugin } from '../types'; import { parse } from '../utils/parseSfc'; +const codeblockReg = /```[\s\S]+?```/g; +const inlineCodeblockReg = /`[^\n`]+?`/g; +const scriptSetupReg = /\\\<[\s\S]+?\>\n?/g; +const sfcBlockReg = /\<(script|style)\b[\s\S]*?\>([\s\S]*?)\<\/\1\>/g; +const angleBracketReg = /\<\S*\:\S*\>/g; +const linkReg = /\[[\s\S]*?\]\([\s\S]*?\)/g; + const plugin: VueLanguagePlugin = () => { return { @@ -15,13 +22,12 @@ const plugin: VueLanguagePlugin = () => { content = content // code block - .replace(/```[\s\S]+?```/g, match => '```' + ' '.repeat(match.length - 6) + '```') + .replace(codeblockReg, match => '```' + ' '.repeat(match.length - 6) + '```') // inline code block - .replace(/`[^\n`]+?`/g, match => `\`${' '.repeat(match.length - 2)}\``) + .replace(inlineCodeblockReg, match => `\`${' '.repeat(match.length - 2)}\``) // # \