Skip to content

Commit

Permalink
feature: @putout/printer: TSTypeParameterInstantiation: couple params (
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jul 14, 2023
1 parent a805e86 commit 71bf28d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
17 changes: 11 additions & 6 deletions lib/tokenize/expressions/functions/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
const {parseComments} = require('../../comment/comment');

module.exports.printParams = (path, printer, semantics, customization = {}) => {
const {params = path.get('params')} = customization;

const {
params = path.get('params'),
braceOpen = '(',
braceClose = ')',
} = customization;
const {print, traverse} = printer;

printBraceOpen(path, {
print,
braceOpen,
});

parseComments(path, printer, semantics);
Expand All @@ -28,21 +32,22 @@ module.exports.printParams = (path, printer, semantics, customization = {}) => {

printBraceClose(path, {
print,
braceClose,
});
};

function printBraceOpen(path, {print}) {
function printBraceOpen(path, {print, braceOpen}) {
if (isOneArgArrow(path))
return print.roundBraceOpen();

return print('(');
return print(braceOpen);
}

function printBraceClose(path, {print}) {
function printBraceClose(path, {print, braceClose}) {
if (isOneArgArrow(path))
return print.roundBraceClose();

print(')');
print(braceClose);
}

function isOneArgArrow(path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function test(map: Map<string, unknown>) {
console.log('putout');
map.set('test', 42);
}
28 changes: 12 additions & 16 deletions lib/tokenize/typescript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {TSInterfaceBody} = require('./interface/ts-interface-body');
const {TSIntersectionType} = require('./ts-intersection-type');
const {TSPropertySignature} = require('./ts-property-signature');
const {TSFunctionType} = require('./ts-function-type');
const {printParams} = require('../expressions/functions/params');

module.exports = {
TSAsExpression,
Expand Down Expand Up @@ -77,23 +78,17 @@ module.exports = {
print('...');
print('__typeAnnotation');
},
TSTypeParameterDeclaration(path, {print}) {
print('<');

path
.get('params')
.forEach(print);

print('>');
TSTypeParameterDeclaration(path, printer, semantics) {
printParams(path, printer, semantics, {
braceOpen: '<',
braceClose: '>',
});
},
TSTypeParameterInstantiation(path, {print}) {
print('<');

path
.get('params')
.forEach(print);

print('>');
TSTypeParameterInstantiation(path, printer, semantics) {
printParams(path, printer, semantics, {
braceOpen: '<',
braceClose: '>',
});
},
TSTypeReference(path, {print}) {
print('__typeName');
Expand Down Expand Up @@ -249,3 +244,4 @@ module.exports = {
TSPropertySignature,
TSFunctionType,
};

5 changes: 5 additions & 0 deletions lib/tokenize/typescript/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ test('printer: tokenizer: typescript: ts-type-parameter', (t) => {
t.end();
});

test('printer: tokenizer: typescript: ts-type-parameter-instantiation', (t) => {
t.print(fixture.tsTypeParameterInstantiation);
t.end();
});

test('printer: tokenizer: typescript: ts-conditional-type', (t) => {
t.print(fixture.tsConditionalType);
t.end();
Expand Down

0 comments on commit 71bf28d

Please sign in to comment.