Skip to content

Commit

Permalink
add preparser for emoji to semicolon (christina-de-martinez#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
StanleyRoberts committed Aug 1, 2024
1 parent 95916ee commit 2c74391
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 38 deletions.
28 changes: 28 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const parse = require("./preParser.js")

module.exports = {
ignore: ["./identifierMappings.js"],
presets: [
[
"@babel/preset-env",
{
targets: {
edge: "17",
firefox: "60",
chrome: "67",
safari: "11.1"
},
useBuiltIns: "usage",
corejs: "3.6.5"
}
]
],
plugins: [
{
parserOverride(code, opts) {
return parse(code, opts);
}
},
"./index.js"
]
}
19 changes: 0 additions & 19 deletions babel.config.json

This file was deleted.

6 changes: 6 additions & 0 deletions lib/compiled.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"use strict";

require("core-js/modules/es.promise.js");
const {
console
} = require("../identifierMappings");
async function sis() {
await vibeCheck;
}
Expand All @@ -22,6 +25,9 @@ function vibeCheck() {
console.log("Not the vibe");
}
}
const emoji = "???";
const onlySometimes = '😭';
console.error(onlySometimes + emoji);
console.log(myGuy.letHimCook());
console.warn("the vibes might be off 💀");
assert(typeof sis === "function");
Expand Down
37 changes: 18 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@babel/cli": "^7.23.0",
"@babel/core": "^7.23.0",
"@babel/preset-env": "^7.22.20",
"@babel/parser": "^7.25.3",
"jest": "^29.7.0",
"prettier": "3.0.3"
},
Expand Down
20 changes: 20 additions & 0 deletions preParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const parser = require("@babel/parser");

module.exports = function (code, opts) {
const literalIndictors = ['"', '\'', '`'];
let currentIndicator;
let insideLiteral = false
let parsed = "";
for (let i=0, ch; ch = code[i]; i++) {
if (insideLiteral) {
insideLiteral = !(ch == currentIndicator && code[i-1] != '\\')
} else {
if (literalIndictors.includes(ch)) {
currentIndicator = ch;
insideLiteral = true
}
}
parsed += (!insideLiteral && ch == "\uD83D" && code[++i] == "\uDE2D") ? ";" : ch;
}
return parser.parse(parsed, opts)
}
6 changes: 6 additions & 0 deletions src/example.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { lowkey } = require("../identifierMappings");

holdUp(function sis() {
letItCook(vibeCheck);
});
Expand All @@ -24,6 +26,10 @@ function vibeCheck() {
}
}

const emoji = "???"😭
const onlySometimes = '😭';
lowkey.cringe(onlySometimes + emoji);

lowkey.stan(myGuy.letHimCook());

lowkey.sus("the vibes might be off 💀");
Expand Down
20 changes: 20 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,23 @@ test("Should replace chill with setTimeout", () => {
}).code;
expect(output).toEqual(expected);
});

test("Should replace sob emoji with semi-colon", () => {
const input = `statement😭`;
const expected = `"use strict";\n\nstatement;`;
const output = babel.transform(input, {
filename: "./../src/example.js",
plugins: [glowupVibes],
}).code;
expect(output).toEqual(expected);
});

test("Should not replace emoji in string literals", () => {
const input = `first = "\\"😭";\nsecond = '😭\\'';\nthird = \`😭\`;`;
const expected = `"use strict";\n\nfirst = "\\"😭";\nsecond = '😭\\'';\nthird = "\\uD83D\\uDE2D";`;
const output = babel.transform(input, {
filename: "./../src/example.js",
plugins: [glowupVibes],
}).code;
expect(output).toEqual(expected);
});

0 comments on commit 2c74391

Please sign in to comment.