From 34e69823669cf9f779d8f642763e9b15a83bb317 Mon Sep 17 00:00:00 2001 From: KazariEX <1364035137@qq.com> Date: Sat, 2 Nov 2024 19:37:59 +0800 Subject: [PATCH] refactor: use `slice` instead of `substring` --- extensions/vscode/src/features/doctor.ts | 2 +- packages/component-meta/lib/base.ts | 12 +++++----- .../lib/codegen/script/componentSelf.ts | 2 +- .../lib/codegen/script/context.ts | 4 ++-- .../lib/codegen/script/scriptSetup.ts | 4 ++-- .../language-core/lib/codegen/script/src.ts | 6 ++--- .../lib/codegen/script/template.ts | 10 ++++----- .../lib/codegen/template/element.ts | 4 ++-- .../lib/codegen/template/elementProps.ts | 2 +- .../lib/codegen/template/interpolation.ts | 22 +++++++++---------- .../lib/codegen/template/slotOutlet.ts | 2 +- .../lib/codegen/template/templateChild.ts | 4 ++-- .../lib/codegen/template/vFor.ts | 2 +- .../language-core/lib/codegen/utils/index.ts | 2 +- packages/language-core/lib/languagePlugin.ts | 2 +- .../lib/parsers/scriptSetupRanges.ts | 4 ++-- .../language-core/lib/plugins/file-html.ts | 2 +- packages/language-core/lib/plugins/file-md.ts | 2 +- .../language-core/lib/plugins/file-vue.ts | 4 ++-- .../lib/plugins/vue-root-tags.ts | 4 ++-- .../lib/plugins/vue-template-html.ts | 4 ++-- .../lib/plugins/vue-template-inline-css.ts | 2 +- .../lib/plugins/vue-template-inline-ts.ts | 2 +- packages/language-core/lib/plugins/vue-tsx.ts | 2 +- packages/language-server/lib/initialize.ts | 2 +- .../lib/plugins/vue-autoinsert-dotvalue.ts | 2 +- .../lib/plugins/vue-autoinsert-space.ts | 2 +- .../lib/plugins/vue-directive-comments.ts | 2 +- .../lib/plugins/vue-document-drop.ts | 6 ++--- .../lib/plugins/vue-document-links.ts | 6 ++--- .../lib/plugins/vue-extract-file.ts | 4 ++-- .../lib/plugins/vue-template.ts | 22 +++++++++---------- 32 files changed, 76 insertions(+), 76 deletions(-) diff --git a/extensions/vscode/src/features/doctor.ts b/extensions/vscode/src/features/doctor.ts index b6281c5a07..4de7b7c646 100644 --- a/extensions/vscode/src/features/doctor.ts +++ b/extensions/vscode/src/features/doctor.ts @@ -44,7 +44,7 @@ export async function activate(client: BaseLanguageClient) { const fileUri = doctorUri.with({ scheme: 'file', - path: doctorUri.path.substring(0, doctorUri.path.length - '/Doctor.md'.length), + path: doctorUri.path.slice(0, -'/Doctor.md'.length), }); const problems = await getProblems(fileUri); diff --git a/packages/component-meta/lib/base.ts b/packages/component-meta/lib/base.ts index 901dda662a..a749811c81 100644 --- a/packages/component-meta/lib/base.ts +++ b/packages/component-meta/lib/base.ts @@ -141,7 +141,7 @@ export function baseCreate( const globalTypesName = `${commandLine.vueOptions.lib}_${commandLine.vueOptions.target}_${commandLine.vueOptions.strictTemplates}.d.ts`; const globalTypesContents = `// @ts-nocheck\nexport {};\n` + vue.generateGlobalTypes(commandLine.vueOptions.lib, commandLine.vueOptions.target, commandLine.vueOptions.strictTemplates); const globalTypesSnapshot: ts.IScriptSnapshot = { - getText: (start, end) => globalTypesContents.substring(start, end), + getText: (start, end) => globalTypesContents.slice(start, end), getLength: () => globalTypesContents.length, getChangeRange: () => undefined, }; @@ -219,13 +219,13 @@ export function baseCreate( return ( commandLine.vueOptions.extensions.some(ext => fileName.endsWith(ext)) ? fileName - : fileName.substring(0, fileName.lastIndexOf('.')) + : fileName.slice(0, fileName.lastIndexOf('.')) ) + '.meta.ts'; } function getMetaScriptContent(fileName: string) { let code = ` -import * as Components from '${fileName.substring(0, fileName.length - '.meta.ts'.length)}'; +import * as Components from '${fileName.slice(0, -'.meta.ts'.length)}'; export default {} as { [K in keyof typeof Components]: ComponentMeta; }; interface ComponentMeta { @@ -335,7 +335,7 @@ ${commandLine.vueOptions.target < 3 ? vue2TypeHelpersCode : typeHelpersCode} ? (vueFile instanceof vue.VueVirtualCode ? readVueComponentDefaultProps(vueFile, printer, ts, commandLine.vueOptions) : {}) : {}; const tsDefaults = !vueFile ? readTsComponentDefaultProps( - componentPath.substring(componentPath.lastIndexOf('.') + 1), // ts | js | tsx | jsx + componentPath.slice(componentPath.lastIndexOf('.') + 1), // ts | js | tsx | jsx snapshot.getText(0, snapshot.getLength()), exportName, printer, @@ -726,7 +726,7 @@ function readVueComponentDefaultProps( if (descriptor.scriptSetup && scriptSetupRanges?.props.withDefaults?.arg) { - const defaultsText = descriptor.scriptSetup.content.substring(scriptSetupRanges.props.withDefaults.arg.start, scriptSetupRanges.props.withDefaults.arg.end); + const defaultsText = descriptor.scriptSetup.content.slice(scriptSetupRanges.props.withDefaults.arg.start, scriptSetupRanges.props.withDefaults.arg.end); const ast = ts.createSourceFile('/tmp.' + descriptor.scriptSetup.lang, '(' + defaultsText + ')', ts.ScriptTarget.Latest); const obj = findObjectLiteralExpression(ast); @@ -744,7 +744,7 @@ function readVueComponentDefaultProps( } } } else if (descriptor.scriptSetup && scriptSetupRanges?.props.define?.arg) { - const defaultsText = descriptor.scriptSetup.content.substring(scriptSetupRanges.props.define.arg.start, scriptSetupRanges.props.define.arg.end); + const defaultsText = descriptor.scriptSetup.content.slice(scriptSetupRanges.props.define.arg.start, scriptSetupRanges.props.define.arg.end); const ast = ts.createSourceFile('/tmp.' + descriptor.scriptSetup.lang, '(' + defaultsText + ')', ts.ScriptTarget.Latest); const obj = findObjectLiteralExpression(ast); diff --git a/packages/language-core/lib/codegen/script/componentSelf.ts b/packages/language-core/lib/codegen/script/componentSelf.ts index bfda805dc1..085697af3b 100644 --- a/packages/language-core/lib/codegen/script/componentSelf.ts +++ b/packages/language-core/lib/codegen/script/componentSelf.ts @@ -28,7 +28,7 @@ export function* generateComponentSelf( : ['', []] as const, ]) { for (const expose of bindings) { - const varName = content.substring(expose.start, expose.end); + const varName = content.slice(expose.start, expose.end); if (!templateUsageVars.has(varName) && !templateCodegenCtx.accessExternalVariables.has(varName)) { continue; } diff --git a/packages/language-core/lib/codegen/script/context.ts b/packages/language-core/lib/codegen/script/context.ts index a67d018de2..84434470fd 100644 --- a/packages/language-core/lib/codegen/script/context.ts +++ b/packages/language-core/lib/codegen/script/context.ts @@ -21,8 +21,8 @@ export function createScriptCodegenContext(options: ScriptCodegenOptions) { scriptSetupGeneratedOffset: undefined as number | undefined, bypassDefineComponent: options.lang === 'js' || options.lang === 'jsx', bindingNames: new Set([ - ...options.scriptRanges?.bindings.map(range => options.sfc.script!.content.substring(range.start, range.end)) ?? [], - ...options.scriptSetupRanges?.bindings.map(range => options.sfc.scriptSetup!.content.substring(range.start, range.end)) ?? [], + ...options.scriptRanges?.bindings.map(range => options.sfc.script!.content.slice(range.start, range.end)) ?? [], + ...options.scriptSetupRanges?.bindings.map(range => options.sfc.scriptSetup!.content.slice(range.start, range.end)) ?? [], ]), localTypes, inlayHints, diff --git a/packages/language-core/lib/codegen/script/scriptSetup.ts b/packages/language-core/lib/codegen/script/scriptSetup.ts index 491a4328ff..e8e319e39e 100644 --- a/packages/language-core/lib/codegen/script/scriptSetup.ts +++ b/packages/language-core/lib/codegen/script/scriptSetup.ts @@ -12,7 +12,7 @@ export function* generateScriptSetupImports( scriptSetupRanges: ScriptSetupRanges ): Generator { yield [ - scriptSetup.content.substring(0, Math.max(scriptSetupRanges.importSectionEndOffset, scriptSetupRanges.leadingCommentEndOffset)), + scriptSetup.content.slice(0, Math.max(scriptSetupRanges.importSectionEndOffset, scriptSetupRanges.leadingCommentEndOffset)), 'scriptSetup', 0, codeFeatures.all, @@ -524,5 +524,5 @@ function getRangeName( unwrap = false ) { const offset = unwrap ? 1 : 0; - return scriptSetup.content.substring(range.start + offset, range.end - offset); + return scriptSetup.content.slice(range.start + offset, range.end - offset); } \ No newline at end of file diff --git a/packages/language-core/lib/codegen/script/src.ts b/packages/language-core/lib/codegen/script/src.ts index 2c9aeb0dcb..350a4b44bf 100644 --- a/packages/language-core/lib/codegen/script/src.ts +++ b/packages/language-core/lib/codegen/script/src.ts @@ -7,13 +7,13 @@ export function* generateSrc( src: string ): Generator { if (src.endsWith('.d.ts')) { - src = src.substring(0, src.length - '.d.ts'.length); + src = src.slice(0, -'.d.ts'.length); } else if (src.endsWith('.ts')) { - src = src.substring(0, src.length - '.ts'.length); + src = src.slice(0, -'.ts'.length); } else if (src.endsWith('.tsx')) { - src = src.substring(0, src.length - '.tsx'.length) + '.jsx'; + src = src.slice(0, -'.tsx'.length) + '.jsx'; } if (!src.endsWith('.js') && !src.endsWith('.jsx')) { diff --git a/packages/language-core/lib/codegen/script/template.ts b/packages/language-core/lib/codegen/script/template.ts index 7cbaa2d227..a59c69b648 100644 --- a/packages/language-core/lib/codegen/script/template.ts +++ b/packages/language-core/lib/codegen/script/template.ts @@ -43,7 +43,7 @@ function* generateTemplateComponents(options: ScriptCodegenOptions): Generator { - const startTagOffset = node.loc.start.offset + options.template.content.substring(node.loc.start.offset).indexOf(node.tag); + const startTagOffset = node.loc.start.offset + options.template.content.slice(node.loc.start.offset).indexOf(node.tag); const endTagOffset = !node.isSelfClosing && options.template.lang === 'html' ? node.loc.start.offset + node.loc.source.lastIndexOf(node.tag) : undefined; const tagOffsets = endTagOffset !== undefined && endTagOffset > startTagOffset @@ -283,7 +283,7 @@ export function* generateElement( componentCtxVar: string | undefined, isVForChild: boolean ): Generator { - const startTagOffset = node.loc.start.offset + options.template.content.substring(node.loc.start.offset).indexOf(node.tag); + const startTagOffset = node.loc.start.offset + options.template.content.slice(node.loc.start.offset).indexOf(node.tag); const endTagOffset = !node.isSelfClosing && options.template.lang === 'html' ? node.loc.start.offset + node.loc.source.lastIndexOf(node.tag) : undefined; diff --git a/packages/language-core/lib/codegen/template/elementProps.ts b/packages/language-core/lib/codegen/template/elementProps.ts index 17c0ad0be6..b348c89eca 100644 --- a/packages/language-core/lib/codegen/template/elementProps.ts +++ b/packages/language-core/lib/codegen/template/elementProps.ts @@ -100,7 +100,7 @@ export function* generateElementProps( } if (prop.modifiers.some(m => m.content === 'prop' || m.content === 'attr')) { - propName = propName.substring(1); + propName = propName.slice(1); } const shouldSpread = propName === 'style' || propName === 'class'; diff --git a/packages/language-core/lib/codegen/template/interpolation.ts b/packages/language-core/lib/codegen/template/interpolation.ts index 57e87ec077..f03969daee 100644 --- a/packages/language-core/lib/codegen/template/interpolation.ts +++ b/packages/language-core/lib/codegen/template/interpolation.ts @@ -35,12 +35,12 @@ export function* generateInterpolation( let addSuffix = ''; const overLength = offset + section.length - _code.length; if (overLength > 0) { - addSuffix = section.substring(section.length - overLength); - section = section.substring(0, section.length - overLength); + addSuffix = section.slice(section.length - overLength); + section = section.slice(0, -overLength); } if (offset < 0) { - yield section.substring(0, -offset); - section = section.substring(-offset); + yield section.slice(0, -offset); + section = section.slice(-offset); offset = 0; } const shouldSkip = section.length === 0 && (type === 'startText' || type === 'endText'); @@ -117,11 +117,11 @@ export function* forEachInterpolationSegment( if (ctxVars.length) { if (ctxVars[0].isShorthand) { - yield [code.substring(0, ctxVars[0].offset + ctxVars[0].text.length), 0]; + yield [code.slice(0, ctxVars[0].offset + ctxVars[0].text.length), 0]; yield [': ', undefined]; } else if (ctxVars[0].offset > 0) { - yield [code.substring(0, ctxVars[0].offset), 0, 'startText']; + yield [code.slice(0, ctxVars[0].offset), 0, 'startText']; } for (let i = 0; i < ctxVars.length - 1; i++) { @@ -131,18 +131,18 @@ export function* forEachInterpolationSegment( yield* generateVar(code, destructuredPropNames, templateRefNames, curVar, nextVar); if (nextVar.isShorthand) { - yield [code.substring(curVar.offset + curVar.text.length, nextVar.offset + nextVar.text.length), curVar.offset + curVar.text.length]; + yield [code.slice(curVar.offset + curVar.text.length, nextVar.offset + nextVar.text.length), curVar.offset + curVar.text.length]; yield [': ', undefined]; } else { - yield [code.substring(curVar.offset + curVar.text.length, nextVar.offset), curVar.offset + curVar.text.length]; + yield [code.slice(curVar.offset + curVar.text.length, nextVar.offset), curVar.offset + curVar.text.length]; } } const lastVar = ctxVars.at(-1)!; yield* generateVar(code, destructuredPropNames, templateRefNames, lastVar); if (lastVar.offset + lastVar.text.length < code.length) { - yield [code.substring(lastVar.offset + lastVar.text.length), lastVar.offset + lastVar.text.length, 'endText']; + yield [code.slice(lastVar.offset + lastVar.text.length), lastVar.offset + lastVar.text.length, 'endText']; } } else { @@ -173,14 +173,14 @@ function* generateVar( const isTemplateRef = templateRefNames?.has(curVar.text) ?? false; if (isTemplateRef) { yield [`__VLS_unref(`, undefined]; - yield [code.substring(curVar.offset, curVar.offset + curVar.text.length), curVar.offset]; + yield [code.slice(curVar.offset, curVar.offset + curVar.text.length), curVar.offset]; yield [`)`, undefined]; } else { if (!isDestructuredProp) { yield [`__VLS_ctx.`, undefined]; } - yield [code.substring(curVar.offset, curVar.offset + curVar.text.length), curVar.offset]; + yield [code.slice(curVar.offset, curVar.offset + curVar.text.length), curVar.offset]; } } diff --git a/packages/language-core/lib/codegen/template/slotOutlet.ts b/packages/language-core/lib/codegen/template/slotOutlet.ts index f21d337ce6..a3908e90a5 100644 --- a/packages/language-core/lib/codegen/template/slotOutlet.ts +++ b/packages/language-core/lib/codegen/template/slotOutlet.ts @@ -15,7 +15,7 @@ export function* generateSlotOutlet( currentComponent: CompilerDOM.ElementNode | undefined, componentCtxVar: string | undefined ): Generator { - const startTagOffset = node.loc.start.offset + options.template.content.substring(node.loc.start.offset).indexOf(node.tag); + const startTagOffset = node.loc.start.offset + options.template.content.slice(node.loc.start.offset).indexOf(node.tag); const varSlot = ctx.getInternalVariable(); const nameProp = node.props.find(prop => { if (prop.type === CompilerDOM.NodeTypes.ATTRIBUTE) { diff --git a/packages/language-core/lib/codegen/template/templateChild.ts b/packages/language-core/lib/codegen/template/templateChild.ts index e8ded97eb7..c0360c0867 100644 --- a/packages/language-core/lib/codegen/template/templateChild.ts +++ b/packages/language-core/lib/codegen/template/templateChild.ts @@ -194,11 +194,11 @@ export function parseInterpolationNode(node: CompilerDOM.InterpolationNode, temp let rightCharacter: string; // fix https://github.com/vuejs/language-tools/issues/1787 - while ((leftCharacter = template.substring(start - 1, start)).trim() === '' && leftCharacter.length) { + while ((leftCharacter = template.slice(start - 1, start)).trim() === '' && leftCharacter.length) { start--; content = leftCharacter + content; } - while ((rightCharacter = template.substring(start + content.length, start + content.length + 1)).trim() === '' && rightCharacter.length) { + while ((rightCharacter = template.slice(start + content.length, start + content.length + 1)).trim() === '' && rightCharacter.length) { content = content + rightCharacter; } diff --git a/packages/language-core/lib/codegen/template/vFor.ts b/packages/language-core/lib/codegen/template/vFor.ts index 36c55692ac..c89104497d 100644 --- a/packages/language-core/lib/codegen/template/vFor.ts +++ b/packages/language-core/lib/codegen/template/vFor.ts @@ -105,7 +105,7 @@ export function parseVForNode(node: CompilerDOM.ForNode) { } : undefined; const leftExpressionText = leftExpressionRange - ? node.loc.source.substring( + ? node.loc.source.slice( leftExpressionRange.start - node.loc.start.offset, leftExpressionRange.end - node.loc.start.offset ) diff --git a/packages/language-core/lib/codegen/utils/index.ts b/packages/language-core/lib/codegen/utils/index.ts index b297524222..e404e73c9a 100644 --- a/packages/language-core/lib/codegen/utils/index.ts +++ b/packages/language-core/lib/codegen/utils/index.ts @@ -91,7 +91,7 @@ export function createTsAst(ts: typeof import('typescript'), astHolder: any, tex export function generateSfcBlockSection(block: SfcBlock, start: number, end: number, features: VueCodeInformation): Code { return [ - block.content.substring(start, end), + block.content.slice(start, end), block.name, start, features, diff --git a/packages/language-core/lib/languagePlugin.ts b/packages/language-core/lib/languagePlugin.ts index 8d6055f169..3cea8cc20f 100644 --- a/packages/language-core/lib/languagePlugin.ts +++ b/packages/language-core/lib/languagePlugin.ts @@ -119,7 +119,7 @@ export function createVueLanguagePlugin( getServiceScript(root) { for (const code of forEachEmbeddedCode(root)) { if (/script_(js|jsx|ts|tsx)/.test(code.id)) { - const lang = code.id.substring('script_'.length); + const lang = code.id.slice('script_'.length); return { code, extension: '.' + lang, diff --git a/packages/language-core/lib/parsers/scriptSetupRanges.ts b/packages/language-core/lib/parsers/scriptSetupRanges.ts index b020a7f24b..6e4f82e5c5 100644 --- a/packages/language-core/lib/parsers/scriptSetupRanges.ts +++ b/packages/language-core/lib/parsers/scriptSetupRanges.ts @@ -113,7 +113,7 @@ export function parseScriptSetupRanges( const templateRefNames = new Set(templateRefs.map(ref => ref.name)); bindings = bindings.filter(range => { - const name = text.substring(range.start, range.end); + const name = text.slice(range.start, range.end); return !templateRefNames.has(name); }); @@ -512,7 +512,7 @@ export function getNodeText( sourceFile: ts.SourceFile ) { const { start, end } = getStartEnd(ts, node, sourceFile); - return sourceFile.text.substring(start, end); + return sourceFile.text.slice(start, end); } function getStatementRange( diff --git a/packages/language-core/lib/plugins/file-html.ts b/packages/language-core/lib/plugins/file-html.ts index 036d6bf03b..2bb52b7d9e 100644 --- a/packages/language-core/lib/plugins/file-html.ts +++ b/packages/language-core/lib/plugins/file-html.ts @@ -81,7 +81,7 @@ const plugin: VueLanguagePlugin = ({ vueCompilerOptions }) => { }; } - templateContent = templateContent.substring(0, match.index) + ' '.repeat(matchText.length) + templateContent.substring(match.index + matchText.length); + templateContent = templateContent.slice(0, match.index) + ' '.repeat(matchText.length) + templateContent.slice(match.index + matchText.length); } sfc.descriptor.template = { diff --git a/packages/language-core/lib/plugins/file-md.ts b/packages/language-core/lib/plugins/file-md.ts index 3516476d8d..b3705ce88a 100644 --- a/packages/language-core/lib/plugins/file-md.ts +++ b/packages/language-core/lib/plugins/file-md.ts @@ -51,7 +51,7 @@ const plugin: VueLanguagePlugin = ({ vueCompilerOptions }) => { const matchText = match[0]; codes.push([matchText, undefined, match.index]); codes.push('\n\n'); - content = content.substring(0, match.index) + ' '.repeat(matchText.length) + content.substring(match.index + matchText.length); + content = content.slice(0, match.index) + ' '.repeat(matchText.length) + content.slice(match.index + matchText.length); } } diff --git a/packages/language-core/lib/plugins/file-vue.ts b/packages/language-core/lib/plugins/file-vue.ts index 476a44b015..441c4022a1 100644 --- a/packages/language-core/lib/plugins/file-vue.ts +++ b/packages/language-core/lib/plugins/file-vue.ts @@ -41,9 +41,9 @@ const plugin: VueLanguagePlugin = ({ vueCompilerOptions }) => { const oldContent = hitBlock.content; const newContent = hitBlock.content = - hitBlock.content.substring(0, change.start - hitBlock.loc.start.offset) + hitBlock.content.slice(0, change.start - hitBlock.loc.start.offset) + change.newText - + hitBlock.content.substring(change.end - hitBlock.loc.start.offset); + + hitBlock.content.slice(change.end - hitBlock.loc.start.offset); // #3449 const endTagRegex = new RegExp(``); diff --git a/packages/language-core/lib/plugins/vue-root-tags.ts b/packages/language-core/lib/plugins/vue-root-tags.ts index 2e9e5c56f0..d535bb93e4 100644 --- a/packages/language-core/lib/plugins/vue-root-tags.ts +++ b/packages/language-core/lib/plugins/vue-root-tags.ts @@ -42,7 +42,7 @@ const plugin: VueLanguagePlugin = () => { undefined, block.startTagEnd, block.endTagStart, - sfc.content.substring( + sfc.content.slice( block.startTagEnd, block.startTagEnd + offset ), @@ -52,7 +52,7 @@ const plugin: VueLanguagePlugin = () => { block.startTagEnd + offset, { structure: true }, ], - sfc.content.substring( + sfc.content.slice( block.startTagEnd + offset, block.endTagStart ) diff --git a/packages/language-core/lib/plugins/vue-template-html.ts b/packages/language-core/lib/plugins/vue-template-html.ts index e06a3a605b..8282a34c8f 100644 --- a/packages/language-core/lib/plugins/vue-template-html.ts +++ b/packages/language-core/lib/plugins/vue-template-html.ts @@ -202,9 +202,9 @@ const plugin: VueLanguagePlugin = ({ modules }) => { if (withinChangeRange(loc)) { loc.source = - loc.source.substring(0, change.start - loc.start.offset) + loc.source.slice(0, change.start - loc.start.offset) + change.newText - + loc.source.substring(change.end - loc.start.offset); + + loc.source.slice(change.end - loc.start.offset); (loc as any).__endOffset = loc.end.offset; loc.end.offset += lengthDiff; return true; diff --git a/packages/language-core/lib/plugins/vue-template-inline-css.ts b/packages/language-core/lib/plugins/vue-template-inline-css.ts index cd285bf75a..79c13f93ec 100644 --- a/packages/language-core/lib/plugins/vue-template-inline-css.ts +++ b/packages/language-core/lib/plugins/vue-template-inline-css.ts @@ -48,7 +48,7 @@ function* generate(templateAst: NonNullable): Generator { const start = leftExpressionRange.start; const end = source.loc.start.offset + source.content.length; addFormatCodes( - templateContent.substring(start, end), + templateContent.slice(start, end), start, formatBrackets.for ); diff --git a/packages/language-core/lib/plugins/vue-tsx.ts b/packages/language-core/lib/plugins/vue-tsx.ts index 9e2e4b9d97..a6d29e9682 100644 --- a/packages/language-core/lib/plugins/vue-tsx.ts +++ b/packages/language-core/lib/plugins/vue-tsx.ts @@ -129,7 +129,7 @@ function createTsx( const bindings = scriptSetupRanges.get()?.bindings; if (_sfc.scriptSetup && bindings) { for (const binding of bindings) { - newNames.add(_sfc.scriptSetup?.content.substring(binding.start, binding.end)); + newNames.add(_sfc.scriptSetup?.content.slice(binding.start, binding.end)); } } if (newNames && oldNames && twoSetsEqual(newNames, oldNames)) { diff --git a/packages/language-server/lib/initialize.ts b/packages/language-server/lib/initialize.ts index 4a2a02e97c..88c5c626f1 100644 --- a/packages/language-server/lib/initialize.ts +++ b/packages/language-server/lib/initialize.ts @@ -58,7 +58,7 @@ export function initialize( const globalTypesName = `${vueCompilerOptions.lib}_${vueCompilerOptions.target}_${vueCompilerOptions.strictTemplates}.d.ts`; const globalTypesContents = `// @ts-nocheck\nexport {};\n` + generateGlobalTypes(vueCompilerOptions.lib, vueCompilerOptions.target, vueCompilerOptions.strictTemplates); const globalTypesSnapshot: ts.IScriptSnapshot = { - getText: (start, end) => globalTypesContents.substring(start, end), + getText: (start, end) => globalTypesContents.slice(start, end), getLength: () => globalTypesContents.length, getChangeRange: () => undefined, }; diff --git a/packages/language-service/lib/plugins/vue-autoinsert-dotvalue.ts b/packages/language-service/lib/plugins/vue-autoinsert-dotvalue.ts index 43e0bf854c..3c07c84f23 100644 --- a/packages/language-service/lib/plugins/vue-autoinsert-dotvalue.ts +++ b/packages/language-service/lib/plugins/vue-autoinsert-dotvalue.ts @@ -124,7 +124,7 @@ const charReg = /\w/; export function isCharacterTyping(document: TextDocument, change: { text: string; rangeOffset: number; rangeLength: number; }) { const lastCharacter = change.text[change.text.length - 1]; - const nextCharacter = document.getText().substring( + const nextCharacter = document.getText().slice( change.rangeOffset + change.text.length, change.rangeOffset + change.text.length + 1 ); diff --git a/packages/language-service/lib/plugins/vue-autoinsert-space.ts b/packages/language-service/lib/plugins/vue-autoinsert-space.ts index 8c23035663..4ff7c0de91 100644 --- a/packages/language-service/lib/plugins/vue-autoinsert-space.ts +++ b/packages/language-service/lib/plugins/vue-autoinsert-space.ts @@ -22,7 +22,7 @@ export function create(): LanguageServicePlugin { if ( change.text === '{}' - && document.getText().substring(change.rangeOffset - 1, change.rangeOffset + 3) === '{{}}' + && document.getText().slice(change.rangeOffset - 1, change.rangeOffset + 3) === '{{}}' && document.offsetAt(selection) === change.rangeOffset + 1 ) { return ` $0 `; diff --git a/packages/language-service/lib/plugins/vue-directive-comments.ts b/packages/language-service/lib/plugins/vue-directive-comments.ts index 17b70b1077..c695d02644 100644 --- a/packages/language-service/lib/plugins/vue-directive-comments.ts +++ b/packages/language-service/lib/plugins/vue-directive-comments.ts @@ -33,7 +33,7 @@ export function create(): LanguageServicePlugin { } const startIndex = cmdStart.index! + cmdStart[0].length; - const remainText = line.substring(startIndex); + const remainText = line.slice(startIndex); const result: CompletionItem[] = []; for (const [label, text = label] of cmds) { diff --git a/packages/language-service/lib/plugins/vue-document-drop.ts b/packages/language-service/lib/plugins/vue-document-drop.ts index 7b0e3cfcb3..a6aa6b220b 100644 --- a/packages/language-service/lib/plugins/vue-document-drop.ts +++ b/packages/language-service/lib/plugins/vue-document-drop.ts @@ -51,8 +51,8 @@ export function create( return; } - let baseName = importUri.substring(importUri.lastIndexOf('/') + 1); - baseName = baseName.substring(0, baseName.lastIndexOf('.')); + let baseName = importUri.slice(importUri.lastIndexOf('/') + 1); + baseName = baseName.slice(0, baseName.lastIndexOf('.')); const newName = capitalize(camelize(baseName)); const { _sfc: sfc } = vueVirtualCode; @@ -83,7 +83,7 @@ export function create( if (!importPath) { importPath = path.relative(path.dirname(vueVirtualCode.fileName), incomingFileName) - || importUri.substring(importUri.lastIndexOf('/') + 1); + || importUri.slice(importUri.lastIndexOf('/') + 1); if (!importPath.startsWith('./') && !importPath.startsWith('../')) { importPath = './' + importPath; diff --git a/packages/language-service/lib/plugins/vue-document-links.ts b/packages/language-service/lib/plugins/vue-document-links.ts index 6bab96528a..23947e09ac 100644 --- a/packages/language-service/lib/plugins/vue-document-links.ts +++ b/packages/language-service/lib/plugins/vue-document-links.ts @@ -33,10 +33,10 @@ export function create(): LanguageServicePlugin { const style = sourceScript.generated.root._sfc.styles[i]; if (option === 'always' || (option === 'scoped' && style.scoped)) { for (const className of style.classNames) { - if (!styleClasses.has(className.text.substring(1))) { - styleClasses.set(className.text.substring(1), []); + if (!styleClasses.has(className.text.slice(1))) { + styleClasses.set(className.text.slice(1), []); } - styleClasses.get(className.text.substring(1))!.push({ + styleClasses.get(className.text.slice(1))!.push({ index: i, style, classOffset: className.offset, diff --git a/packages/language-service/lib/plugins/vue-extract-file.ts b/packages/language-service/lib/plugins/vue-extract-file.ts index 4cfaab82b6..de17245580 100644 --- a/packages/language-service/lib/plugins/vue-extract-file.ts +++ b/packages/language-service/lib/plugins/vue-extract-file.ts @@ -100,13 +100,13 @@ export function create( const templateInitialIndent = await context.env.getConfiguration!('vue.format.template.initialIndent') ?? true; const scriptInitialIndent = await context.env.getConfiguration!('vue.format.script.initialIndent') ?? false; - const newUri = sfcDocument.uri.substring(0, sfcDocument.uri.lastIndexOf('/') + 1) + `${newName}.vue`; + const newUri = sfcDocument.uri.slice(0, sfcDocument.uri.lastIndexOf('/') + 1) + `${newName}.vue`; const lastImportNode = getLastImportNode(ts, script.ast); let newFileTags = []; newFileTags.push( - constructTag('template', [], templateInitialIndent, sfc.template.content.substring(templateCodeRange[0], templateCodeRange[1])) + constructTag('template', [], templateInitialIndent, sfc.template.content.slice(templateCodeRange[0], templateCodeRange[1])) ); if (toExtract.length) { diff --git a/packages/language-service/lib/plugins/vue-template.ts b/packages/language-service/lib/plugins/vue-template.ts index 78f7d7b11a..3bc7be5a94 100644 --- a/packages/language-service/lib/plugins/vue-template.ts +++ b/packages/language-service/lib/plugins/vue-template.ts @@ -106,7 +106,7 @@ export function create( .split('\n- ')[4] .split('\n').slice(2, -1); for (let text of modifiers) { - text = text.substring(' - `.'.length); + text = text.slice(' - `.'.length); const [name, disc] = text.split('` - '); eventModifiers[name] = disc; } @@ -117,7 +117,7 @@ export function create( .split('\n- ')[4] .split('\n').slice(2, -1); for (let text of modifiers) { - text = text.substring(' - `.'.length); + text = text.slice(' - `.'.length); const [name, disc] = text.split('` - '); propModifiers[name] = disc; } @@ -252,19 +252,19 @@ export function create( } // normalize if (attrText.startsWith('v-bind:')) { - attrText = attrText.substring('v-bind:'.length); + attrText = attrText.slice('v-bind:'.length); } else if (attrText.startsWith(':')) { - attrText = attrText.substring(':'.length); + attrText = attrText.slice(':'.length); } else if (attrText.startsWith('v-model:')) { - attrText = attrText.substring('v-model:'.length); + attrText = attrText.slice('v-model:'.length); } else if (attrText === 'v-model') { attrText = vueCompilerOptions.target >= 3 ? 'modelValue' : 'value'; // TODO: support for experimentalModelPropName? } else if (attrText.startsWith('@')) { - attrText = 'on-' + hyphenateAttr(attrText.substring('@'.length)); + attrText = 'on-' + hyphenateAttr(attrText.slice('@'.length)); } current.unburnedRequiredProps = current.unburnedRequiredProps.filter(propName => { @@ -501,7 +501,7 @@ export function create( } for (const binding of scriptSetupRanges?.bindings ?? []) { - const name = vueCode._sfc.scriptSetup!.content.substring(binding.start, binding.end); + const name = vueCode._sfc.scriptSetup!.content.slice(binding.start, binding.end); if (casing.tag === TagNameCasing.Kebab) { names.add(hyphenateTag(name)); } @@ -553,8 +553,8 @@ export function create( return []; } let ctxVars = [ - ..._tsCodegen.scriptRanges.get()?.bindings.map(binding => vueCode._sfc.script!.content.substring(binding.start, binding.end)) ?? [], - ..._tsCodegen.scriptSetupRanges.get()?.bindings.map(binding => vueCode._sfc.scriptSetup!.content.substring(binding.start, binding.end)) ?? [], + ..._tsCodegen.scriptRanges.get()?.bindings.map(binding => vueCode._sfc.script!.content.slice(binding.start, binding.end)) ?? [], + ..._tsCodegen.scriptSetupRanges.get()?.bindings.map(binding => vueCode._sfc.scriptSetup!.content.slice(binding.start, binding.end)) ?? [], ...templateContextProps, ]; ctxVars = [...new Set(ctxVars)]; @@ -647,12 +647,12 @@ export function create( for (const prop of [...props, ...attrs]) { if (prop.startsWith('onUpdate:')) { const isGlobal = !propsSet.has(prop); - models.push([isGlobal, prop.substring('onUpdate:'.length)]); + models.push([isGlobal, prop.slice('onUpdate:'.length)]); } } for (const event of events) { if (event.startsWith('update:')) { - models.push([false, event.substring('update:'.length)]); + models.push([false, event.slice('update:'.length)]); } }