Skip to content

Commit 7ceb31d

Browse files
committed
make language required
1 parent f5d512e commit 7ceb31d

File tree

7 files changed

+38
-46
lines changed

7 files changed

+38
-46
lines changed

src/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
/** @import { BaseNode, Command, Visitors, PrintOptions, Location, Margin, Newline, Indent, Dedent } from './types' */
1+
/** @import { BaseNode, Command, Visitors, PrintOptions } from './types' */
22
import { encode } from '@jridgewell/sourcemap-codec';
33
import { Context, dedent, indent, margin, newline } from './context.js';
4-
import ts from './languages/ts.js';
54

65
/** @type {(str: string) => string} str */
76
let btoa = () => {
@@ -17,19 +16,21 @@ if (typeof window !== 'undefined' && typeof window.btoa === 'function') {
1716
}
1817

1918
/**
20-
* @template {BaseNode} T
19+
* @template {BaseNode} [T=BaseNode]
2120
* @param {{ type: string, [key: string]: any }} node
22-
* @param {PrintOptions<T>} opts
21+
* @param {Visitors<T>} visitors
22+
* @param {PrintOptions} opts
2323
* @returns {{ code: string, map: any }} // TODO
2424
*/
25-
export function print(node, opts = {}) {
25+
export function print(node, visitors, opts = {}) {
2626
if (Array.isArray(node)) {
2727
return print(
2828
{
2929
type: 'Program',
3030
body: node,
3131
sourceType: 'module'
3232
},
33+
visitors,
3334
opts
3435
);
3536
}
@@ -38,7 +39,7 @@ export function print(node, opts = {}) {
3839
const commands = [];
3940

4041
// @ts-expect-error some nonsense I don't understand
41-
const context = new Context(opts.visitors ?? ts({ quotes: opts.quotes }), commands);
42+
const context = new Context(visitors, commands);
4243

4344
context.visit(node);
4445

src/types.d.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TSESTree } from '@typescript-eslint/types';
22
import type { Context } from './context.js';
33

4-
type BaseNode = { type: string };
4+
export type BaseNode = { type: string };
55

66
type NodeOf<T extends string, X> = X extends { type: T } ? X : never;
77

@@ -64,11 +64,9 @@ export interface IndentChange {
6464

6565
export type Command = string | Location | Margin | Newline | Indent | Dedent | Command[];
6666

67-
export interface PrintOptions<T extends BaseNode = BaseNode> {
67+
export interface PrintOptions {
6868
sourceMapSource?: string;
6969
sourceMapContent?: string;
7070
sourceMapEncodeMappings?: boolean; // default true
7171
indent?: string; // default tab
72-
quotes?: 'single' | 'double'; // default single
73-
visitors?: Visitors<T>; // default to ts
7472
}

test/esrap.test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ for (const dir of fs.readdirSync(`${__dirname}/samples`)) {
7272
/** @type {TSESTree.Comment[]} */
7373
let comments;
7474

75-
/** @type {PrintOptions<TSESTree.Node>} */
75+
/** @type {PrintOptions} */
7676
let opts;
7777

7878
if (input_json.length > 0) {
@@ -88,9 +88,7 @@ for (const dir of fs.readdirSync(`${__dirname}/samples`)) {
8888
};
8989
}
9090

91-
opts.visitors = tsx({ comments });
92-
93-
const { code, map } = print(ast, opts);
91+
const { code, map } = print(ast, tsx({ comments }), opts);
9492

9593
fs.writeFileSync(`${__dirname}/samples/${dir}/_actual.${fileExtension}`, code);
9694
fs.writeFileSync(

test/indent.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import { test } from 'vitest';
44
import { load } from './common';
55
import { print } from '../src';
66
import { expect } from 'vitest';
7+
import ts from '../src/languages/ts.js';
78

89
const test_code = "const foo = () => { const bar = 'baz' }";
910

1011
test('default indent type is tab', () => {
1112
const { ast } = load(test_code);
12-
const code = print(ast).code;
13+
const code = print(ast, ts()).code;
1314

1415
expect(code).toMatchInlineSnapshot(`
1516
"const foo = () => {
@@ -20,7 +21,7 @@ test('default indent type is tab', () => {
2021

2122
test('two space indent', () => {
2223
const { ast } = load(test_code);
23-
const code = print(ast, { indent: ' ' }).code;
24+
const code = print(ast, ts(), { indent: ' ' }).code;
2425

2526
expect(code).toMatchInlineSnapshot(`
2627
"const foo = () => {
@@ -31,7 +32,7 @@ test('two space indent', () => {
3132

3233
test('four space indent', () => {
3334
const { ast } = load(test_code);
34-
const code = print(ast, { indent: ' ' }).code;
35+
const code = print(ast, ts(), { indent: ' ' }).code;
3536

3637
expect(code).toMatchInlineSnapshot(`
3738
"const foo = () => {

test/pluggability.test.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,21 @@ import { print } from '../src';
33

44
/** @import { Visitors } from '../types' */
55
test('custom printers work', () => {
6-
/** @type {Visitors} */
7-
const funkyStringPrinter = {
8-
CustomType(node, state) {
9-
if (typeof node.value === 'string') {
10-
state.write(`:) - `);
11-
12-
state.write(node.value);
13-
14-
state.write(` - (:`);
15-
}
16-
}
17-
};
18-
196
const { code } = print(
207
{
218
type: 'CustomType',
229
value: 'testing 123'
2310
},
2411
{
25-
visitors: { ...funkyStringPrinter }
12+
CustomType(node, context) {
13+
if (typeof node.value === 'string') {
14+
context.write(`:) - `);
15+
16+
context.write(node.value);
17+
18+
context.write(` - (:`);
19+
}
20+
}
2621
}
2722
);
2823

test/quotes.test.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { print } from '../src/index.js';
55
import { expect } from 'vitest';
66
import { load } from './common.js';
77
import { walk } from 'zimmerframe';
8+
import ts from '../src/languages/ts.js';
89

910
/** @import { TSESTree } from '@typescript-eslint/types' */
1011

@@ -29,95 +30,95 @@ const test_code = "const foo = 'bar'";
2930
test('default quote type is single', () => {
3031
const { ast } = load(test_code);
3132
clean(ast);
32-
const code = print(ast).code;
33+
const code = print(ast, ts()).code;
3334

3435
expect(code).toMatchInlineSnapshot(`"const foo = 'bar';"`);
3536
});
3637

3738
test('single quotes used when single quote type provided', () => {
3839
const { ast } = load(test_code);
3940
clean(ast);
40-
const code = print(ast, { quotes: 'single' }).code;
41+
const code = print(ast, ts({ quotes: 'single' })).code;
4142

4243
expect(code).toMatchInlineSnapshot(`"const foo = 'bar';"`);
4344
});
4445

4546
test('double quotes used when double quote type provided', () => {
4647
const { ast } = load(test_code);
4748
clean(ast);
48-
const code = print(ast, { quotes: 'double' }).code;
49+
const code = print(ast, ts({ quotes: 'double' })).code;
4950

5051
expect(code).toMatchInlineSnapshot(`"const foo = "bar";"`);
5152
});
5253

5354
test('escape single quotes if present in string literal', () => {
5455
const { ast } = load('const foo = "b\'ar"');
5556
clean(ast);
56-
const code = print(ast, { quotes: 'single' }).code;
57+
const code = print(ast, ts({ quotes: 'single' })).code;
5758

5859
expect(code).toMatchInlineSnapshot(`"const foo = 'b\\'ar';"`);
5960
});
6061

6162
test('escape double quotes if present in string literal', () => {
6263
const { ast } = load("const foo = 'b\"ar'");
6364
clean(ast);
64-
const code = print(ast, { quotes: 'double' }).code;
65+
const code = print(ast, ts({ quotes: 'double' })).code;
6566

6667
expect(code).toMatchInlineSnapshot(`"const foo = "b\\"ar";"`);
6768
});
6869

6970
test('escapes new lines', () => {
7071
const { ast } = load('const str = "a\\nb"');
7172
clean(ast);
72-
const code = print(ast).code;
73+
const code = print(ast, ts()).code;
7374

7475
expect(code).toMatchInlineSnapshot(`"const str = 'a\\nb';"`);
7576
});
7677

7778
test('escapes escape characters', () => {
7879
const { ast } = load('const str = "a\\\\nb"');
7980
clean(ast);
80-
const code = print(ast).code;
81+
const code = print(ast, ts()).code;
8182

8283
expect(code).toMatchInlineSnapshot(`"const str = 'a\\\\nb';"`);
8384
});
8485

8586
test('escapes escape characters#2', () => {
8687
const { ast } = load('const str = "a\\\\\\nb"');
8788
clean(ast);
88-
const code = print(ast).code;
89+
const code = print(ast, ts()).code;
8990

9091
expect(code).toMatchInlineSnapshot(`"const str = 'a\\\\\\nb';"`);
9192
});
9293

9394
test('escapes double escaped backslashes', () => {
9495
const { ast } = load("var text = $.text('\\\\\\\\');");
9596
clean(ast);
96-
const code = print(ast).code;
97+
const code = print(ast, ts()).code;
9798

9899
expect(code).toMatchInlineSnapshot(`"var text = $.text('\\\\\\\\');"`);
99100
});
100101

101102
test('does not escape already-escaped single quotes', () => {
102103
const { ast } = load(`const str = 'a\\'b'`);
103104
clean(ast);
104-
const code = print(ast).code;
105+
const code = print(ast, ts()).code;
105106

106107
expect(code).toMatchInlineSnapshot(`"const str = 'a\\'b';"`);
107108
});
108109

109110
test('does not escape already-escaped double quotes', () => {
110111
const { ast } = load('const str = "a\\"b"');
111112
clean(ast);
112-
const code = print(ast).code;
113+
const code = print(ast, ts()).code;
113114

114115
expect(code).toMatchInlineSnapshot(`"const str = 'a"b';"`);
115116
});
116117

117118
test('correctly handle \\n\\r', () => {
118119
const { ast } = load('const str = "a\\n\\rb"');
119120
clean(ast);
120-
const code = print(ast).code;
121+
const code = print(ast, ts()).code;
121122

122123
expect(code).toMatchInlineSnapshot(`"const str = 'a\\n\\rb';"`);
123124
});

test/sandbox/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,5 @@ const dir = path.resolve(fileURLToPath(import.meta.url), '..');
1010
const input_js = fs.readFileSync(`${dir}/_input.ts`);
1111
const content = input_js.toString();
1212
const { ast, comments } = load(content);
13-
const { code } = print(ast, {
14-
visitors: ts({ comments })
15-
});
13+
const { code } = print(ast, ts({ comments }));
1614
fs.writeFileSync(`${dir}/_output.ts`, code);

0 commit comments

Comments
 (0)