Skip to content

Commit

Permalink
Merge pull request #20 from fink-lang/fixes
Browse files Browse the repository at this point in the history
fix(literals): fix escape handling in strings and add regex tests
  • Loading branch information
kollhof authored Mar 27, 2020
2 parents e6a0088 + e4fa5e4 commit fa3577a
Show file tree
Hide file tree
Showing 9 changed files with 518 additions and 504 deletions.
826 changes: 377 additions & 449 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
"@babel/cli": "^7.2.3",
"@babel/plugin-proposal-pipeline-operator": "^7.3.2",
"@babel/preset-env": "^7.8.6",
"@fink/larix": "^4.4.0",
"@fink/larix": "^4.5.2",
"@nearmap/eslint-config-base": "^1.1.0",
"babel-eslint": "^10.1.0",
"commitizen": "^4.0.3",
"cz-conventional-changelog": "^3.1.0",
"eslint": "^6.8.0",
"eslint-cli": "^1.1.1",
"eslint-plugin-babel": "^5.3.0",
"jest-cli": "^25.1.0",
"jest-cli": "^25.2.3",
"npx-run": "^2.1.2",
"semantic-release": "^17.0.4"
},
Expand Down
2 changes: 1 addition & 1 deletion src/lang/iterable/find.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {fink2js} from '../../testing';


describe('compiles fold', ()=> {
describe('compiles find', ()=> {
it('compiles simple find', ()=> {
expect(
fink2js(`
Expand Down
4 changes: 2 additions & 2 deletions src/lang/iterable/find.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`compiles fold compiles complex find 1`] = `
exports[`compiles find compiles complex find 1`] = `
"ˆitems_2 => {
for (const ˆitem_1 of ˆitems_2) {
const {
Expand Down Expand Up @@ -40,7 +40,7 @@ exports[`compiles fold compiles complex find 1`] = `
Object.assign(module.exports, {});"
`;

exports[`compiles fold compiles simple find 1`] = `
exports[`compiles find compiles simple find 1`] = `
"ˆitems_2 => {
for (const ˆitem_1 of ˆitems_2) {
const item = ˆitem_1;
Expand Down
40 changes: 28 additions & 12 deletions src/lang/literals/regex.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import {fink2js} from '../../testing';


test('compiles regex', ()=> {
expect(
fink2js(`
regex1 = rx/
(?<year>\\d{4})- # year part of a date
(?<month>\\d{2})- # month part of a date
(?<day>\\d{2}) # day part of a date
/gm
describe('regex', ()=> {
it('compiles single line', ()=> {
expect(
fink2js(`
regex = rx/(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})/
`)
).toMatchSnapshot();
});

regex2 = rx/(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})/

regex3 = rx/.+\\/.+/
`)
).toMatchSnapshot();
it('compiles multiline', ()=> {
expect(
fink2js(`
regex = rx/
(?<year>\\d{4})- # year part of a date
(?<month>\\d{2})- # month part of a date
(?<day>\\d{2}) # day part of a date
/gm
`)
).toMatchSnapshot();
});


it('compiles escape char', ()=> {
expect(
fink2js(`
regex = rx/.+\\/.+\\\\/
`)
).toMatchSnapshot();
});
});
24 changes: 17 additions & 7 deletions src/lang/literals/regex.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`compiles regex 1`] = `
"const regex1 = /(?<year>\\\\d{4})-(?<month>\\\\d{2})-(?<day>\\\\d{2})/gm;
const regex2 = /(?<year>\\\\d{4})-(?<month>\\\\d{2})-(?<day>\\\\d{2})/;
const regex3 = /.+\\\\/.+/;
exports[`regex compiles escape char 1`] = `
"const regex = /.+\\\\/.+\\\\\\\\/;
Object.assign(module.exports, {
regex1,
regex2,
regex3
regex
});"
`;

exports[`regex compiles multiline 1`] = `
"const regex = /(?<year>\\\\d{4})-(?<month>\\\\d{2})-(?<day>\\\\d{2})/gm;
Object.assign(module.exports, {
regex
});"
`;
exports[`regex compiles single line 1`] = `
"const regex = /(?<year>\\\\d{4})-(?<month>\\\\d{2})-(?<day>\\\\d{2})/;
Object.assign(module.exports, {
regex
});"
`;
3 changes: 1 addition & 2 deletions src/lang/literals/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ export const transform_string = (node, {transform})=> {
const quasies = node.parts
.filter((part, idx)=> idx % 2 === 0)
.map((part)=> templateElement({
raw: part.value,
cooked: part.value
raw: part.value.replace(/\\([\s\S])|(`)/g, '\\$1$2')
}));

const expressions = node.parts
Expand Down
73 changes: 54 additions & 19 deletions src/lang/literals/string.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,58 @@
import {fink2js} from '../../testing';


test('compiles regex', ()=> {
expect(
fink2js(`
str1 = \`
line 1
line 2 with leading space
line 3\`
str2 = \`ab\`
str3 = foo\`bar spam \${shrub + ni} na\`
str4 = \`
bar
spam \${shrub + ni}\${foo}
ni
\`
`)
).toMatchSnapshot();
describe('string', ()=> {

it('compiles simple', ()=> {
expect(
fink2js(`
str = 'ab'
`)
).toMatchSnapshot();
});


it('compiles multiline', ()=> {
expect(
fink2js(`
str = '
line 1
line 2 with leading space
line 3'
`)
).toMatchSnapshot();
});


it('compiles escape chars', ()=> {
expect(
fink2js(`
${"str1 = 'foo`bar\\nspam`ni'"}
${'str2 = `foo\\`bar`'}
${'str3 = "foo\\\\"'}
`)
).toMatchSnapshot();
});


it('compiles tagged template string', ()=> {
expect(
fink2js(`
str = foo'bar \${spam ni shrub} na'
`)
).toMatchSnapshot();
});


it('compiles multiline with expressions', ()=> {
expect(
fink2js(`
str = '
bar
spam \${shrub + ni}\${foo}
ni
'
`)
).toMatchSnapshot();
});
});
46 changes: 36 additions & 10 deletions src/lang/literals/string.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`compiles regex 1`] = `
"const str1 = \`line 1
exports[`string compiles escape chars 1`] = `
"const str1 = \`foo\\\\\`bar\\\\nspam\\\\\`ni\`;
const str2 = \`foo\\\\\`bar\`;
const str3 = \`foo\\\\\\\\\`;
Object.assign(module.exports, {
str1,
str2,
str3
});"
`;

exports[`string compiles multiline 1`] = `
"const str = \`line 1
line 2 with leading space
line 3\`;
const str2 = \`ab\`;
const str3 = foo\`bar spam \${shrub + ni} na\`;
const str4 = \`bar
spam \${shrub + ni}\${foo}
Object.assign(module.exports, {
str
});"
`;

exports[`string compiles multiline with expressions 1`] = `
"const str = \`bar
spam \${shrub + ni}\${foo}
ni
\`;
Object.assign(module.exports, {
str1,
str2,
str3,
str4
str
});"
`;

exports[`string compiles simple 1`] = `
"const str = \`ab\`;
Object.assign(module.exports, {
str
});"
`;

exports[`string compiles tagged template string 1`] = `
"const str = foo\`bar \${ni(spam, shrub)} na\`;
Object.assign(module.exports, {
str
});"
`;

0 comments on commit fa3577a

Please sign in to comment.