Skip to content

Commit

Permalink
feature: goldstein: convert: if let
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Oct 19, 2023
1 parent e323530 commit 005959d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
18 changes: 18 additions & 0 deletions packages/convert/apply-if-let/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {types} from 'putout';
const {
VariableDeclaration,
VariableDeclarator,
} = types;

export const report = () => `Use 'add-array'`;
export const replace = () => ({
'{let __a = __b; if (__c) __d}': ({__a, __b}, path) => {
const ifPath = path.get('body.1');
ifPath.node.test = VariableDeclaration('let', [
VariableDeclarator(__a, __b),
]);

return ifPath;
},
});

2 changes: 2 additions & 0 deletions packages/convert/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {print} from '../printer/index.js';
import * as removeImportTry from './remove-import-try/index.js';
import * as applyTry from './apply-try/index.js';
import * as addArray from './add-array/index.js';
import * as applyIfLet from './apply-if-let/index.js';
import {
fixEmpty,
parse,
Expand All @@ -15,6 +16,7 @@ export const convert = (source) => {
transform(ast, source, {
plugins: [
['add-array', addArray],
['apply-if-let', applyIfLet],
['apply-try', applyTry],
['remove-import-try', removeImportTry],
],
Expand Down
23 changes: 23 additions & 0 deletions packages/convert/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,26 @@ test('goldstein: convert: add-array', (t) => {
t.equal(result, expected);
t.end();
});

test('goldstein: convert: convert-if-let', (t) => {
const source = montag`
{
let a = b?.c;
if (a) {
log(a);
}
}
`;

const result = convert(source);

const expected = montag`
if let a = b?.c {
log(a);
}\n
`;

t.equal(result, expected);
t.end();
});
3 changes: 2 additions & 1 deletion packages/printer/visitors/if-statement.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {visitors as v} from '@putout/printer';

export const IfStatement = (path, printer, semantics) => {
const {print} = printer;
const {print, indent} = printer;
const {node} = path;

if (!node.goldsteinIf)
return v.IfStatement(path, printer, semantics);

indent();
print('if ');
print('__test');
print(' ');
Expand Down

0 comments on commit 005959d

Please sign in to comment.