Skip to content

Commit

Permalink
feat: 类方法、生成器函数参数匹配优化
Browse files Browse the repository at this point in the history
Co-authored-by: ygqygq2 <ygqygq2@qq.com>
  • Loading branch information
ygqygq2 committed Apr 21, 2024
1 parent a7b46aa commit 11dd825
Show file tree
Hide file tree
Showing 25 changed files with 214 additions and 50 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

All notable changes to the "turbo-file-header" extension will be documented in this file.

# [0.2.3]

## 新增功能 🌱

- feat: 函数注释支持 go

## 功能优化 🚀

- refactor: js/ts 匹配更准确,支持可选参数,默认参数

# [0.2.2]

## 新增功能 🌱
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class ClassA {
/**
* @description
* @return default {auto}
* @param a {string}
* @param [b='b'] {string}
* @param [c] {string}
*/
get func(a: string, b:string = 'b', c?: string) {
return a + b + c;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ClassA {
get func(a: string, b:string = 'b', c?: string) {
return a + b + c;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ClassA {
get a() {
return 'test';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ClassA {
/**
* @description
* @return default {auto}
*/
get a() {
return 'test';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ClassA {
/**
* @description
* @return default {auto}
*/
get a() {
return 'test';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ClassA {
get a() {
return 'test';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class classA {
/**
* @description
* @return default {string}
* @param a {string}
* @param [b='b'] {any}
* @param [c] {string}
*/
public func(a: string, b = 'b', c?: string): string {
return a + b + c;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class classA {
public func(a: string, b = 'b', c?: string): string {
return a + b + c;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class classA {
public func(a, b = 'b') {
return a + b;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class classA {
/**
* @description
* @return default {auto}
* @param a {any}
* @param [b='b'] {any}
*/
public func(a, b = 'b') {
return a + b;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class classA {
/**
* @description
* @return default {string}
* @param a {any}
* @param [b='b'] {any}
*/
public func(a, b = 'b'): string {
return a + b;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class classA {
public func(a, b = 'b'): string {
return a + b;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @description
* @return default {Generator<string>}
* @param a {string}
* @param [b='b'] {any}
* @param [c] {string}
*/
export function* func(a: string, b = 'b', c?: string): Generator<string> {
console.log(a + b);
yield a + b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function* func(a: string, b = 'b', c?: string): Generator<string> {
console.log(a + b);
yield a + b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @description
* @return default {Generator<string>}
* @param a {string}
* @param b {string}
*/
export function* func(a: string, b: string): Generator<string> {
console.log(a + b);
yield a + b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function* func(a: string, b: string): Generator<string> {
console.log(a + b);
yield a + b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function* func(a, b) {
console.log(a + b);
yield a + b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @description
* @return default {auto}
* @param a {any}
* @param b {any}
*/
export function* func(a, b) {
console.log(a + b);
yield a + b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @description
* @return default {auto}
* @param a {any}
* @param b {any}
*/
export function* func(a, b) {
console.log(a + b);
yield a + b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function* func(a, b) {
console.log(a + b);
yield a + b;
}
71 changes: 23 additions & 48 deletions src/function-params-parser/ts-splitParams.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LanguageFunctionCommentSettings } from '@/typings/types';

import { ParamsInfo, ReturnInfo } from './types';
import { ParamsInfo } from './types';

export function splitParams(
paramsStr: string,
Expand All @@ -16,57 +16,32 @@ export function splitParams(
bracketCount++;
} else if (char === ')' || char === ']' || char === '}' || char === '>') {
bracketCount--;
} else if (char === ',' && bracketCount === 0) {
const paramStr = paramsStr.slice(paramStartIndex, i);
const colonIndex = paramStr.indexOf(':');
const equalIndex = paramStr.indexOf('=');
const questionIndex = paramStr.indexOf('?');
const name = paramStr
.slice(
0,
questionIndex !== -1 ? questionIndex : colonIndex !== -1 ? colonIndex : paramStr.length,
)
} else if (
(char === ',' && bracketCount === 0) ||
(i === paramsStr.length - 1 && bracketCount === 0)
) {
const paramStr = paramsStr
.slice(paramStartIndex, i === paramsStr.length - 1 ? i + 1 : i)
.trim();
const type =
colonIndex !== -1
? paramStr.slice(colonIndex + 1, equalIndex !== -1 ? equalIndex : paramStr.length).trim()
: defaultParamType;
const paramInfo: any = { type, description: '' };
if (equalIndex !== -1) {
paramInfo.defaultValue = paramStr.slice(equalIndex + 1).trim();
}
if (questionIndex !== -1) {
paramInfo.optional = true;
}
if (name) {
params[name] = paramInfo;
const normalParamPattern = /^(\w+)\s*:\s*([^,\s]*)$/;
const defaultParamPattern = /^(\w+)(?:\s*:\s*(.*?))?\s*=\s*([^,\s]*)$/;
const optionalParamPattern = /^(\w+)\?\s*:\s*([^,\s]*)$/;
const noTypeParamPattern = /^(\w+)$/;
let match;
if ((match = normalParamPattern.exec(paramStr))) {
const type = match[2].trim() || defaultParamType;
params[match[1]] = { type, description: '' };
} else if ((match = defaultParamPattern.exec(paramStr))) {
const type = match[2] ? match[2].trim() : defaultParamType;
params[match[1]] = { type, defaultValue: match[3].trim(), description: '' };
} else if ((match = optionalParamPattern.exec(paramStr))) {
const type = match[2].trim() || defaultParamType;
params[match[1]] = { type, optional: true, description: '' };
} else if ((match = noTypeParamPattern.exec(paramStr))) {
params[match[1]] = { type: defaultParamType, description: '' };
}
paramStartIndex = i + 1;
}
}
const paramStr = paramsStr.slice(paramStartIndex);
const colonIndex = paramStr.indexOf(':');
const equalIndex = paramStr.indexOf('=');
const questionIndex = paramStr.indexOf('?');
const name = paramStr
.slice(
0,
questionIndex !== -1 ? questionIndex : colonIndex !== -1 ? colonIndex : paramStr.length,
)
.trim();
const type =
colonIndex !== -1
? paramStr.slice(colonIndex + 1, equalIndex !== -1 ? equalIndex : paramStr.length).trim()
: defaultParamType;
const paramInfo: any = { type, description: '' };
if (equalIndex !== -1) {
paramInfo.defaultValue = paramStr.slice(equalIndex + 1).trim();
}
if (questionIndex !== -1) {
paramInfo.optional = true;
}
if (name) {
params[name] = paramInfo;
}
return params;
}
9 changes: 7 additions & 2 deletions src/function-params-parser/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import ts from 'typescript';
import * as vscode from 'vscode';

export type ParamType = { type: string; description: string };
export type ParamInfo = {
type: string;
description: string;
optional?: boolean;
defaultValue?: string;
};

export interface ParamsInfo {
[key: string]: { type: string; description: string; optional?: boolean; defaultValue?: string };
[key: string]: ParamInfo;
}

export interface ReturnInfo {
Expand Down
6 changes: 6 additions & 0 deletions src/test/suite/ts-classMethodComment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ const testInfo: TestInfo = [
{ fileName: 'class-construct-with-params-type.ts', cursorLine: 1 },
{ fileName: 'class-construct-without-params-type.js', cursorLine: 1 },
{ fileName: 'class-construct-without-params-type.ts', cursorLine: 1 },
{ fileName: 'class-get-optional-params-with-type.ts', cursorLine: 1 },
{ fileName: 'class-get-without-params-type.js', cursorLine: 1 },
{ fileName: 'class-get-without-params-type.ts', cursorLine: 1 },
{ fileName: 'class-public-optional-params-with-type.ts', cursorLine: 1 },
{ fileName: 'class-public-optional-params-without-type.js', cursorLine: 1 },
{ fileName: 'class-public-optional-params-without-type.ts', cursorLine: 1 },
{ fileName: 'class-private-without-params-type.js', cursorLine: 1 },
{ fileName: 'class-private-without-params-type.ts', cursorLine: 1 },
{ fileName: 'class-public-with-params-type.ts', cursorLine: 1 },
Expand Down
17 changes: 17 additions & 0 deletions src/test/suite/ts-generatorFunctionComment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { functionCommentTester } from './common/functionCommentTester';
import { TestInfo } from './types';

const testInfo: TestInfo = [
{
testName: 'ts-generatorFunction',
workspaceName: 'function-comment-for-ts',
files: [
{ fileName: 'generator-function-optional-params-type.ts', cursorLine: 0 },
{ fileName: 'generator-function-with-params-type.ts', cursorLine: 0 },
{ fileName: 'generator-function-without-params-type.ts', cursorLine: 0 },
{ fileName: 'generator-function-without-params-type.js', cursorLine: 0 },
],
},
];

functionCommentTester(testInfo);

0 comments on commit 11dd825

Please sign in to comment.