Path: blob/main/extensions/copilot/src/extension/renameSuggestions/common/namingConvention.ts
13399 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45export enum NamingConvention {6/** example: camelCase */7CamelCase = 'camelCase',89/** example: PascalCase */10PascalCase = 'PascalCase',1112/** example: snake_case */13SnakeCase = 'snake_case',1415/** example: SCREAMING_SNAKE_CASE */16ScreamingSnakeCase = 'SCREAMING_SNAKE_CASE',1718/** example: Capital_snake_case */19CapitalSnakeCase = 'Capital_snake_case',2021/** example: kebab-case */22KebabCase = 'kebab-case',2324/** example: Capitalized */25Capitalized = 'Capitalized',2627/** example: ALLCAPS */28Uppercase = 'Uppercase',2930/**31* example: lowercase32*33* @remark could also be camel case, snake case, kebab case, e.g., `foo`34*/35LowerCase = 'lowercase',3637Unknown = 'Unknown',38}3940// Regular expressions for each naming convention4142export function guessNamingConvention(ident: string): NamingConvention {4344// lowercase45if (/^[a-z][a-z0-9]*$/.test(ident)) {46return NamingConvention.LowerCase;47}4849// camelCase50if (/^[a-z][a-zA-Z0-9]*$/.test(ident)) {51return NamingConvention.CamelCase;52}5354// snake_case55if (/^[a-z]+(_[a-z0-9]+)*$/.test(ident)) {56return NamingConvention.SnakeCase;57}5859// kebab-case60if (/^[a-z]+(-[a-z0-9]+)*$/.test(ident)) {61return NamingConvention.KebabCase;62}6364// Capitalized65if (/^[A-Z][a-z0-9]*$/.test(ident)) {66return NamingConvention.Capitalized;67}6869// SCREAMING_SNAKE_CASE70if (/^[A-Z0-9]+(_[A-Z0-9]+)+$/.test(ident)) {71return NamingConvention.ScreamingSnakeCase;72}7374// UPPERCASE75if (/^[A-Z]+$/.test(ident)) {76return NamingConvention.Uppercase;77}7879// PascalCase80if (/^[A-Z][a-zA-Z0-9]*$/.test(ident)) {81return NamingConvention.PascalCase;82}8384// Capital_snake_case85if (/^[A-Z][a-z0-9]*(_[a-z0-9]+)*$/.test(ident)) {86return NamingConvention.CapitalSnakeCase;87}8889return NamingConvention.Unknown;90}9192function chunksToCamelCase(chunks: string[]): string {93return chunks.map((chunk, i) => {94if (i === 0) {95return chunk.toLowerCase();96}9798return chunk.charAt(0).toUpperCase() + chunk.substring(1).toLowerCase();99}).join('');100}101102function chunksToPascalCase(chunks: string[]): string {103return chunks.map(chunk => chunk.charAt(0).toUpperCase() + chunk.substring(1).toLowerCase()).join('');104}105106function chunksToSnakeCase(chunks: string[]): string {107return chunks.map(chunk => chunk.toLowerCase()).join('_');108}109110function chunksToKebabCase(chunks: string[]): string {111return chunks.map(chunk => chunk.toLowerCase()).join('-');112}113114export function enforceNamingConvention(givenIdent: string, targetConvention: NamingConvention): string {115116const namingConvention = guessNamingConvention(givenIdent);117118if (namingConvention === targetConvention) {119return givenIdent;120} else {121const chunks = chunkUpIdentByConvention(givenIdent, namingConvention);122switch (targetConvention) {123case NamingConvention.CamelCase:124return chunksToCamelCase(chunks);125case NamingConvention.PascalCase:126return chunksToPascalCase(chunks);127case NamingConvention.SnakeCase:128return chunksToSnakeCase(chunks);129case NamingConvention.ScreamingSnakeCase:130return chunksToSnakeCase(chunks).toUpperCase();131case NamingConvention.CapitalSnakeCase:132return chunksToSnakeCase(chunks).charAt(0).toUpperCase() + chunksToSnakeCase(chunks).substring(1);133case NamingConvention.KebabCase:134return chunksToKebabCase(chunks);135case NamingConvention.Capitalized:136return chunksToCamelCase(chunks).charAt(0).toUpperCase() + chunksToCamelCase(chunks).substring(1);137case NamingConvention.Uppercase:138return givenIdent.toUpperCase();139case NamingConvention.LowerCase:140return givenIdent.toLowerCase();141case NamingConvention.Unknown:142return givenIdent;143}144}145}146147export function chunkUpIdentByConvention(ident: string, identConvention: NamingConvention): string[] {148switch (identConvention) {149case NamingConvention.CamelCase:150case NamingConvention.PascalCase:151return ident.split(/(?=[A-Z])/);152case NamingConvention.SnakeCase:153case NamingConvention.ScreamingSnakeCase:154case NamingConvention.CapitalSnakeCase:155case NamingConvention.KebabCase:156return ident.split(/[-_]/).map(chunk => chunk.toLowerCase());157case NamingConvention.Capitalized:158case NamingConvention.Uppercase:159case NamingConvention.LowerCase:160case NamingConvention.Unknown:161return [ident];162}163}164165166