Skip to content

Commit

Permalink
Split into two packages, improve Babel plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
verkholantsev committed Mar 22, 2020
1 parent 7cdfe1c commit 9cbb437
Show file tree
Hide file tree
Showing 38 changed files with 8,525 additions and 179 deletions.
10 changes: 1 addition & 9 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,14 @@ jobs:
- name: Lint files
run: yarn lint

- name: Check Flow
run: yarn flow

- name: Run tests with coverage
run: yarn run coverage

- name: Run build
run: yarn run build

- name: Coveralls
uses: coverallsapp/github-action@master
with:
path-to-lcov: ./packages/superoverload/coverage/lcov.info
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: Check in Bundlephobia
uses: carlesnunez/[email protected]

env:
CI: true
72 changes: 16 additions & 56 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,60 +1,20 @@
{
"name": "superoverload",
"version": "1.4.0",
"description": "Function overload for JavaScript",
"main": "build/index.js",
"scripts": {
"test": "yarn run build && jest",
"benchmark": "yarn run build && node ./benchmark",
"build": "rollup -c && prettier build/index.js --write",
"format": "prettier --write **/*.{md,json,js} && eslint --fix .",
"lint": "eslint ./**/*.js",
"flow": "flow .",
"pre-commit": "yarn run lint && yarn run flow",
"coverage": "jest --coverage",
"release": "np"
},
"keywords": [
"function",
"overload",
"javascript"
],
"author": "Aleksey Verkholantsev <[email protected]> (https://github.com/verkholantsev)",
"contributors": [
"escaton (https://github.com/escaton)"
],
"name": "superoverload-repo",
"version": "1.0.0",
"description": "Superoverload Repo",
"main": "index.js",
"repository": "[email protected]:verkholantsev/superoverload.git",
"author": "Aleksei Verkholantcev <[email protected]>",
"license": "MIT",
"repository": {
"type": "git",
"url": "git://github.com/verkholantsev/superoverload.git"
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/plugin-external-helpers": "^7.8.3",
"@babel/preset-env": "^7.9.0",
"@babel/preset-flow": "^7.9.0",
"@babel/register": "^7.9.0",
"babel-eslint": "^10.1.0",
"benchmark": "^2.1.4",
"captain-git-hook": "^2.0.0",
"coveralls": "^3.0.11",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-flowtype": "^4.6.0",
"eslint-plugin-prettier": "^3.1.2",
"flow-bin": "^0.121.0",
"jest": "^25.1.0",
"lodash": "^4.17.15",
"np": "^6.2.0",
"prettier": "^1.19.1",
"rollup": "^2.1.0",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^5.3.0"
"private": true,
"scripts": {
"coverage": "yarn workspaces run coverage",
"format": "prettier --write \"packages/**/*.{js,json,md}\"",
"lint": "yarn workspaces run lint",
"pre-commit": "yarn run lint",
"test": "yarn run lint && yarn workspaces run test"
},
"dependencies": {},
"engines": {
"node": ">=10"
}
"workspaces": [
"packages/*"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fixtures/
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
compress(`
var a = 1;
var b = ${x};
console.log(a + b);
`);

compress(`
if (
true
)
{
console.log('true');
}
else
{
console.log('nope');
}
`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`var a=1;var b=${x};console.log(a+b);`;
`if(true){console.log('true');}else{console.log('nope');}`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["compress-template-literals"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function compress() {}
78 changes: 78 additions & 0 deletions packages/babel-plugin-compress-template-literals/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
function isWhitespace(char) {
return /\s/.test(char);
}

function isNonWhitespace(char) {
return /[^\s]/.test(char);
}

function isStringBoundary(char) {
return char === "'" || char === '"' || char === '`';
}

function compressString(input) {
const output = [];
let i = 0;
let isBetweenWords = false;
let isInString = false;
while (i < input.length) {
const char = input[i];
const previousChar = output[output.length - 1];

if (isStringBoundary(char) && previousChar !== '\\') {
isInString = !isInString;
output.push(char);
} else if (isInString) {
output.push(char);
} else if (isNonWhitespace(char)) {
if (
isBetweenWords &&
output.length > 0 &&
/\w/.test(previousChar) &&
/\w/.test(char)
) {
output.push(' ');
}
output.push(char);
isBetweenWords = false;
} else if (isWhitespace(char)) {
isBetweenWords = true;
}

i++;
}
return output.join('');
}

module.exports = function (babel) {
const {types: t} = babel;

return {
name: 'ast-transform', // not required
visitor: {
FunctionDeclaration(path) {
if (path.node.id.name === 'compress') {
path.remove();
}
},
CallExpression(path) {
if (path.node.callee.name === 'compress') {
path.node.arguments[0].quasis.forEach(templateElement => {
templateElement.value.raw = compressString(
templateElement.value.raw,
);
templateElement.value.cooked = compressString(
templateElement.value.cooked,
);
});
path.replaceWith(
t.templateLiteral(
path.node.arguments[0].quasis,
path.node.arguments[0].expressions,
),
);
}
},
},
};
};
22 changes: 22 additions & 0 deletions packages/babel-plugin-compress-template-literals/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "babel-plugin-compress-template-literals",
"version": "1.0.0",
"description": "Babel plugin to compress template literals which contain JS code",
"main": "index.js",
"author": "Aleksei Verkholantcev <[email protected]>",
"license": "MIT",
"private": true,
"dependencies": {},
"devDependencies": {
"@babel/helper-plugin-test-runner": "^7.8.3",
"eslint": "^6.8.0",
"eslint-plugin-prettier": "^3.1.2",
"jest": "^25.1.0",
"prettier": "^2.0.1"
},
"scripts": {
"test": "jest test.js",
"lint": "eslint .",
"coverage": "echo 'Not implemented'"
}
}
5 changes: 5 additions & 0 deletions packages/babel-plugin-compress-template-literals/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

const runner = require('@babel/helper-plugin-test-runner').default;

runner(__dirname);
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 6 additions & 6 deletions build/index.js → packages/superoverload/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@
longestSignature = signature;
}

ifsArray[i] = "if (hashKey === '"
.concat(hashKey, "') {return fns[")
.concat(String(i), '].call(this, ')
ifsArray[i] = "if(hashKey==='"
.concat(hashKey, "') {\n return fns[")
.concat(String(i), '].call(this,')
.concat(serializeSignature(signature), ');}');
}

Expand All @@ -167,16 +167,16 @@
var code = 'return function overloadedFn('
.concat(
serializedSignature,
") {var hashKey = '';var len = arguments.length;var args = new Array(len);for (var i = 0; i < len; i++) {args[i] = arguments[i];}for (var i = 0; i < len; i++) {hashKey += getType(args[i]);if (i !== len - 1) {hashKey += ', ';}}",
"){var hashKey='';var len=arguments.length;var args=new Array(len);for(var i=0;i<len;i++){args[i]=arguments[i];}for(var i=0;i<len;i++){hashKey+=getType(args[i]);if(i!==len-1){hashKey+=', ';}}",
)
.concat(ifs)
.concat(
pairs.length > 0 ? 'else {' : '',
"if (!defaultFn) {throw new Error('No matching function for call with signature \"' + hashKey + '\"');}",
"if(!defaultFn){throw new Error('No matching function for call with signature \"' + hashKey + '\"');}",
)
.concat(
pairs.length > 0 ? '}' : '',
'return defaultFn.apply(this, args);}',
'return defaultFn.apply(this,args);}',
);
var superFunc = new Function('getType, fns, defaultFn', code); // $FlowFixMe errors with `new Function(...)`

Expand Down

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

61 changes: 61 additions & 0 deletions packages/superoverload/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "superoverload",
"version": "1.4.0",
"description": "Function overload for JavaScript",
"main": "build/index.js",
"scripts": {
"test": "yarn run build && jest",
"benchmark": "yarn run build && node ./benchmark",
"build": "rollup -c && prettier build/index.js --write",
"format": "prettier --write **/*.{md,json,js} && eslint --fix .",
"lint": "eslint ./**/*.js && flow .",
"flow": "flow .",
"pre-commit": "yarn run lint && yarn run flow",
"coverage": "jest --coverage",
"release": "np"
},
"keywords": [
"function",
"overload",
"javascript"
],
"author": "Aleksey Verkholantsev <[email protected]> (https://github.com/verkholantsev)",
"contributors": [
"escaton (https://github.com/escaton)"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "git://github.com/verkholantsev/superoverload.git"
},
"devDependencies": {
"babel-plugin-compress-template-literals": "^1.0.0",
"@babel/core": "^7.9.0",
"@babel/plugin-external-helpers": "^7.8.3",
"@babel/preset-env": "^7.9.0",
"@babel/preset-flow": "^7.9.0",
"@babel/register": "^7.9.0",
"babel-eslint": "^10.1.0",
"benchmark": "^2.1.4",
"captain-git-hook": "^2.0.0",
"coveralls": "^3.0.11",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-flowtype": "^4.6.0",
"eslint-plugin-prettier": "^3.1.2",
"flow-bin": "^0.121.0",
"jest": "^25.1.0",
"lodash": "^4.17.15",
"np": "^6.2.0",
"prettier": "^1.19.1",
"rollup": "^2.1.0",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^5.3.0"
},
"dependencies": {},
"engines": {
"node": ">=10"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
plugins: [
babel({
presets: [['@babel/env', {modules: false}], '@babel/flow'],
plugins: ['./src/babel-plugin-compress-template-literals'],
plugins: ['babel-plugin-compress-template-literals'],
exclude: 'node_modules/**',
babelrc: false,
}),
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 9cbb437

Please sign in to comment.