From 3c3cd80fc6c3155451e67f5dc80bc8811368d9ce Mon Sep 17 00:00:00 2001 From: Tim van der Lippe Date: Mon, 26 Jul 2021 15:41:28 +0100 Subject: [PATCH] Remove FormatterInterface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This class was a remnant of the code structure during the Closure Compiler era. In ES modules, we no longer need a special class and instead can explicitly export the relevant functions and remove the interim class. R=szuend@chromium.org Bug: none Change-Id: Ib6a2bcaef9a1dad462f02a3411b9bef1ee989a34 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3053747 Commit-Queue: Tim van der Lippe Reviewed-by: Simon Zünd --- front_end/models/formatter/ScriptFormatter.ts | 64 +++++++++---------- front_end/models/formatter/SourceFormatter.ts | 4 +- .../models/formatter/formatter-legacy.ts | 3 - .../sources/InplaceFormatterEditorAction.ts | 2 +- 4 files changed, 32 insertions(+), 41 deletions(-) diff --git a/front_end/models/formatter/ScriptFormatter.ts b/front_end/models/formatter/ScriptFormatter.ts index d33ab64acd..41e3ee5ab4 100644 --- a/front_end/models/formatter/ScriptFormatter.ts +++ b/front_end/models/formatter/ScriptFormatter.ts @@ -36,42 +36,40 @@ import * as Platform from '../../core/platform/platform.js'; import type {FormatMapping, FormatResult} from './FormatterWorkerPool.js'; import {formatterWorkerPool} from './FormatterWorkerPool.js'; -export class FormatterInterface { - static format( - contentType: Common.ResourceType.ResourceType, mimeType: string, content: string, - callback: (arg0: string, arg1: FormatterSourceMapping) => Promise): void { - if (contentType.isDocumentOrScriptOrStyleSheet()) { - new ScriptFormatter(mimeType, content, callback); - } else { - new ScriptIdentityFormatter(mimeType, content, callback); - } +function locationToPosition(lineEndings: number[], lineNumber: number, columnNumber: number): number { + const position = lineNumber ? lineEndings[lineNumber - 1] + 1 : 0; + return position + columnNumber; +} + +function positionToLocation(lineEndings: number[], position: number): number[] { + const lineNumber = + Platform.ArrayUtilities.upperBound(lineEndings, position - 1, Platform.ArrayUtilities.DEFAULT_COMPARATOR); + let columnNumber; + if (!lineNumber) { + columnNumber = position; + } else { + columnNumber = position - lineEndings[lineNumber - 1] - 1; } - static locationToPosition(lineEndings: number[], lineNumber: number, columnNumber: number): number { - const position = lineNumber ? lineEndings[lineNumber - 1] + 1 : 0; - return position + columnNumber; - } - static positionToLocation(lineEndings: number[], position: number): number[] { - const lineNumber = - Platform.ArrayUtilities.upperBound(lineEndings, position - 1, Platform.ArrayUtilities.DEFAULT_COMPARATOR); - let columnNumber; - if (!lineNumber) { - columnNumber = position; - } else { - columnNumber = position - lineEndings[lineNumber - 1] - 1; - } - return [lineNumber, columnNumber]; + return [lineNumber, columnNumber]; +} + +export function format( + contentType: Common.ResourceType.ResourceType, mimeType: string, content: string, + callback: (arg0: string, arg1: FormatterSourceMapping) => Promise): void { + if (contentType.isDocumentOrScriptOrStyleSheet()) { + new ScriptFormatter(mimeType, content, callback); + } else { + new ScriptIdentityFormatter(mimeType, content, callback); } } -export class ScriptFormatter extends FormatterInterface { +export class ScriptFormatter { _mimeType: string; _originalContent: string; _callback: (arg0: string, arg1: FormatterSourceMapping) => Promise; constructor( mimeType: string, content: string, callback: (arg0: string, arg1: FormatterSourceMapping) => Promise) { - super(); - this._mimeType = mimeType; this._originalContent = content.replace(/\r\n?|[\n\u2028\u2029]/g, '\n').replace(/^\uFEFF/, ''); this._callback = callback; @@ -101,11 +99,9 @@ export class ScriptFormatter extends FormatterInterface { } } -class ScriptIdentityFormatter extends FormatterInterface { +class ScriptIdentityFormatter { constructor( mimeType: string, content: string, callback: (arg0: string, arg1: FormatterSourceMapping) => Promise) { - super(); - callback(content, new IdentityFormatterSourceMapping()); } } @@ -139,18 +135,16 @@ class FormatterSourceMappingImpl extends FormatterSourceMapping { } originalToFormatted(lineNumber: number, columnNumber?: number): number[] { - const originalPosition = - FormatterInterface.locationToPosition(this._originalLineEndings, lineNumber, columnNumber || 0); + const originalPosition = locationToPosition(this._originalLineEndings, lineNumber, columnNumber || 0); const formattedPosition = this._convertPosition(this._mapping.original, this._mapping.formatted, originalPosition || 0); - return FormatterInterface.positionToLocation(this._formattedLineEndings, formattedPosition); + return positionToLocation(this._formattedLineEndings, formattedPosition); } formattedToOriginal(lineNumber: number, columnNumber?: number): number[] { - const formattedPosition = - FormatterInterface.locationToPosition(this._formattedLineEndings, lineNumber, columnNumber || 0); + const formattedPosition = locationToPosition(this._formattedLineEndings, lineNumber, columnNumber || 0); const originalPosition = this._convertPosition(this._mapping.formatted, this._mapping.original, formattedPosition); - return FormatterInterface.positionToLocation(this._originalLineEndings, originalPosition || 0); + return positionToLocation(this._originalLineEndings, originalPosition || 0); } _convertPosition(positions1: number[], positions2: number[], position: number): number { diff --git a/front_end/models/formatter/SourceFormatter.ts b/front_end/models/formatter/SourceFormatter.ts index 8e5aface1e..bcb424defc 100644 --- a/front_end/models/formatter/SourceFormatter.ts +++ b/front_end/models/formatter/SourceFormatter.ts @@ -11,7 +11,7 @@ import * as TextUtils from '../text_utils/text_utils.js'; import * as Workspace from '../workspace/workspace.js'; import type {FormatterSourceMapping} from './ScriptFormatter.js'; -import {FormatterInterface} from './ScriptFormatter.js'; +import {format} from './ScriptFormatter.js'; const objectToFormattingResult = new WeakMap(); @@ -119,7 +119,7 @@ export class SourceFormatter { const resultPromise = new Promise(async resolve => { const {content} = await uiSourceCode.requestContent(); - FormatterInterface.format( + format( uiSourceCode.contentType(), uiSourceCode.mimeType(), content || '', async (formattedContent, formatterMapping) => { const cacheEntry = this._formattedSourceCodes.get(uiSourceCode); diff --git a/front_end/models/formatter/formatter-legacy.ts b/front_end/models/formatter/formatter-legacy.ts index a85a4772cd..4cad282acc 100644 --- a/front_end/models/formatter/formatter-legacy.ts +++ b/front_end/models/formatter/formatter-legacy.ts @@ -14,9 +14,6 @@ Formatter.FormatterWorkerPool = FormatterModule.FormatterWorkerPool.FormatterWor Formatter.formatterWorkerPool = FormatterModule.FormatterWorkerPool.formatterWorkerPool; -/** @interface */ -Formatter.Formatter = FormatterModule.ScriptFormatter.FormatterInterface; - /** @constructor */ Formatter.ScriptFormatter = FormatterModule.ScriptFormatter.ScriptFormatter; diff --git a/front_end/panels/sources/InplaceFormatterEditorAction.ts b/front_end/panels/sources/InplaceFormatterEditorAction.ts index 231b80051c..f38b32dd61 100644 --- a/front_end/panels/sources/InplaceFormatterEditorAction.ts +++ b/front_end/panels/sources/InplaceFormatterEditorAction.ts @@ -113,7 +113,7 @@ export class InplaceFormatterEditorAction implements EditorAction { _contentLoaded(uiSourceCode: Workspace.UISourceCode.UISourceCode, content: string): void { const highlighterType = uiSourceCode.mimeType(); - Formatter.ScriptFormatter.FormatterInterface.format( + Formatter.ScriptFormatter.format( uiSourceCode.contentType(), highlighterType, content, async (formattedContent, formatterMapping) => { this._formattingComplete(uiSourceCode, formattedContent, formatterMapping); });