Skip to content

Commit

Permalink
refactor: use slice instead of substring
Browse files Browse the repository at this point in the history
  • Loading branch information
KazariEX committed Nov 2, 2024
1 parent 1a9eb26 commit 34e6982
Show file tree
Hide file tree
Showing 32 changed files with 76 additions and 76 deletions.
2 changes: 1 addition & 1 deletion extensions/vscode/src/features/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 6 additions & 6 deletions packages/component-meta/lib/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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<typeof Components[K]>; };
interface ComponentMeta<T> {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/script/componentSelf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/codegen/script/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/codegen/script/scriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function* generateScriptSetupImports(
scriptSetupRanges: ScriptSetupRanges
): Generator<Code> {
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,
Expand Down Expand Up @@ -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);
}
6 changes: 3 additions & 3 deletions packages/language-core/lib/codegen/script/src.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ export function* generateSrc(
src: string
): Generator<Code> {
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')) {
Expand Down
10 changes: 5 additions & 5 deletions packages/language-core/lib/codegen/script/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function* generateTemplateComponents(options: ScriptCodegenOptions): Generator<C
if (options.sfc.script && options.scriptRanges?.exportDefault?.componentsOption) {
const { componentsOption } = options.scriptRanges.exportDefault;
exps.push([
options.sfc.script.content.substring(componentsOption.start, componentsOption.end),
options.sfc.script.content.slice(componentsOption.start, componentsOption.end),
'script',
componentsOption.start,
codeFeatures.navigation,
Expand All @@ -53,11 +53,11 @@ function* generateTemplateComponents(options: ScriptCodegenOptions): Generator<C
let nameType: Code | undefined;
if (options.sfc.script && options.scriptRanges?.exportDefault?.nameOption) {
const { nameOption } = options.scriptRanges.exportDefault;
nameType = options.sfc.script.content.substring(nameOption.start, nameOption.end);
nameType = options.sfc.script.content.slice(nameOption.start, nameOption.end);
}
else if (options.sfc.scriptSetup) {
const baseName = path.basename(options.fileName);
nameType = `'${options.scriptSetupRanges?.options.name ?? baseName.substring(0, baseName.lastIndexOf('.'))}'`;
nameType = `'${options.scriptSetupRanges?.options.name ?? baseName.slice(0, baseName.lastIndexOf('.'))}'`;
}
if (nameType) {
exps.push(
Expand Down Expand Up @@ -87,7 +87,7 @@ export function* generateTemplateDirectives(options: ScriptCodegenOptions): Gene
if (options.sfc.script && options.scriptRanges?.exportDefault?.directivesOption) {
const { directivesOption } = options.scriptRanges.exportDefault;
exps.push([
options.sfc.script.content.substring(directivesOption.start, directivesOption.end),
options.sfc.script.content.slice(directivesOption.start, directivesOption.end),
'script',
directivesOption.start,
codeFeatures.navigation,
Expand Down Expand Up @@ -198,7 +198,7 @@ export function* generateCssClassProperty(
];
yield `'`;
yield [
classNameWithDot.substring(1),
classNameWithDot.slice(1),
'style_' + styleIndex,
offset + 1,
codeFeatures.navigation,
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/codegen/template/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function* generateComponent(
node: CompilerDOM.ElementNode,
currentComponent: CompilerDOM.ElementNode | undefined
): Generator<Code> {
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
Expand Down Expand Up @@ -283,7 +283,7 @@ export function* generateElement(
componentCtxVar: string | undefined,
isVForChild: boolean
): Generator<Code> {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
22 changes: 11 additions & 11 deletions packages/language-core/lib/codegen/template/interpolation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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++) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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];
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/template/slotOutlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function* generateSlotOutlet(
currentComponent: CompilerDOM.ElementNode | undefined,
componentCtxVar: string | undefined
): Generator<Code> {
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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/codegen/template/templateChild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/template/vFor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/languagePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export function createVueLanguagePlugin<T>(
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,
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/parsers/scriptSetupRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/plugins/file-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/plugins/file-md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/plugins/file-vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(`</\\s*${hitBlock.type}\\s*>`);
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/plugins/vue-root-tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const plugin: VueLanguagePlugin = () => {
undefined,
block.startTagEnd,
block.endTagStart,
sfc.content.substring(
sfc.content.slice(
block.startTagEnd,
block.startTagEnd + offset
),
Expand All @@ -52,7 +52,7 @@ const plugin: VueLanguagePlugin = () => {
block.startTagEnd + offset,
{ structure: true },
],
sfc.content.substring(
sfc.content.slice(
block.startTagEnd + offset,
block.endTagStart
)
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/plugins/vue-template-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function* generate(templateAst: NonNullable<CompilerDOM.RootNode>): Generator<Co
const endCrt = prop.arg.loc.source[prop.arg.loc.source.length - 1]; // " | '
const start = prop.arg.loc.source.indexOf(endCrt) + 1;
const end = prop.arg.loc.source.lastIndexOf(endCrt);
const content = prop.arg.loc.source.substring(start, end);
const content = prop.arg.loc.source.slice(start, end);

yield `x { `;
yield [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ const plugin: VueLanguagePlugin = ctx => {
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
);
Expand Down
Loading

0 comments on commit 34e6982

Please sign in to comment.