Skip to content

Commit

Permalink
Port lib/formatters to TypeScript (#3741)
Browse files Browse the repository at this point in the history
  • Loading branch information
junlarsen committed Jun 5, 2022
1 parent 411e6aa commit 02ba6fa
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 27 deletions.
13 changes: 8 additions & 5 deletions docs/AddingAFormatter.md
Expand Up @@ -5,10 +5,12 @@
- Add a new formatter under the `formatters` key
- The new formatter can have the following keys: name, exe, styles, type, explicitVersion (to override version
parsing), version (argument to get version info), versionRe (regex to filter out the right version info)
- Add a `lib/formatters/<formatter>.js` file using the template below, replacing `Type` and `type` as appropriate
- Add a `lib/formatters/<formatter>.ts` file using the template below, replacing `Type` and `type` as appropriate

```js
import {BaseFormatter} from '../base-formatter';
import {UnprocessedExecResult} from '../../types/execution/execution.interfaces';
import {BaseFormatter} from './base';
import {FormatOptions} from './base.interfaces';

export class TypeFormatter extends BaseFormatter {
static get key() {
Expand All @@ -19,10 +21,11 @@

- The value returned by `key` above corresponds to the `type` property you set in the compiler-explorer properties
configuration file.
- Tweak `format(source, options)` and `isValidStyle(style)` 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)`.
- Tweak `format(source: string, options: FormatOptions): Promise<UnprocessedExecResult>` 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)`.

- Add your `TypeFormatter` to `lib/formatters/_all.js` in alphabetical order
- Add your `TypeFormatter` to `lib/formatters/_all.ts` in alphabetical order

- You can check the output of http://localhost:10240/api/formats to be sure your formatter is there.

Expand Down
File renamed without changes.
39 changes: 39 additions & 0 deletions lib/formatters/base.interfaces.ts
@@ -0,0 +1,39 @@
// Copyright (c) 2022, Compiler Explorer Authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

export interface FormatterInfo {
name: string;
exe: string;
styles: string[];
type: string;
explicitVersion?: string;
versionArgument?: string;
versionReExp?: string;
}

export type FormatOptions = {
useSpaces: boolean;
tabWidth: number;
baseStyle: string;
};
26 changes: 11 additions & 15 deletions lib/formatters/base.js → lib/formatters/base.ts
Expand Up @@ -22,37 +22,33 @@
// 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';

export class BaseFormatter {
constructor(formatterInfo) {
import {FormatOptions, FormatterInfo} from './base.interfaces';

export abstract class BaseFormatter {
public formatterInfo: FormatterInfo;

public constructor(formatterInfo: FormatterInfo) {
this.formatterInfo = formatterInfo;
}

/**
* Format the provided source code using the formatting tool.
*
* This function should construct the final arguments passed to the tool
* executable.
*
* Optionally accept a third argument to respect other formatting options.
* The possible options passed are as follows:
*
* interface AdditionalFormatOptions {
* useSpaces: boolean;
* tabWidth: number;
* baseStyle: string;
* }
* This method should construct the command line arguments and call the formatter executable with the constructed
* arguments, returning the execution result
*/
async format(source, options) {
async format(source: string, options: FormatOptions): Promise<UnprocessedExecResult> {
const args = [`--style=${options.baseStyle}`];
return await exec.execute(this.formatterInfo.exe, args, {input: source});
}

/**
* Test if a formatting base style is valid for this formatter
*/
isValidStyle(style) {
isValidStyle(style: string): boolean {
return this.formatterInfo.styles.includes(style);
}
}
Expand Up @@ -22,16 +22,18 @@
// 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} from './base';
import {FormatOptions} from './base.interfaces';

export class ClangFormatFormatter extends BaseFormatter {
static get key() {
return 'clangformat';
}

async format(source, options) {
override async format(source: string, options: FormatOptions): Promise<UnprocessedExecResult> {
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});
Expand Down
6 changes: 4 additions & 2 deletions lib/formatters/dartformat.js → lib/formatters/dartformat.ts
Expand Up @@ -22,20 +22,22 @@
// 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} from './base';
import {FormatOptions} from './base.interfaces';

export class DartFormatFormatter extends BaseFormatter {
static get key() {
return 'dartformat';
}

async format(source) {
override async format(source: string, options: FormatOptions): Promise<UnprocessedExecResult> {
return await exec.execute(this.formatterInfo.exe, ['format'], {input: source});
}

isValidStyle() {
override isValidStyle(style: string): boolean {
// Dart supports only one style
return true;
}
Expand Down
6 changes: 4 additions & 2 deletions lib/formatters/gofmt.js → lib/formatters/gofmt.ts
Expand Up @@ -22,9 +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 {BaseFormatter} from './base';
import {FormatOptions} from './base.interfaces';

export class GoFmtFormatter extends BaseFormatter {
static get key() {
Expand All @@ -37,14 +39,14 @@ export class GoFmtFormatter extends BaseFormatter {
* This function does not use any options, because gofmt does not have any
* options.
*/
async format(source) {
override async format(source: string, options: FormatOptions): Promise<UnprocessedExecResult> {
return await exec.execute(this.formatterInfo.exe, [], {input: source});
}

/**
* Gofmt has no styling options
*/
isValidStyle() {
override isValidStyle(style: string): boolean {
return true;
}
}
File renamed without changes.
6 changes: 4 additions & 2 deletions lib/formatters/rustfmt.js → lib/formatters/rustfmt.ts
Expand Up @@ -22,16 +22,18 @@
// 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} from './base';
import {FormatOptions} from './base.interfaces';

export class RustFmtFormatter extends BaseFormatter {
static get key() {
return 'rustfmt';
}

async format(source, options) {
override async format(source: string, options: FormatOptions): Promise<UnprocessedExecResult> {
const args = [
'--emit',
'stdout',
Expand All @@ -46,7 +48,7 @@ export class RustFmtFormatter extends BaseFormatter {
/**
* Rust format only has one style.
*/
isValidStyle() {
override isValidStyle(style: string): boolean {
return true;
}
}

0 comments on commit 02ba6fa

Please sign in to comment.