From 2bd978fad9169a44c71283af297f1069ab37d82d Mon Sep 17 00:00:00 2001 From: Mats Larsen Date: Wed, 1 Jun 2022 14:17:36 +0200 Subject: [PATCH] Address review comments --- docs/AddingAFormatter.md | 5 +++-- lib/formatters/base.interfaces.ts | 4 ++-- lib/formatters/base.ts | 10 ++-------- lib/formatters/clang-format.ts | 5 +++-- lib/formatters/dartformat.ts | 5 +++-- lib/formatters/gofmt.ts | 5 +++-- lib/formatters/rustfmt.ts | 5 +++-- 7 files changed, 19 insertions(+), 20 deletions(-) diff --git a/docs/AddingAFormatter.md b/docs/AddingAFormatter.md index 4fce8ce85..8cd0bc97b 100644 --- a/docs/AddingAFormatter.md +++ b/docs/AddingAFormatter.md @@ -8,7 +8,8 @@ - Add a `lib/formatters/.ts` file using the template below, replacing `Type` and `type` as appropriate ```js - import {BaseFormatter, ExecResult} from './base'; + import {UnprocessedExecResult} from '../../types/execution/execution.interfaces'; + import {BaseFormatter} from './base'; import {FormatOptions} from './base.interfaces'; export class TypeFormatter extends BaseFormatter { @@ -20,7 +21,7 @@ - The value returned by `key` above corresponds to the `type` property you set in the compiler-explorer properties configuration file. - - Tweak `format(source: string, options: FormatOptions): Promise` and + - Tweak `format(source: string, options: FormatOptions): Promise` and `isValidStyle(style: string): boolean` as necessary. See the JSDoc for `format` and the implementations for other formatters to get a further understanding of how to implement `format(source, options)`. diff --git a/lib/formatters/base.interfaces.ts b/lib/formatters/base.interfaces.ts index 02b8d6c10..32f4b0cd8 100644 --- a/lib/formatters/base.interfaces.ts +++ b/lib/formatters/base.interfaces.ts @@ -32,8 +32,8 @@ export interface FormatterInfo { versionReExp?: string; } -export interface FormatOptions { +export type FormatOptions = { useSpaces: boolean; tabWidth: number; baseStyle: string; -} +}; diff --git a/lib/formatters/base.ts b/lib/formatters/base.ts index 4ebdf893d..6d02fe1e3 100644 --- a/lib/formatters/base.ts +++ b/lib/formatters/base.ts @@ -22,17 +22,11 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. +import {UnprocessedExecResult} from '../../types/execution/execution.interfaces'; import * as exec from '../exec'; import {FormatOptions, FormatterInfo} from './base.interfaces'; -// TODO(supergrecko): initial type for ../exec.js exec method -export type ExecResult = { - code: number; - stdout: string; - stderr: string; -}; - export abstract class BaseFormatter { public formatterInfo: FormatterInfo; @@ -46,7 +40,7 @@ export abstract class BaseFormatter { * This method should construct the command line arguments and call the formatter executable with the constructed * arguments, returning the execution result */ - async format(source: string, options: FormatOptions): Promise { + async format(source: string, options: FormatOptions): Promise { const args = [`--style=${options.baseStyle}`]; return await exec.execute(this.formatterInfo.exe, args, {input: source}); } diff --git a/lib/formatters/clang-format.ts b/lib/formatters/clang-format.ts index 703a32598..870516178 100644 --- a/lib/formatters/clang-format.ts +++ b/lib/formatters/clang-format.ts @@ -22,9 +22,10 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. +import {UnprocessedExecResult} from '../../types/execution/execution.interfaces'; import * as exec from '../exec'; -import {BaseFormatter, ExecResult} from './base'; +import {BaseFormatter} from './base'; import {FormatOptions} from './base.interfaces'; export class ClangFormatFormatter extends BaseFormatter { @@ -32,7 +33,7 @@ export class ClangFormatFormatter extends BaseFormatter { return 'clangformat'; } - override async format(source: string, options: FormatOptions): Promise { + override async format(source: string, options: FormatOptions): Promise { const tabText = options.useSpaces ? 'Never' : 'AlignWithSpaces'; const arg = `{BasedOnStyle: ${options.baseStyle}, IndentWidth: ${options.tabWidth}, UseTab: ${tabText}}`; return await exec.execute(this.formatterInfo.exe, [`--style=${arg}`], {input: source}); diff --git a/lib/formatters/dartformat.ts b/lib/formatters/dartformat.ts index 9f9a3a91b..09899afca 100644 --- a/lib/formatters/dartformat.ts +++ b/lib/formatters/dartformat.ts @@ -22,9 +22,10 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. +import {UnprocessedExecResult} from '../../types/execution/execution.interfaces'; import * as exec from '../exec'; -import {BaseFormatter, ExecResult} from './base'; +import {BaseFormatter} from './base'; import {FormatOptions} from './base.interfaces'; export class DartFormatFormatter extends BaseFormatter { @@ -32,7 +33,7 @@ export class DartFormatFormatter extends BaseFormatter { return 'dartformat'; } - override async format(source: string, options: FormatOptions): Promise { + override async format(source: string, options: FormatOptions): Promise { return await exec.execute(this.formatterInfo.exe, ['format'], {input: source}); } diff --git a/lib/formatters/gofmt.ts b/lib/formatters/gofmt.ts index fe669206a..2b90ee7a1 100644 --- a/lib/formatters/gofmt.ts +++ b/lib/formatters/gofmt.ts @@ -22,9 +22,10 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. +import {UnprocessedExecResult} from '../../types/execution/execution.interfaces'; import * as exec from '../exec'; -import {BaseFormatter, ExecResult} from './base'; +import {BaseFormatter} from './base'; import {FormatOptions} from './base.interfaces'; export class GoFmtFormatter extends BaseFormatter { @@ -38,7 +39,7 @@ export class GoFmtFormatter extends BaseFormatter { * This function does not use any options, because gofmt does not have any * options. */ - override async format(source: string, options: FormatOptions): Promise { + override async format(source: string, options: FormatOptions): Promise { return await exec.execute(this.formatterInfo.exe, [], {input: source}); } diff --git a/lib/formatters/rustfmt.ts b/lib/formatters/rustfmt.ts index a85002fce..632841a5f 100644 --- a/lib/formatters/rustfmt.ts +++ b/lib/formatters/rustfmt.ts @@ -22,9 +22,10 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. +import {UnprocessedExecResult} from '../../types/execution/execution.interfaces'; import * as exec from '../exec'; -import {BaseFormatter, ExecResult} from './base'; +import {BaseFormatter} from './base'; import {FormatOptions} from './base.interfaces'; export class RustFmtFormatter extends BaseFormatter { @@ -32,7 +33,7 @@ export class RustFmtFormatter extends BaseFormatter { return 'rustfmt'; } - override async format(source: string, options: FormatOptions): Promise { + override async format(source: string, options: FormatOptions): Promise { const args = [ '--emit', 'stdout',