Skip to content

Commit 02ba6fa

Browse files
authored
Port lib/formatters to TypeScript (compiler-explorer#3741)
1 parent 411e6aa commit 02ba6fa

File tree

9 files changed

+73
-27
lines changed

9 files changed

+73
-27
lines changed

docs/AddingAFormatter.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
- Add a new formatter under the `formatters` key
66
- The new formatter can have the following keys: name, exe, styles, type, explicitVersion (to override version
77
parsing), version (argument to get version info), versionRe (regex to filter out the right version info)
8-
- Add a `lib/formatters/<formatter>.js` file using the template below, replacing `Type` and `type` as appropriate
8+
- Add a `lib/formatters/<formatter>.ts` file using the template below, replacing `Type` and `type` as appropriate
99

1010
```js
11-
import {BaseFormatter} from '../base-formatter';
11+
import {UnprocessedExecResult} from '../../types/execution/execution.interfaces';
12+
import {BaseFormatter} from './base';
13+
import {FormatOptions} from './base.interfaces';
1214

1315
export class TypeFormatter extends BaseFormatter {
1416
static get key() {
@@ -19,10 +21,11 @@
1921

2022
- The value returned by `key` above corresponds to the `type` property you set in the compiler-explorer properties
2123
configuration file.
22-
- Tweak `format(source, options)` and `isValidStyle(style)` as necessary. See the JSDoc for `format` and the
23-
implementations for other formatters to get a further understanding of how to implement `format(source, options)`.
24+
- Tweak `format(source: string, options: FormatOptions): Promise<UnprocessedExecResult>` and
25+
`isValidStyle(style: string): boolean` as necessary. See the JSDoc for `format` and the implementations for other
26+
formatters to get a further understanding of how to implement `format(source, options)`.
2427

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

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

File renamed without changes.

lib/formatters/base.interfaces.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2022, Compiler Explorer Authors
2+
// All rights reserved.
3+
//
4+
// Redistribution and use in source and binary forms, with or without
5+
// modification, are permitted provided that the following conditions are met:
6+
//
7+
// * Redistributions of source code must retain the above copyright notice,
8+
// this list of conditions and the following disclaimer.
9+
// * Redistributions in binary form must reproduce the above copyright
10+
// notice, this list of conditions and the following disclaimer in the
11+
// documentation and/or other materials provided with the distribution.
12+
//
13+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23+
// POSSIBILITY OF SUCH DAMAGE.
24+
25+
export interface FormatterInfo {
26+
name: string;
27+
exe: string;
28+
styles: string[];
29+
type: string;
30+
explicitVersion?: string;
31+
versionArgument?: string;
32+
versionReExp?: string;
33+
}
34+
35+
export type FormatOptions = {
36+
useSpaces: boolean;
37+
tabWidth: number;
38+
baseStyle: string;
39+
};

lib/formatters/base.js renamed to lib/formatters/base.ts

+11-15
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,33 @@
2222
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2323
// POSSIBILITY OF SUCH DAMAGE.
2424

25+
import {UnprocessedExecResult} from '../../types/execution/execution.interfaces';
2526
import * as exec from '../exec';
2627

27-
export class BaseFormatter {
28-
constructor(formatterInfo) {
28+
import {FormatOptions, FormatterInfo} from './base.interfaces';
29+
30+
export abstract class BaseFormatter {
31+
public formatterInfo: FormatterInfo;
32+
33+
public constructor(formatterInfo: FormatterInfo) {
2934
this.formatterInfo = formatterInfo;
3035
}
3136

3237
/**
3338
* Format the provided source code using the formatting tool.
3439
*
35-
* This function should construct the final arguments passed to the tool
36-
* executable.
37-
*
38-
* Optionally accept a third argument to respect other formatting options.
39-
* The possible options passed are as follows:
40-
*
41-
* interface AdditionalFormatOptions {
42-
* useSpaces: boolean;
43-
* tabWidth: number;
44-
* baseStyle: string;
45-
* }
40+
* This method should construct the command line arguments and call the formatter executable with the constructed
41+
* arguments, returning the execution result
4642
*/
47-
async format(source, options) {
43+
async format(source: string, options: FormatOptions): Promise<UnprocessedExecResult> {
4844
const args = [`--style=${options.baseStyle}`];
4945
return await exec.execute(this.formatterInfo.exe, args, {input: source});
5046
}
5147

5248
/**
5349
* Test if a formatting base style is valid for this formatter
5450
*/
55-
isValidStyle(style) {
51+
isValidStyle(style: string): boolean {
5652
return this.formatterInfo.styles.includes(style);
5753
}
5854
}

lib/formatters/clang-format.js renamed to lib/formatters/clang-format.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@
2222
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2323
// POSSIBILITY OF SUCH DAMAGE.
2424

25+
import {UnprocessedExecResult} from '../../types/execution/execution.interfaces';
2526
import * as exec from '../exec';
2627

2728
import {BaseFormatter} from './base';
29+
import {FormatOptions} from './base.interfaces';
2830

2931
export class ClangFormatFormatter extends BaseFormatter {
3032
static get key() {
3133
return 'clangformat';
3234
}
3335

34-
async format(source, options) {
36+
override async format(source: string, options: FormatOptions): Promise<UnprocessedExecResult> {
3537
const tabText = options.useSpaces ? 'Never' : 'AlignWithSpaces';
3638
const arg = `{BasedOnStyle: ${options.baseStyle}, IndentWidth: ${options.tabWidth}, UseTab: ${tabText}}`;
3739
return await exec.execute(this.formatterInfo.exe, [`--style=${arg}`], {input: source});

lib/formatters/dartformat.js renamed to lib/formatters/dartformat.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,22 @@
2222
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2323
// POSSIBILITY OF SUCH DAMAGE.
2424

25+
import {UnprocessedExecResult} from '../../types/execution/execution.interfaces';
2526
import * as exec from '../exec';
2627

2728
import {BaseFormatter} from './base';
29+
import {FormatOptions} from './base.interfaces';
2830

2931
export class DartFormatFormatter extends BaseFormatter {
3032
static get key() {
3133
return 'dartformat';
3234
}
3335

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

38-
isValidStyle() {
40+
override isValidStyle(style: string): boolean {
3941
// Dart supports only one style
4042
return true;
4143
}

lib/formatters/gofmt.js renamed to lib/formatters/gofmt.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2323
// POSSIBILITY OF SUCH DAMAGE.
2424

25+
import {UnprocessedExecResult} from '../../types/execution/execution.interfaces';
2526
import * as exec from '../exec';
2627

2728
import {BaseFormatter} from './base';
29+
import {FormatOptions} from './base.interfaces';
2830

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

4446
/**
4547
* Gofmt has no styling options
4648
*/
47-
isValidStyle() {
49+
override isValidStyle(style: string): boolean {
4850
return true;
4951
}
5052
}
File renamed without changes.

lib/formatters/rustfmt.js renamed to lib/formatters/rustfmt.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@
2222
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2323
// POSSIBILITY OF SUCH DAMAGE.
2424

25+
import {UnprocessedExecResult} from '../../types/execution/execution.interfaces';
2526
import * as exec from '../exec';
2627

2728
import {BaseFormatter} from './base';
29+
import {FormatOptions} from './base.interfaces';
2830

2931
export class RustFmtFormatter extends BaseFormatter {
3032
static get key() {
3133
return 'rustfmt';
3234
}
3335

34-
async format(source, options) {
36+
override async format(source: string, options: FormatOptions): Promise<UnprocessedExecResult> {
3537
const args = [
3638
'--emit',
3739
'stdout',
@@ -46,7 +48,7 @@ export class RustFmtFormatter extends BaseFormatter {
4648
/**
4749
* Rust format only has one style.
4850
*/
49-
isValidStyle() {
51+
override isValidStyle(style: string): boolean {
5052
return true;
5153
}
5254
}

0 commit comments

Comments
 (0)