From e6cc1d94a7ae45c81e92c645d827759727f3769f Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Thu, 15 Sep 2022 04:57:27 +0300 Subject: [PATCH 1/4] fix pluralize https://github.com/plurals/pluralize/issues/192#issuecomment-1079970233 --- package-lock.json | 11 +++++------ package.json | 2 +- test/extension.singleline.test.ts | 1 + 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index f107079..c6c03df 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "MIT", "dependencies": { "lodash": "^4.17.21", - "pluralize": "^8.0.0", + "pluralize": "github:plurals/pluralize", "typescript": "^4.6.4", "vscode-snippet-parser": "^0.0.5" }, @@ -3216,8 +3216,8 @@ }, "node_modules/pluralize": { "version": "8.0.0", - "resolved": "https://registry.npmjs.org/pluralize/-/pluralize-8.0.0.tgz", - "integrity": "sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA==", + "resolved": "git+ssh://git@github.com/plurals/pluralize.git#36f03cd2d573fa6d23e12e1529fa4627e2af74b4", + "license": "MIT", "engines": { "node": ">=4" } @@ -6619,9 +6619,8 @@ "dev": true }, "pluralize": { - "version": "8.0.0", - "resolved": "https://registry.npmjs.org/pluralize/-/pluralize-8.0.0.tgz", - "integrity": "sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA==" + "version": "git+ssh://git@github.com/plurals/pluralize.git#36f03cd2d573fa6d23e12e1529fa4627e2af74b4", + "from": "pluralize@github:plurals/pluralize" }, "prebuild-install": { "version": "7.1.0", diff --git a/package.json b/package.json index e04635e..b387698 100644 --- a/package.json +++ b/package.json @@ -152,7 +152,7 @@ }, "dependencies": { "lodash": "^4.17.21", - "pluralize": "^8.0.0", + "pluralize": "github:plurals/pluralize", "typescript": "^4.6.4", "vscode-snippet-parser": "^0.0.5" } diff --git a/test/extension.singleline.test.ts b/test/extension.singleline.test.ts index 7816818..d164bb8 100644 --- a/test/extension.singleline.test.ts +++ b/test/extension.singleline.test.ts @@ -102,6 +102,7 @@ describe('Single line template tests', () => { Test('let template with name - call expression | getSomethingCool(1, 2, 3){let} >> let somethingCool = getSomethingCool(1, 2, 3)') Test('let template with name - call expression | this.getSomethingCool(1, 2, 3){let} >> let somethingCool = this.getSomethingCool(1, 2, 3)') Test('forof template with array item name | usersList{forof} >> for(letuserofusersList){}', true) + Test('forof template with array item name 2 | cookies{forof} >> for(letcookieofcookies){}', true) }) describe('custom template tests', () => { From 11c40dd0a36c65b07d57b573f9963f6ad455a9b9 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Thu, 15 Sep 2022 04:57:42 +0300 Subject: [PATCH 2/4] enable proposal of multiple variants --- src/templates/varTemplates.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/templates/varTemplates.ts b/src/templates/varTemplates.ts index 7a00f13..c55c39b 100644 --- a/src/templates/varTemplates.ts +++ b/src/templates/varTemplates.ts @@ -5,7 +5,8 @@ import { BaseExpressionTemplate } from './baseTemplates' import { getConfigValue } from '../utils' export class VarTemplate extends BaseExpressionTemplate { - private static MethodCallRegex = /^(get|read|create|retrieve|select|modify|update|use|find)(?[A-Z].+?)(By.*)?$/ + private static MethodCallRegex = /^(get|read|create|retrieve|select|modify|update|use|find)(?[A-Z].+?)?$/ + private static CleanNameRegex = /((By|With|From).*$)|(Sync$)/ constructor(private keyword: 'var' | 'let' | 'const') { super(keyword) @@ -13,11 +14,12 @@ export class VarTemplate extends BaseExpressionTemplate { buildCompletionItem(node: ts.Node, indentSize?: number) { const inferVarNameEnabled = getConfigValue('inferVariableName') - const suggestedVarName = inferVarNameEnabled ? this.inferVarName(node) : undefined; + const suggestedVarName = inferVarNameEnabled ? this.inferVarName(node) : undefined + const nameSnippet = suggestedVarName?.length > 1 ? `\${1|${suggestedVarName.join(',')}|}` : `\${1:${suggestedVarName?.[0] ?? 'name'}}` return CompletionItemBuilder .create(this.keyword, node, indentSize) - .replace(`${this.keyword} \${1:${suggestedVarName ?? 'name'}} = {{expr}}$0`, true) + .replace(`${this.keyword} ${nameSnippet} = {{expr}}$0`, true) .build() } @@ -33,12 +35,16 @@ export class VarTemplate extends BaseExpressionTemplate { const buildVarName = (name: string) => name && _.lowerFirst(name) if (ts.isNewExpression(node)) { - return buildVarName(this.inferNewExpressionVar(node)) + return [buildVarName(this.inferNewExpressionVar(node))] } else if (ts.isCallExpression(node)) { const methodName = this.getMethodName(node) - const matches = VarTemplate.MethodCallRegex.exec(methodName) + const name = VarTemplate.MethodCallRegex.exec(methodName)?.groups?.name + const cleanerVariant = name?.replace(VarTemplate.CleanNameRegex, '') + if (!name) { + return + } - return buildVarName(matches?.groups?.name) + return [...cleanerVariant && cleanerVariant !== name ? [cleanerVariant] : [], name].map(buildVarName) } } From d65ff072ff90954398c7e5a91f833188fcb8bb4d Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Fri, 16 Sep 2022 11:31:28 +0300 Subject: [PATCH 3/4] lock pluralize commit --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index c6c03df..5f6b16f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "MIT", "dependencies": { "lodash": "^4.17.21", - "pluralize": "github:plurals/pluralize", + "pluralize": "github:plurals/pluralize#36f03cd2d573fa6d23e12e1529fa4627e2af74b4", "typescript": "^4.6.4", "vscode-snippet-parser": "^0.0.5" }, @@ -6620,7 +6620,7 @@ }, "pluralize": { "version": "git+ssh://git@github.com/plurals/pluralize.git#36f03cd2d573fa6d23e12e1529fa4627e2af74b4", - "from": "pluralize@github:plurals/pluralize" + "from": "pluralize@github:plurals/pluralize#36f03cd2d573fa6d23e12e1529fa4627e2af74b4" }, "prebuild-install": { "version": "7.1.0", diff --git a/package.json b/package.json index b387698..8b88b7a 100644 --- a/package.json +++ b/package.json @@ -152,7 +152,7 @@ }, "dependencies": { "lodash": "^4.17.21", - "pluralize": "github:plurals/pluralize", + "pluralize": "github:plurals/pluralize#36f03cd2d573fa6d23e12e1529fa4627e2af74b4", "typescript": "^4.6.4", "vscode-snippet-parser": "^0.0.5" } From d9e7d8bb8a9b09b016278b2ad32d95aa99997408 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Fri, 16 Sep 2022 11:34:06 +0300 Subject: [PATCH 4/4] make code cleaner! --- src/templates/varTemplates.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/templates/varTemplates.ts b/src/templates/varTemplates.ts index c55c39b..0fa0d13 100644 --- a/src/templates/varTemplates.ts +++ b/src/templates/varTemplates.ts @@ -44,7 +44,8 @@ export class VarTemplate extends BaseExpressionTemplate { return } - return [...cleanerVariant && cleanerVariant !== name ? [cleanerVariant] : [], name].map(buildVarName) + const uniqueValues = [...new Set([cleanerVariant, name])] + return uniqueValues.filter(x => x).map(buildVarName) } }