Skip to content

Commit 7921d24

Browse files
committed
refactor: Standardized options.ts
1 parent da8545c commit 7921d24

8 files changed

Lines changed: 28 additions & 25 deletions

File tree

src/options.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
import type { Node } from '@babel/types';
21
import type {
32
BooleanSupportOption,
43
ParserOptions,
54
SupportOptions,
65
} from 'prettier';
76

8-
export interface Options extends ParserOptions<Node | undefined> {
7+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8+
export interface PluginOptions<T = any> extends ParserOptions<T> {
99
templateExportDefault?: boolean;
1010
templateSingleQuote?: boolean;
1111
}
1212

1313
const templateExportDefault: BooleanSupportOption = {
1414
category: 'Format',
15-
type: 'boolean',
1615
default: false,
1716
description:
1817
'Prepend default export template tags with "export default". Since 0.1.0.',
18+
type: 'boolean',
1919
};
2020

2121
/**
2222
* Extracts a valid `templateSingleQuote` option out of the provided options. If
2323
* `templateSingleQuote` is defined, it will be used, otherwise the value for
2424
* `singleQuote` will be inherited.
2525
*/
26-
export function getTemplateSingleQuote(options: Options): boolean {
26+
export function getTemplateSingleQuote(options: PluginOptions): boolean {
2727
const { singleQuote, templateSingleQuote } = options;
2828
return typeof templateSingleQuote === 'boolean'
2929
? templateSingleQuote
@@ -32,9 +32,9 @@ export function getTemplateSingleQuote(options: Options): boolean {
3232

3333
const templateSingleQuote: BooleanSupportOption = {
3434
category: 'Format',
35-
type: 'boolean',
3635
description:
3736
'Use single quotes instead of double quotes within template tags. Since 0.0.3.',
37+
type: 'boolean',
3838
};
3939

4040
export const options: SupportOptions = {

src/parse/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
import type { Parser } from 'prettier';
1010
import { parsers as babelParsers } from 'prettier/plugins/babel.js';
1111

12-
import type { Options } from '../options.js';
12+
import type { PluginOptions } from '../options.js';
1313
import { assert } from '../utils/assert.js';
1414
import { PRINTER_NAME } from '../utils/index.js';
1515
import { preprocess, type Template } from './preprocess.js';
@@ -124,7 +124,7 @@ export const parser: Parser<Node | undefined> = {
124124
...typescript,
125125
astFormat: PRINTER_NAME,
126126

127-
async parse(code: string, options: Options): Promise<Node> {
127+
async parse(code: string, options: PluginOptions): Promise<Node> {
128128
const preprocessed = preprocess(code, options.filepath);
129129

130130
const ast = await typescript.parse(preprocessed.code, options);

src/print/ambiguity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Node } from '@babel/types';
22
import type { AstPath, doc, Printer } from 'prettier';
33
import { printers as estreePrinters } from 'prettier/plugins/estree.js';
44

5-
import type { Options } from '../options.js';
5+
import type { PluginOptions } from '../options.js';
66
import { flattenDoc } from '../utils/doc.js';
77

88
const estreePrinter = estreePrinters['estree'] as Printer<Node | undefined>;
@@ -31,7 +31,7 @@ export function saveCurrentPrintOnSiblingNode(
3131
export function fixPreviousPrint(
3232
previousTemplatePrinted: doc.builders.Doc[],
3333
path: AstPath<Node | undefined>,
34-
options: Options,
34+
options: PluginOptions,
3535
print: (path: AstPath<Node | undefined>) => doc.builders.Doc,
3636
args: unknown,
3737
): void {

src/print/index.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
} from 'prettier';
88
import { printers as estreePrinters } from 'prettier/plugins/estree.js';
99

10-
import type { Options } from '../options.js';
10+
import type { PluginOptions } from '../options.js';
1111
import {
1212
isGlimmerTemplate,
1313
isGlimmerTemplateParent,
@@ -32,13 +32,16 @@ export const printer: Printer<Node | undefined> = {
3232
return estreePrinter.getVisitorKeys?.(node, nonTraversableKeys) || [];
3333
},
3434

35-
printPrettierIgnored(path: AstPath<Node | undefined>, options: Options) {
35+
printPrettierIgnored(
36+
path: AstPath<Node | undefined>,
37+
options: PluginOptions,
38+
) {
3639
return printRawText(path, options);
3740
},
3841

3942
print(
4043
path: AstPath<Node | undefined>,
41-
options: Options,
44+
options: PluginOptions,
4245
print: (path: AstPath<Node | undefined>) => doc.builders.Doc,
4346
args: unknown,
4447
) {
@@ -102,22 +105,22 @@ export const printer: Printer<Node | undefined> = {
102105
return async (textToDoc) => {
103106
if (node && isGlimmerTemplate(node)) {
104107
if (checkPrettierIgnore(path)) {
105-
return printRawText(path, embedOptions as Options);
108+
return printRawText(path, embedOptions as PluginOptions);
106109
}
107110

108111
try {
109112
const content = await printTemplateContent(
110113
node.extra.template.contents,
111114
textToDoc,
112-
embedOptions as Options,
115+
embedOptions as PluginOptions,
113116
);
114117

115118
const printed = printTemplateTag(content);
116119
saveCurrentPrintOnSiblingNode(path, printed);
117120
return printed;
118121
} catch (error) {
119122
console.error(error);
120-
const printed = [printRawText(path, embedOptions as Options)];
123+
const printed = [printRawText(path, embedOptions as PluginOptions)];
121124
saveCurrentPrintOnSiblingNode(path, printed);
122125
return printed;
123126
}
@@ -142,7 +145,7 @@ function trimPrinted(printed: doc.builders.Doc[]): void {
142145

143146
function printRawText(
144147
{ node }: AstPath<Node | undefined>,
145-
options: Options,
148+
options: PluginOptions,
146149
): string {
147150
if (!node) {
148151
return '';

src/print/template.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Options as PrettierOptions } from 'prettier';
22
import { doc } from 'prettier';
33

4-
import type { Options } from '../options.js';
4+
import type { PluginOptions } from '../options.js';
55
import { getTemplateSingleQuote } from '../options.js';
66
import { flattenDoc } from '../utils/doc.js';
77
import { TEMPLATE_TAG_CLOSE, TEMPLATE_TAG_OPEN } from '../utils/index.js';
@@ -25,7 +25,7 @@ export async function printTemplateContent(
2525
// should normalize them into standard Prettier options at this point.
2626
options: PrettierOptions,
2727
) => Promise<doc.builders.Doc>,
28-
options: Options,
28+
options: PluginOptions,
2929
): Promise<doc.builders.Doc> {
3030
return await textToDoc(text.trim(), {
3131
...options,

tests/helpers/ambiguous.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from 'vitest';
22

3-
import type { Options } from '../../src/options.js';
3+
import type { PluginOptions } from '../../src/options.js';
44
import { parse } from '../../src/utils/content-tag.js';
55
import type { TestCase } from '../helpers/cases.js';
66
import { getAllCases } from '../helpers/cases.js';
@@ -89,7 +89,7 @@ export function makeAmbiguousExpressionTest(
8989

9090
async function behavesLikeFormattedAmbiguousCase(
9191
code: string,
92-
formatOptions: Partial<Options> = {},
92+
formatOptions: Partial<PluginOptions> = {},
9393
): Promise<void> {
9494
try {
9595
const result = await format(code, formatOptions);

tests/helpers/format.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { Plugin } from 'prettier';
22
import { format as prettierFormat } from 'prettier';
33

44
import plugin from '../../src/main.js';
5-
import type { Options } from '../../src/options.js';
5+
import type { PluginOptions } from '../../src/options.js';
66

7-
const DEFAULT_OPTIONS: Partial<Options> = {
7+
const DEFAULT_OPTIONS: Partial<PluginOptions> = {
88
parser: 'ember-template-tag',
99
plugins: [plugin as Plugin],
1010
};
@@ -17,7 +17,7 @@ const DEFAULT_OPTIONS: Partial<Options> = {
1717
*/
1818
export async function format(
1919
code: string,
20-
overrides: Partial<Options> = {},
20+
overrides: Partial<PluginOptions> = {},
2121
): Promise<string> {
2222
return await prettierFormat(code, {
2323
...DEFAULT_OPTIONS,

tests/helpers/make-suite.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from 'vitest';
22

3-
import type { Options } from '../../src/options.js';
3+
import type { PluginOptions } from '../../src/options.js';
44
import {
55
AMBIGUOUS_PLACEHOLDER,
66
getAmbiguousCases,
@@ -12,7 +12,7 @@ import { format } from './format.js';
1212

1313
export interface Config {
1414
name: string;
15-
options?: Partial<Options>;
15+
options?: Partial<PluginOptions>;
1616
}
1717

1818
/**

0 commit comments

Comments
 (0)