From c2680dfc9b91ad633d8590f3617cf121b295187b Mon Sep 17 00:00:00 2001 From: Peter Hale Date: Thu, 28 Dec 2023 13:58:31 -0700 Subject: [PATCH] chore: refactor vf LS to use arrow functions @W-14564471@ refactor package VF language server to use arrow functions where applicable --- .../fix-these-rules.json | 1 + .../src/languageModelCache.ts | 12 ++-- .../src/modes/cssMode.ts | 32 ++++----- .../src/modes/embeddedSupport.ts | 56 +++++++-------- .../src/modes/formatting.ts | 6 +- .../src/modes/htmlMode.ts | 38 +++++----- .../src/modes/javascriptMode.ts | 72 +++++++++---------- .../src/modes/languageModes.ts | 22 +++--- .../src/utils/arrays.ts | 9 +-- .../src/utils/edits.ts | 8 +-- .../src/utils/strings.ts | 36 ++++------ .../src/visualforceServer.ts | 28 ++++---- .../test/unit/embedded.test.ts | 10 +-- .../test/unit/formatting.test.ts | 16 ++--- .../test/unit/javascriptMode.test.ts | 4 +- .../test/unit/words.test.ts | 4 +- 16 files changed, 171 insertions(+), 183 deletions(-) diff --git a/packages/salesforcedx-visualforce-language-server/fix-these-rules.json b/packages/salesforcedx-visualforce-language-server/fix-these-rules.json index 2480da0c07..bc660cea16 100644 --- a/packages/salesforcedx-visualforce-language-server/fix-these-rules.json +++ b/packages/salesforcedx-visualforce-language-server/fix-these-rules.json @@ -1,5 +1,6 @@ { "rules": { + "prefer-arrow/prefer-arrow-functions": ["error", {}], "@typescript-eslint/no-unsafe-assignment": "warn", "@typescript-eslint/no-misused-promises": "warn", "@typescript-eslint/no-unsafe-argument": "warn", diff --git a/packages/salesforcedx-visualforce-language-server/src/languageModelCache.ts b/packages/salesforcedx-visualforce-language-server/src/languageModelCache.ts index 1da09b16b3..4f44cccf01 100644 --- a/packages/salesforcedx-visualforce-language-server/src/languageModelCache.ts +++ b/packages/salesforcedx-visualforce-language-server/src/languageModelCache.ts @@ -13,11 +13,11 @@ export interface LanguageModelCache { dispose(): void; } -export function getLanguageModelCache( +export const getLanguageModelCache = ( maxEntries: number, cleanupIntervalTimeInSec: number, parse: (document: TextDocument) => T -): LanguageModelCache { +): LanguageModelCache => { let languageModels: { [uri: string]: { version: number; @@ -44,7 +44,7 @@ export function getLanguageModelCache( } return { - get(document: TextDocument): T { + get: (document: TextDocument): T => { const version = document.version; const languageId = document.languageId; const languageModelInfo = languageModels[document.uri]; @@ -85,14 +85,14 @@ export function getLanguageModelCache( } return languageModel; }, - onDocumentRemoved(document: TextDocument) { + onDocumentRemoved: (document: TextDocument) => { const uri = document.uri; if (languageModels[uri]) { delete languageModels[uri]; nModels--; } }, - dispose() { + dispose: () => { if (typeof cleanupInterval !== 'undefined') { clearInterval(cleanupInterval); cleanupInterval = void 0; @@ -101,4 +101,4 @@ export function getLanguageModelCache( } } }; -} +}; diff --git a/packages/salesforcedx-visualforce-language-server/src/modes/cssMode.ts b/packages/salesforcedx-visualforce-language-server/src/modes/cssMode.ts index d651fd672b..ba2a78fdf1 100644 --- a/packages/salesforcedx-visualforce-language-server/src/modes/cssMode.ts +++ b/packages/salesforcedx-visualforce-language-server/src/modes/cssMode.ts @@ -14,9 +14,9 @@ import { import { CSS_STYLE_RULE, HTMLDocumentRegions } from './embeddedSupport'; import { ColorInformation, LanguageMode, Settings } from './languageModes'; -export function getCSSMode( +export const getCSSMode = ( documentRegions: LanguageModelCache -): LanguageMode { +): LanguageMode => { const cssLanguageService = getCSSLanguageService(); const embeddedCSSDocuments = getLanguageModelCache( 10, @@ -28,13 +28,13 @@ export function getCSSMode( ); return { - getId() { + getId: () => { return 'css'; }, - configure(options: any) { + configure: (options: any) => { cssLanguageService.configure(options && options.css); }, - doValidation(document: TextDocument, settings: Settings) { + doValidation: (document: TextDocument, settings: Settings) => { const embedded = embeddedCSSDocuments.get(document); return cssLanguageService.doValidation( embedded, @@ -42,7 +42,7 @@ export function getCSSMode( settings && settings.css ); }, - doComplete(document: TextDocument, position: Position) { + doComplete: (document: TextDocument, position: Position) => { const embedded = embeddedCSSDocuments.get(document); return cssLanguageService.doComplete( embedded, @@ -50,7 +50,7 @@ export function getCSSMode( cssStylesheets.get(embedded) ); }, - doHover(document: TextDocument, position: Position) { + doHover: (document: TextDocument, position: Position) => { const embedded = embeddedCSSDocuments.get(document); return cssLanguageService.doHover( embedded, @@ -58,7 +58,7 @@ export function getCSSMode( cssStylesheets.get(embedded) ); }, - findDocumentHighlight(document: TextDocument, position: Position) { + findDocumentHighlight: (document: TextDocument, position: Position) => { const embedded = embeddedCSSDocuments.get(document); return cssLanguageService.findDocumentHighlights( embedded, @@ -66,13 +66,13 @@ export function getCSSMode( cssStylesheets.get(embedded) ); }, - findDocumentSymbols(document: TextDocument) { + findDocumentSymbols: (document: TextDocument) => { const embedded = embeddedCSSDocuments.get(document); return cssLanguageService .findDocumentSymbols(embedded, cssStylesheets.get(embedded)) .filter(s => s.name !== CSS_STYLE_RULE); }, - findDefinition(document: TextDocument, position: Position) { + findDefinition: (document: TextDocument, position: Position) => { const embedded = embeddedCSSDocuments.get(document); return cssLanguageService.findDefinition( embedded, @@ -80,7 +80,7 @@ export function getCSSMode( cssStylesheets.get(embedded) ); }, - findReferences(document: TextDocument, position: Position) { + findReferences: (document: TextDocument, position: Position) => { const embedded = embeddedCSSDocuments.get(document); return cssLanguageService.findReferences( embedded, @@ -88,14 +88,14 @@ export function getCSSMode( cssStylesheets.get(embedded) ); }, - findDocumentColors(document: TextDocument) { + findDocumentColors: (document: TextDocument) => { const embedded = embeddedCSSDocuments.get(document); return cssLanguageService.findDocumentColors( embedded, cssStylesheets.get(embedded) ); }, - getColorPresentations(document: TextDocument, colorInfo: ColorInformation) { + getColorPresentations: (document: TextDocument, colorInfo: ColorInformation) => { const embedded = embeddedCSSDocuments.get(document); return cssLanguageService.getColorPresentations( embedded, @@ -103,13 +103,13 @@ export function getCSSMode( colorInfo ); }, - onDocumentRemoved(document: TextDocument) { + onDocumentRemoved: (document: TextDocument) => { embeddedCSSDocuments.onDocumentRemoved(document); cssStylesheets.onDocumentRemoved(document); }, - dispose() { + dispose: () => { embeddedCSSDocuments.dispose(); cssStylesheets.dispose(); } }; -} +}; diff --git a/packages/salesforcedx-visualforce-language-server/src/modes/embeddedSupport.ts b/packages/salesforcedx-visualforce-language-server/src/modes/embeddedSupport.ts index 46737f55b6..da9399ab34 100644 --- a/packages/salesforcedx-visualforce-language-server/src/modes/embeddedSupport.ts +++ b/packages/salesforcedx-visualforce-language-server/src/modes/embeddedSupport.ts @@ -40,10 +40,10 @@ interface EmbeddedRegion { attributeValue?: boolean; } -export function getDocumentRegions( +export const getDocumentRegions = ( languageService: LanguageService, document: TextDocument -): HTMLDocumentRegions { +): HTMLDocumentRegions => { const regions: EmbeddedRegion[] = []; const scanner = languageService.createScanner(document.getText()); let lastTagName: string; @@ -132,13 +132,12 @@ export function getDocumentRegions( getLanguagesInDocument: () => getLanguagesInDocument(document, regions), getImportedScripts: () => importedScripts }; -} - -function getLanguageRanges( +}; +const getLanguageRanges = ( document: TextDocument, regions: EmbeddedRegion[], range: Range -): LanguageRange[] { +): LanguageRange[] => { const result: LanguageRange[] = []; let currentPos = range ? range.start : Position.create(0, 0); let currentOffset = range ? document.offsetAt(range.start) : 0; @@ -179,12 +178,12 @@ function getLanguageRanges( }); } return result; -} +}; -function getLanguagesInDocument( +const getLanguagesInDocument = ( document: TextDocument, regions: EmbeddedRegion[] -): string[] { +): string[] => { const result = []; for (const region of regions) { if (region.languageId && result.indexOf(region.languageId) === -1) { @@ -196,13 +195,13 @@ function getLanguagesInDocument( } result.push('html'); return result; -} +}; -function getLanguageAtPosition( +const getLanguageAtPosition = ( document: TextDocument, regions: EmbeddedRegion[], position: Position -): string { +): string => { const offset = document.offsetAt(position); for (const region of regions) { if (region.start <= offset) { @@ -214,14 +213,14 @@ function getLanguageAtPosition( } } return 'html'; -} +}; -function getEmbeddedDocument( +const getEmbeddedDocument = ( document: TextDocument, contents: EmbeddedRegion[], languageId: string, ignoreAttributeValues: boolean -): TextDocument { +): TextDocument => { let currentPos = 0; const oldContent = document.getText(); let result = ''; @@ -258,9 +257,9 @@ function getEmbeddedDocument( document.version, result ); -} +}; -function getPrefix(c: EmbeddedRegion) { +const getPrefix = (c: EmbeddedRegion) => { if (c.attributeValue) { switch (c.languageId) { case 'css': @@ -268,8 +267,9 @@ function getPrefix(c: EmbeddedRegion) { } } return ''; -} -function getSuffix(c: EmbeddedRegion) { +}; + +const getSuffix = (c: EmbeddedRegion) => { if (c.attributeValue) { switch (c.languageId) { case 'css': @@ -279,16 +279,16 @@ function getSuffix(c: EmbeddedRegion) { } } return ''; -} +}; -function substituteWithWhitespace( +const substituteWithWhitespace = ( result: string, start: number, end: number, oldContent: string, before: string, after: string -) { +) => { let accumulatedWS = 0; result += before; for (let i = start + before.length; i < end; i++) { @@ -304,10 +304,9 @@ function substituteWithWhitespace( result = append(result, ' ', accumulatedWS - after.length); result += after; return result; -} +}; -// tslint:disable:no-bitwise -function append(result: string, str: string, n: number): string { +const append = (result: string, str: string, n: number): string => { while (n > 0) { if (n & 1) { result += str; @@ -316,13 +315,12 @@ function append(result: string, str: string, n: number): string { str += str; } return result; -} -// tslint:enable:no-bitwise +}; -function getAttributeLanguage(attributeName: string): string { +const getAttributeLanguage = (attributeName: string): string => { const match = attributeName.match(/^(style)$|^(on\w+)$/i); if (!match) { return null; } return match[1] ? 'css' : 'javascript'; -} +}; diff --git a/packages/salesforcedx-visualforce-language-server/src/modes/formatting.ts b/packages/salesforcedx-visualforce-language-server/src/modes/formatting.ts index 18c2ddd5e2..c09a658c56 100644 --- a/packages/salesforcedx-visualforce-language-server/src/modes/formatting.ts +++ b/packages/salesforcedx-visualforce-language-server/src/modes/formatting.ts @@ -17,14 +17,14 @@ import { applyEdits } from '../utils/edits'; import { isEOL } from '../utils/strings'; import { LanguageModes, Settings } from './languageModes'; -export function format( +export const format = ( languageModes: LanguageModes, document: TextDocument, formatRange: Range, formattingOptions: FormattingOptions, settings: Settings, enabledModes: { [mode: string]: boolean } -) { +) => { const result: TextEdit[] = []; const endPos = formatRange.end; @@ -143,4 +143,4 @@ export function format( } finally { languageModes.onDocumentRemoved(newDocument); } -} +}; diff --git a/packages/salesforcedx-visualforce-language-server/src/modes/htmlMode.ts b/packages/salesforcedx-visualforce-language-server/src/modes/htmlMode.ts index b81777f29f..b7fb84a121 100644 --- a/packages/salesforcedx-visualforce-language-server/src/modes/htmlMode.ts +++ b/packages/salesforcedx-visualforce-language-server/src/modes/htmlMode.ts @@ -16,25 +16,25 @@ import { Position, Range, TextDocument } from 'vscode-languageserver-types'; import { getLanguageModelCache } from '../languageModelCache'; import { LanguageMode, Settings } from './languageModes'; -export function getHTMLMode( +export const getHTMLMode = ( htmlLanguageService: HTMLLanguageService -): LanguageMode { +): LanguageMode => { let globalSettings: Settings = {}; const htmlDocuments = getLanguageModelCache(10, 60, document => htmlLanguageService.parseHTMLDocument(document) ); return { - getId() { + getId: () => { return 'html'; }, - configure(options: any) { + configure: (options: any) => { globalSettings = options; }, - doComplete( + doComplete: ( document: TextDocument, position: Position, settings: Settings = globalSettings - ) { + ) => { const options = settings && settings.visualforce && settings.visualforce.suggest; const doAutoComplete = @@ -51,38 +51,38 @@ export function getHTMLMode( options ); }, - doHover(document: TextDocument, position: Position) { + doHover: (document: TextDocument, position: Position) => { return htmlLanguageService.doHover( document, position, htmlDocuments.get(document) ); }, - findDocumentHighlight(document: TextDocument, position: Position) { + findDocumentHighlight: (document: TextDocument, position: Position) => { return htmlLanguageService.findDocumentHighlights( document, position, htmlDocuments.get(document) ); }, - findDocumentLinks( + findDocumentLinks: ( document: TextDocument, documentContext: DocumentContext - ) { + ) => { return htmlLanguageService.findDocumentLinks(document, documentContext); }, - findDocumentSymbols(document: TextDocument) { + findDocumentSymbols: (document: TextDocument) => { return htmlLanguageService.findDocumentSymbols( document, htmlDocuments.get(document) ); }, - format( + format: ( document: TextDocument, range: Range, formatParams: FormattingOptions, settings: Settings = globalSettings - ) { + ) => { let formatSettings: HTMLFormatConfiguration = settings && settings.visualforce && settings.visualforce.format; if (formatSettings) { @@ -99,7 +99,7 @@ export function getHTMLMode( formatSettings = merge(formatParams, formatSettings); return htmlLanguageService.format(document, range, formatSettings); }, - doAutoClose(document: TextDocument, position: Position) { + doAutoClose: (document: TextDocument, position: Position) => { const offset = document.offsetAt(position); const text = document.getText(); if (offset > 0 && text.charAt(offset - 1).match(/[>\/]/g)) { @@ -111,20 +111,20 @@ export function getHTMLMode( } return null; }, - onDocumentRemoved(document: TextDocument) { + onDocumentRemoved: (document: TextDocument) => { htmlDocuments.onDocumentRemoved(document); }, - dispose() { + dispose: () => { htmlDocuments.dispose(); } }; -} +}; -function merge(src: any, dst: any): any { +const merge = (src: any, dst: any): any => { for (const key in src) { if (src.hasOwnProperty(key)) { dst[key] = src[key]; } } return dst; -} +}; diff --git a/packages/salesforcedx-visualforce-language-server/src/modes/javascriptMode.ts b/packages/salesforcedx-visualforce-language-server/src/modes/javascriptMode.ts index 40f9a91ae7..657dc81b88 100644 --- a/packages/salesforcedx-visualforce-language-server/src/modes/javascriptMode.ts +++ b/packages/salesforcedx-visualforce-language-server/src/modes/javascriptMode.ts @@ -59,9 +59,9 @@ import { LanguageMode, Settings } from './languageModes'; const FILE_NAME = 'vscode://javascript/1'; // the same 'file' is used for all contents const JS_WORD_REGEX = /(-?\d*\.\d\w*)|([^\`\~\!\@\#\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)/g; -export function getJavascriptMode( +export const getJavascriptMode = ( documentRegions: LanguageModelCache -): LanguageMode { +): LanguageMode => { const jsDocuments = getLanguageModelCache(10, 60, document => documentRegions.get(document).getEmbeddedDocument('javascript') ); @@ -75,7 +75,7 @@ export function getJavascriptMode( }; let currentTextDocument: TextDocument; let scriptFileVersion = 0; - function updateCurrentTextDocument(doc: TextDocument) { + const updateCurrentTextDocument = (doc: TextDocument) => { if ( !currentTextDocument || doc.uri !== currentTextDocument.uri || @@ -84,7 +84,7 @@ export function getJavascriptMode( currentTextDocument = jsDocuments.get(doc); scriptFileVersion++; } - } + }; const host: LanguageServiceHost = { getCompilationSettings: () => compilerOptions, @@ -125,13 +125,13 @@ export function getJavascriptMode( let globalSettings: Settings = {}; return { - getId() { + getId: () => { return 'javascript'; }, - configure(options: any) { + configure: (options: any) => { globalSettings = options; }, - doValidation(document: TextDocument): Diagnostic[] { + doValidation: (document: TextDocument): Diagnostic[] =>{ updateCurrentTextDocument(document); const syntaxDiagnostics = jsLanguageService.getSyntacticDiagnostics( FILE_NAME @@ -149,7 +149,7 @@ export function getJavascriptMode( } ); }, - doComplete(document: TextDocument, position: Position): CompletionList { + doComplete: (document: TextDocument, position: Position): CompletionList => { updateCurrentTextDocument(document); const offset = currentTextDocument.offsetAt(position); const completions = jsLanguageService.getCompletionsAtPosition( @@ -184,7 +184,7 @@ export function getJavascriptMode( }) }; }, - doResolve(document: TextDocument, item: CompletionItem): CompletionItem { + doResolve: (document: TextDocument, item: CompletionItem): CompletionItem => { updateCurrentTextDocument(document); const details = jsLanguageService.getCompletionEntryDetails( FILE_NAME, @@ -202,7 +202,7 @@ export function getJavascriptMode( } return item; }, - doHover(document: TextDocument, position: Position): Hover { + doHover: (document: TextDocument, position: Position): Hover => { updateCurrentTextDocument(document); const info = jsLanguageService.getQuickInfoAtPosition( FILE_NAME, @@ -217,7 +217,7 @@ export function getJavascriptMode( } return null; }, - doSignatureHelp(document: TextDocument, position: Position): SignatureHelp { + doSignatureHelp: (document: TextDocument, position: Position): SignatureHelp => { updateCurrentTextDocument(document); const signHelp = jsLanguageService.getSignatureHelpItems( FILE_NAME, @@ -259,10 +259,10 @@ export function getJavascriptMode( } return null; }, - findDocumentHighlight( + findDocumentHighlight: ( document: TextDocument, position: Position - ): DocumentHighlight[] { + ): DocumentHighlight[] => { updateCurrentTextDocument(document); const highlights = jsLanguageService.getDocumentHighlights( FILE_NAME, @@ -284,7 +284,7 @@ export function getJavascriptMode( } return null; }, - findDocumentSymbols(document: TextDocument): SymbolInformation[] { + findDocumentSymbols: (document: TextDocument): SymbolInformation[] => { updateCurrentTextDocument(document); const items = jsLanguageService.getNavigationBarItems(FILE_NAME); if (items) { @@ -322,7 +322,7 @@ export function getJavascriptMode( } return null; }, - findDefinition(document: TextDocument, position: Position): Definition { + findDefinition: (document: TextDocument, position: Position): Definition => { updateCurrentTextDocument(document); const definition = jsLanguageService.getDefinitionAtPosition( FILE_NAME, @@ -340,7 +340,7 @@ export function getJavascriptMode( } return null; }, - findReferences(document: TextDocument, position: Position): Location[] { + findReferences: (document: TextDocument, position: Position): Location[] => { updateCurrentTextDocument(document); const references = jsLanguageService.getReferencesAtPosition( FILE_NAME, @@ -358,12 +358,12 @@ export function getJavascriptMode( } return null; }, - format( + format: ( document: TextDocument, range: Range, formatParams: FormattingOptions, settings: Settings = globalSettings - ): TextEdit[] { + ): TextEdit[] => { currentTextDocument = documentRegions .get(document) .getEmbeddedDocument('javascript', true); @@ -428,26 +428,26 @@ export function getJavascriptMode( } return null; }, - onDocumentRemoved(document: TextDocument) { + onDocumentRemoved: (document: TextDocument) => { jsDocuments.onDocumentRemoved(document); }, - dispose() { + dispose: () => { jsLanguageService.dispose(); jsDocuments.dispose(); } }; -} +}; -function convertRange( +const convertRange = ( document: TextDocument, span: { start: number; length: number } -): Range { +): Range => { const startPosition = document.positionAt(span.start); const endPosition = document.positionAt(span.start + span.length); return Range.create(startPosition, endPosition); -} +}; -function convertKind(kind: string): CompletionItemKind { +const convertKind = (kind: string): CompletionItemKind => { switch (kind) { case 'primitive type': case 'keyword': @@ -478,9 +478,9 @@ function convertKind(kind: string): CompletionItemKind { } return CompletionItemKind.Property; -} +}; -function convertSymbolKind(kind: string): SymbolKind { +const convertSymbolKind = (kind: string): SymbolKind => { switch (kind) { case 'var': case 'local var': @@ -505,13 +505,13 @@ function convertSymbolKind(kind: string): SymbolKind { return SymbolKind.Property; } return SymbolKind.Variable; -} +}; -function convertOptions( +const convertOptions = ( options: FormattingOptions, formatSettings: any, initialIndentLevel: number -): FormatCodeOptions { +): FormatCodeOptions => { return { ConvertTabsToSpaces: options.insertSpaces, TabSize: options.tabSize, @@ -559,13 +559,13 @@ function convertOptions( formatSettings && formatSettings.placeOpenBraceOnNewLineForControlBlocks ) }; -} +}; -function computeInitialIndent( +const computeInitialIndent = ( document: TextDocument, range: Range, options: FormattingOptions -) { +) => { const lineStart = document.offsetAt(Position.create(range.start.line, 0)); const content = document.getText(); @@ -584,12 +584,12 @@ function computeInitialIndent( i++; } return Math.floor(nChars / tabSize); -} +}; -function generateIndent(level: number, options: FormattingOptions) { +const generateIndent = (level: number, options: FormattingOptions) => { if (options.insertSpaces) { return repeat(' ', level * options.tabSize); } else { return repeat('\t', level); } -} +}; diff --git a/packages/salesforcedx-visualforce-language-server/src/modes/languageModes.ts b/packages/salesforcedx-visualforce-language-server/src/modes/languageModes.ts index 18ef52164d..6eda6f9344 100644 --- a/packages/salesforcedx-visualforce-language-server/src/modes/languageModes.ts +++ b/packages/salesforcedx-visualforce-language-server/src/modes/languageModes.ts @@ -113,9 +113,9 @@ export interface LanguageModeRange extends Range { attributeValue?: boolean; } -export function getLanguageModes(supportedLanguages: { +export const getLanguageModes = (supportedLanguages: { [languageId: string]: boolean; -}): LanguageModes { +}): LanguageModes => { const htmlLanguageService = getHTMLLanguageService(); const documentRegions = getLanguageModelCache( 10, @@ -135,10 +135,10 @@ export function getLanguageModes(supportedLanguages: { modes['javascript'] = getJavascriptMode(documentRegions); } return { - getModeAtPosition( + getModeAtPosition: ( document: TextDocument, position: Position - ): LanguageMode { + ): LanguageMode => { const languageId = documentRegions .get(document) .getLanguageAtPosition(position); @@ -147,7 +147,7 @@ export function getLanguageModes(supportedLanguages: { } return null; }, - getModesInRange(document: TextDocument, range: Range): LanguageModeRange[] { + getModesInRange: (document: TextDocument, range: Range): LanguageModeRange[] => { return documentRegions .get(document) .getLanguageRanges(range) @@ -160,7 +160,7 @@ export function getLanguageModes(supportedLanguages: { }; }); }, - getAllModesInDocument(document: TextDocument): LanguageMode[] { + getAllModesInDocument: (document: TextDocument): LanguageMode[] => { const result = []; for (const languageId of documentRegions .get(document) @@ -172,7 +172,7 @@ export function getLanguageModes(supportedLanguages: { } return result; }, - getAllModes(): LanguageMode[] { + getAllModes: (): LanguageMode[] => { const result = []; for (const languageId in modes) { const mode = modes[languageId]; @@ -182,16 +182,16 @@ export function getLanguageModes(supportedLanguages: { } return result; }, - getMode(languageId: string): LanguageMode { + getMode: (languageId: string): LanguageMode => { return modes[languageId]; }, - onDocumentRemoved(document: TextDocument) { + onDocumentRemoved: (document: TextDocument) => { modelCaches.forEach(mc => mc.onDocumentRemoved(document)); for (const mode in modes) { modes[mode].onDocumentRemoved(document); } }, - dispose(): void { + dispose: (): void => { modelCaches.forEach(mc => mc.dispose()); modelCaches = []; for (const mode in modes) { @@ -200,4 +200,4 @@ export function getLanguageModes(supportedLanguages: { modes = {}; } }; -} +}; diff --git a/packages/salesforcedx-visualforce-language-server/src/utils/arrays.ts b/packages/salesforcedx-visualforce-language-server/src/utils/arrays.ts index 50f19fbc0c..2e792872b5 100644 --- a/packages/salesforcedx-visualforce-language-server/src/utils/arrays.ts +++ b/packages/salesforcedx-visualforce-language-server/src/utils/arrays.ts @@ -5,11 +5,8 @@ *--------------------------------------------------------------------------------------------*/ 'use strict'; -export function pushAll(to: T[], from: T[]) { +export const pushAll = (to: T[], from: T[]) => { if (from) { - // tslint:disable-next-line:prefer-for-of - for (let i = 0; i < from.length; i++) { - to.push(from[i]); - } + to.push(...from); } -} +}; diff --git a/packages/salesforcedx-visualforce-language-server/src/utils/edits.ts b/packages/salesforcedx-visualforce-language-server/src/utils/edits.ts index d6a60ea740..9d8484e025 100644 --- a/packages/salesforcedx-visualforce-language-server/src/utils/edits.ts +++ b/packages/salesforcedx-visualforce-language-server/src/utils/edits.ts @@ -7,7 +7,7 @@ import { Position, TextDocument, TextEdit } from 'vscode-languageserver-types'; -export function applyEdits(document: TextDocument, edits: TextEdit[]): string { +export const applyEdits = (document: TextDocument, edits: TextEdit[]): string => { let text = document.getText(); const sortedEdits = edits.sort((a, b) => { const startDiff = comparePositions(a.range.start, b.range.start); @@ -25,12 +25,12 @@ export function applyEdits(document: TextDocument, edits: TextEdit[]): string { text.substring(endOffset, text.length); }); return text; -} +}; -function comparePositions(p1: Position, p2: Position) { +const comparePositions = (p1: Position, p2: Position) => { const diff = p2.line - p1.line; if (diff === 0) { return p2.character - p1.character; } return diff; -} +}; diff --git a/packages/salesforcedx-visualforce-language-server/src/utils/strings.ts b/packages/salesforcedx-visualforce-language-server/src/utils/strings.ts index 74d154e63a..2011976920 100644 --- a/packages/salesforcedx-visualforce-language-server/src/utils/strings.ts +++ b/packages/salesforcedx-visualforce-language-server/src/utils/strings.ts @@ -5,11 +5,11 @@ *--------------------------------------------------------------------------------------------*/ 'use strict'; -export function getWordAtText( +export const getWordAtText = ( text: string, offset: number, wordDefinition: RegExp -): { start: number; length: number } { +): { start: number; length: number } => { let lineStart = offset; while (lineStart > 0 && !isNewlineCharacter(text.charCodeAt(lineStart - 1))) { lineStart--; @@ -30,24 +30,17 @@ export function getWordAtText( } return { start: offset, length: 0 }; -} +}; -export function startsWith(haystack: string, needle: string): boolean { +export const startsWith = (haystack: string, needle: string): boolean => { if (haystack.length < needle.length) { return false; } - for (let i = 0; i < needle.length; i++) { - if (haystack[i] !== needle[i]) { - return false; - } - } - - return true; -} + return haystack.startsWith(needle); +}; -// tslint:disable:no-bitwise -export function repeat(value: string, count: number) { +export const repeat = (value: string, count: number): string => { let s = ''; while (count > 0) { if ((count & 1) === 1) { @@ -57,19 +50,18 @@ export function repeat(value: string, count: number) { count = count >>> 1; } return s; -} -// tslint:enable:no-bitwise +}; -export function isWhitespaceOnly(str: string) { +export const isWhitespaceOnly = (str: string): boolean => { return /^\s*$/.test(str); -} +}; -export function isEOL(content: string, offset: number) { +export const isEOL = (content: string, offset: number): boolean => { return isNewlineCharacter(content.charCodeAt(offset)); -} +}; const CR = '\r'.charCodeAt(0); const NL = '\n'.charCodeAt(0); -export function isNewlineCharacter(charCode: number) { +export const isNewlineCharacter = (charCode: number): boolean => { return charCode === CR || charCode === NL; -} +}; diff --git a/packages/salesforcedx-visualforce-language-server/src/visualforceServer.ts b/packages/salesforcedx-visualforce-language-server/src/visualforceServer.ts index 1c980e511c..9b123edd6f 100644 --- a/packages/salesforcedx-visualforce-language-server/src/visualforceServer.ts +++ b/packages/salesforcedx-visualforce-language-server/src/visualforceServer.ts @@ -87,10 +87,10 @@ documents.onDidClose(e => { delete documentSettings[e.document.uri]; }); -function getDocumentSettings( +const getDocumentSettings = ( textDocument: TextDocument, needsDocumentSettings: () => boolean -): Thenable { +): Thenable => { if (scopedSettingsSupport && needsDocumentSettings()) { let promise = documentSettings[textDocument.uri]; if (!promise) { @@ -110,7 +110,7 @@ function getDocumentSettings( return promise; } return Promise.resolve(void 0); -} +}; // After the server has started the client sends an initilize request. The server receives // in the passed params the rootPath of the workspace plus the client capabilites @@ -132,13 +132,13 @@ connection.onInitialize( languageModes.dispose(); }); - function hasClientCapability(...keys: string[]) { + const hasClientCapability = (...keys: string[]) => { let c = params.capabilities; for (let i = 0; c && i < keys.length; i++) { c = c[keys[i]]; } return !!c; - } + }; clientSnippetSupport = hasClientCapability( 'textDocument', @@ -231,27 +231,27 @@ documents.onDidClose(event => { connection.sendDiagnostics({ uri: event.document.uri, diagnostics: [] }); }); -function cleanPendingValidation(textDocument: TextDocument): void { +const cleanPendingValidation = (textDocument: TextDocument): void => { const request = pendingValidationRequests[textDocument.uri]; if (request) { clearTimeout(request); delete pendingValidationRequests[textDocument.uri]; } -} +}; -function triggerValidation(textDocument: TextDocument): void { +const triggerValidation = (textDocument: TextDocument): void => { cleanPendingValidation(textDocument); pendingValidationRequests[textDocument.uri] = setTimeout(() => { delete pendingValidationRequests[textDocument.uri]; // tslint:disable-next-line:no-floating-promises validateTextDocument(textDocument); }, validationDelayMs); -} +}; -function isValidationEnabled( +const isValidationEnabled = ( languageId: string, settings: Settings = globalSettings -) { +) => { const validationSettings = settings && settings.visualforce && settings.visualforce.validate; if (validationSettings) { @@ -261,9 +261,9 @@ function isValidationEnabled( ); } return true; -} +}; -async function validateTextDocument(textDocument: TextDocument) { +const validateTextDocument = async (textDocument: TextDocument) => { const diagnostics: Diagnostic[] = []; if (textDocument.languageId === 'html') { const modes = languageModes.getAllModesInDocument(textDocument); @@ -277,7 +277,7 @@ async function validateTextDocument(textDocument: TextDocument) { }); } connection.sendDiagnostics({ uri: textDocument.uri, diagnostics }); -} +}; connection.onCompletion(async textDocumentPosition => { const document = documents.get(textDocumentPosition.textDocument.uri); diff --git a/packages/salesforcedx-visualforce-language-server/test/unit/embedded.test.ts b/packages/salesforcedx-visualforce-language-server/test/unit/embedded.test.ts index ab82423d19..2d04d07c56 100644 --- a/packages/salesforcedx-visualforce-language-server/test/unit/embedded.test.ts +++ b/packages/salesforcedx-visualforce-language-server/test/unit/embedded.test.ts @@ -13,7 +13,7 @@ import * as embeddedSupport from '../../src/modes/embeddedSupport'; describe('HTML Embedded Support', () => { const htmlLanguageService = getLanguageService(); - function assertLanguageId(value: string, expectedLanguageId: string): void { + const assertLanguageId = (value: string, expectedLanguageId: string): void => { const offset = value.indexOf('|'); value = value.substr(0, offset) + value.substr(offset + 1); @@ -33,13 +33,13 @@ describe('HTML Embedded Support', () => { const languageId = docRegions.getLanguageAtPosition(position); assert.equal(languageId, expectedLanguageId); - } + }; - function assertEmbeddedLanguageContent( + const assertEmbeddedLanguageContent = ( value: string, languageId: string, expectedContent: string - ): void { + ): void => { const document = TextDocument.create( 'test://test/test.html', 'html', @@ -53,7 +53,7 @@ describe('HTML Embedded Support', () => { ); const content = docRegions.getEmbeddedDocument(languageId); assert.equal(content.getText(), expectedContent); - } + }; it('Should handle styles tag', () => { assertLanguageId('|', 'html'); diff --git a/packages/salesforcedx-visualforce-language-server/test/unit/formatting.test.ts b/packages/salesforcedx-visualforce-language-server/test/unit/formatting.test.ts index f0f990326b..4737972d0b 100644 --- a/packages/salesforcedx-visualforce-language-server/test/unit/formatting.test.ts +++ b/packages/salesforcedx-visualforce-language-server/test/unit/formatting.test.ts @@ -29,13 +29,13 @@ describe('HTML Embedded Formatting', () => { jest.useRealTimers(); }); - function assertFormat( + const assertFormat = ( value: string, expected: string, options?: any, formatOptions?: FormattingOptions, message?: string - ): void { + ): void => { const languageModes = getLanguageModes({ css: true, javascript: true }); if (options) { languageModes.getAllModes().forEach(m => m.configure(options)); @@ -79,14 +79,14 @@ describe('HTML Embedded Formatting', () => { const actual = applyEdits(document, result); assert.equal(actual, expected, message); - } + }; - function assertFormatWithFixture( + const assertFormatWithFixture = ( fixtureName: string, expectedPath: string, options?: any, formatOptions?: FormattingOptions - ): void { + ): void => { const input = fs .readFileSync(path.join(__dirname, 'fixtures', 'inputs', fixtureName)) .toString(); @@ -94,7 +94,7 @@ describe('HTML Embedded Formatting', () => { .readFileSync(path.join(__dirname, 'fixtures', 'expected', expectedPath)) .toString(); assertFormat(input, expected, options, formatOptions, expectedPath); - } + }; it('Should handle HTML only', () => { assertFormat( @@ -220,7 +220,7 @@ describe('HTML Embedded Formatting', () => { }); }); -function applyEdits(document: TextDocument, edits: TextEdit[]): string { +const applyEdits = (document: TextDocument, edits: TextEdit[]): string => { let text = document.getText(); const sortedEdits = edits.sort((a, b) => { const startDiff = @@ -243,4 +243,4 @@ function applyEdits(document: TextDocument, edits: TextEdit[]): string { lastOffset = startOffset; }); return text; -} +}; diff --git a/packages/salesforcedx-visualforce-language-server/test/unit/javascriptMode.test.ts b/packages/salesforcedx-visualforce-language-server/test/unit/javascriptMode.test.ts index e44fcc5484..7e79b32d8e 100644 --- a/packages/salesforcedx-visualforce-language-server/test/unit/javascriptMode.test.ts +++ b/packages/salesforcedx-visualforce-language-server/test/unit/javascriptMode.test.ts @@ -15,7 +15,7 @@ import { getJavascriptMode } from '../../src/modes/javascriptMode'; describe('HTML Javascript Support', () => { const htmlLanguageService = getLanguageService(); - function assertCompletions(value: string, expectedProposals: string[]): void { + const assertCompletions = (value: string, expectedProposals: string[]): void => { const offset = value.indexOf('|'); value = value.substr(0, offset) + value.substr(offset + 1); @@ -45,7 +45,7 @@ describe('HTML Javascript Support', () => { 'Not found:' + expected + ' is ' + actualLabels.join(', ') ); } - } + }; beforeAll(() => { jest.useFakeTimers(); diff --git a/packages/salesforcedx-visualforce-language-server/test/unit/words.test.ts b/packages/salesforcedx-visualforce-language-server/test/unit/words.test.ts index 1a79bac0c4..4e936bcc3e 100644 --- a/packages/salesforcedx-visualforce-language-server/test/unit/words.test.ts +++ b/packages/salesforcedx-visualforce-language-server/test/unit/words.test.ts @@ -11,7 +11,7 @@ import * as words from '../../src/utils/strings'; describe('Words', () => { const wordRegex = /(-?\d*\.\d\w*)|([^\`\~\!\@\#\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)/g; - function assertWord(value: string, expected: string): void { + const assertWord = (value: string, expected: string): void => { const offset = value.indexOf('|'); value = value.substr(0, offset) + value.substr(offset + 1); @@ -19,7 +19,7 @@ describe('Words', () => { assert(actualRange.start <= offset); assert(actualRange.start + actualRange.length >= offset); assert.equal(value.substr(actualRange.start, actualRange.length), expected); - } + }; it('Should handle basic words', () => { assertWord('|var x1 = new F(a, b);', 'var');