Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infer variable name improvements #72

Merged
merged 4 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
},
"dependencies": {
"lodash": "^4.17.21",
"pluralize": "^8.0.0",
"pluralize": "github:plurals/pluralize#36f03cd2d573fa6d23e12e1529fa4627e2af74b4",
"typescript": "^4.6.4",
"vscode-snippet-parser": "^0.0.5"
}
Expand Down
19 changes: 13 additions & 6 deletions src/templates/varTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ 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)(?<name>[A-Z].+?)(By.*)?$/
private static MethodCallRegex = /^(get|read|create|retrieve|select|modify|update|use|find)(?<name>[A-Z].+?)?$/
private static CleanNameRegex = /((By|With|From).*$)|(Sync$)/

constructor(private keyword: 'var' | 'let' | 'const') {
super(keyword)
}

buildCompletionItem(node: ts.Node, indentSize?: number) {
const inferVarNameEnabled = getConfigValue<boolean>('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()
}

Expand All @@ -33,12 +35,17 @@ 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)
const uniqueValues = [...new Set([cleanerVariant, name])]
return uniqueValues.filter(x => x).map(buildVarName)
}
}

Expand Down
1 change: 1 addition & 0 deletions test/extension.singleline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down