From d6d81e7496a129144b921565b36cdc372a096951 Mon Sep 17 00:00:00 2001 From: Jan Klaas Kollhof Date: Mon, 2 Mar 2020 12:08:41 +0100 Subject: [PATCH 1/3] feat(string): add support for tagged templ strings --- src/transform/__snapshots__/string.test.js.snap | 4 +++- src/transform/string.js | 17 ++++++++++++----- src/transform/string.test.js | 2 ++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/transform/__snapshots__/string.test.js.snap b/src/transform/__snapshots__/string.test.js.snap index 0566309..c155ecc 100644 --- a/src/transform/__snapshots__/string.test.js.snap +++ b/src/transform/__snapshots__/string.test.js.snap @@ -5,8 +5,10 @@ exports[`compiles regex 1`] = ` line 2 with leading space line 3\`; const str2 = \`ab\`; +const str3 = foo\`bar spam \${shrub}\`; Object.assign(module.exports, { str1, - str2 + str2, + str3 });" `; diff --git a/src/transform/string.js b/src/transform/string.js index ee3d6e8..7d8c4f3 100644 --- a/src/transform/string.js +++ b/src/transform/string.js @@ -1,10 +1,17 @@ -import {templateElement, templateLiteral} from '@babel/types'; +import {templateElement, templateLiteral, taggedTemplateExpression} from '@babel/types'; -export const transform_string = (node)=> ( - templateLiteral( +export const transform_string = (node, {transform})=> { + const templ_str = templateLiteral( node.parts.map((part)=> templateElement({raw: part, cooked: part})), [] - ) -); + ); + + if (node.tag) { + + return taggedTemplateExpression(transform(node.tag), templ_str); + } + + return templ_str; +}; diff --git a/src/transform/string.test.js b/src/transform/string.test.js index d592840..dceca0e 100644 --- a/src/transform/string.test.js +++ b/src/transform/string.test.js @@ -10,6 +10,8 @@ test('compiles regex', ()=> { line 3\` str2 = \`ab\` + + str3 = foo\`bar spam \${shrub}\` `) ).toMatchSnapshot(); }); From bfa5e9a29672cc58e4e4aeee503349707fd9baae Mon Sep 17 00:00:00 2001 From: Jan Klaas Kollhof Date: Mon, 2 Mar 2020 12:09:07 +0100 Subject: [PATCH 2/3] feat: add `throw` and `new` operators --- package-lock.json | 6 +++--- package.json | 2 +- src/index.js | 4 ++++ src/transform/__snapshots__/new.test.js.snap | 8 ++++++++ src/transform/__snapshots__/throw.test.js.snap | 8 ++++++++ src/transform/new.js | 10 ++++++++++ src/transform/new.test.js | 12 ++++++++++++ src/transform/throw.js | 10 ++++++++++ src/transform/throw.test.js | 12 ++++++++++++ 9 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 src/transform/__snapshots__/new.test.js.snap create mode 100644 src/transform/__snapshots__/throw.test.js.snap create mode 100644 src/transform/new.js create mode 100644 src/transform/new.test.js create mode 100644 src/transform/throw.js create mode 100644 src/transform/throw.test.js diff --git a/package-lock.json b/package-lock.json index 313b761..959d85c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -999,9 +999,9 @@ } }, "@fink/larix": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/@fink/larix/-/larix-4.1.0.tgz", - "integrity": "sha512-h1dDbPmvcJtS7ehdWG4GWndJSIbD4Gs6wzDNyj3yhU4emBwIb2z7tMHSP3wYg5OJWTkMHIDD4ZvHgBliwpErsw==", + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/@fink/larix/-/larix-4.2.0.tgz", + "integrity": "sha512-PWfLR8pGnd7PMEhMLeUhbnBHulsUuh5gqRhyfSI7Ux57jtK6pb2XhbRugeXGx/rZ7YF6nDSaAJk8gu5va6QoyA==", "dev": true, "requires": { "@fink/prattler": "^1.0.0" diff --git a/package.json b/package.json index 0ec27a0..cb20442 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "@babel/cli": "^7.2.3", "@babel/plugin-proposal-pipeline-operator": "^7.3.2", "@babel/preset-env": "^7.8.6", - "@fink/larix": "^4.1.0", + "@fink/larix": "^4.2.0", "@nearmap/eslint-config-base": "^1.1.0", "babel-eslint": "^10.1.0", "commitizen": "^4.0.3", diff --git a/src/index.js b/src/index.js index 48c8a09..c134900 100644 --- a/src/index.js +++ b/src/index.js @@ -41,6 +41,8 @@ import { import transform_do_expr from './transform/js/do-expression'; import {transform_async} from './transform/js/async'; +import {transform_new} from './transform/new'; +import {transform_throw} from './transform/throw'; const jsx = { @@ -71,6 +73,8 @@ const unary_ops = { await: transform_await, '...': transform_spread, '!': transform_unary, + new: transform_new, + throw: transform_throw, import: transform_import }; diff --git a/src/transform/__snapshots__/new.test.js.snap b/src/transform/__snapshots__/new.test.js.snap new file mode 100644 index 0000000..c152b55 --- /dev/null +++ b/src/transform/__snapshots__/new.test.js.snap @@ -0,0 +1,8 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`new transforms new 1`] = ` +"const foo = new Set(); +Object.assign(module.exports, { + foo +});" +`; diff --git a/src/transform/__snapshots__/throw.test.js.snap b/src/transform/__snapshots__/throw.test.js.snap new file mode 100644 index 0000000..86febdc --- /dev/null +++ b/src/transform/__snapshots__/throw.test.js.snap @@ -0,0 +1,8 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`throw transforms throw 1`] = ` +"const foo = bar || throw err(\`spam\`); +Object.assign(module.exports, { + foo +});" +`; diff --git a/src/transform/new.js b/src/transform/new.js new file mode 100644 index 0000000..c6f51ee --- /dev/null +++ b/src/transform/new.js @@ -0,0 +1,10 @@ +import {newExpression} from '@babel/types'; +import {wrap} from '../types'; + + +export const transform_new = (node, ctx)=> { + const right = ctx.transform(node.right); + + return wrap(node, newExpression(right.callee, right.arguments)); +}; + diff --git a/src/transform/new.test.js b/src/transform/new.test.js new file mode 100644 index 0000000..8644868 --- /dev/null +++ b/src/transform/new.test.js @@ -0,0 +1,12 @@ +import {fink2js} from '../testing'; + + +describe('new', ()=> { + it('transforms new', ()=> { + expect( + fink2js(` + foo = new Set() + `) + ).toMatchSnapshot(); + }); +}); diff --git a/src/transform/throw.js b/src/transform/throw.js new file mode 100644 index 0000000..8d9f4fd --- /dev/null +++ b/src/transform/throw.js @@ -0,0 +1,10 @@ +import {unaryExpression} from '@babel/types'; +import {wrap} from '../types'; + + +export const transform_throw = (node, ctx)=> { + const right = ctx.transform(node.right); + + return wrap(node, unaryExpression('throw', right)); +}; + diff --git a/src/transform/throw.test.js b/src/transform/throw.test.js new file mode 100644 index 0000000..2520eb3 --- /dev/null +++ b/src/transform/throw.test.js @@ -0,0 +1,12 @@ +import {fink2js} from '../testing'; + + +describe('throw', ()=> { + it('transforms throw', ()=> { + expect( + fink2js(` + foo = bar || throw err('spam') + `) + ).toMatchSnapshot(); + }); +}); From 2101be4840de2b6aeafe33448376871fbbc6127f Mon Sep 17 00:00:00 2001 From: Jan Klaas Kollhof Date: Mon, 2 Mar 2020 12:09:54 +0100 Subject: [PATCH 3/3] style(lint): fix lint issues --- src/transform/string.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/transform/string.js b/src/transform/string.js index 7d8c4f3..adf1d20 100644 --- a/src/transform/string.js +++ b/src/transform/string.js @@ -1,4 +1,6 @@ -import {templateElement, templateLiteral, taggedTemplateExpression} from '@babel/types'; +import { + templateElement, templateLiteral, taggedTemplateExpression +} from '@babel/types'; export const transform_string = (node, {transform})=> { @@ -8,7 +10,6 @@ export const transform_string = (node, {transform})=> { ); if (node.tag) { - return taggedTemplateExpression(transform(node.tag), templ_str); }