v0.0.11: Added unit-tests, solved the key={...} problem, updated the build/watch configuration of CSX to be able to build minified and non-minified bundle outputs, as well as a CJS version of lib/ (for consuming in Node-environment, like Jest). The previous tests were renamed to examples, and should still need to be updated.

This commit is contained in:
Miel Truyen 2020-04-14 11:54:30 +02:00
parent 05f0e66a42
commit b95e5506d2
121 changed files with 3406 additions and 4929 deletions

2
.idea/misc.xml generated
View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
<option name="languageLevel" value="JSX" />
</component>
</project>

View File

@ -16,6 +16,8 @@
<path value="file://$PROJECT_DIR$/packages/babel-plugin-transform-csx" />
<path value="file://$PROJECT_DIR$/packages/csx/src" />
<path value="file://$PROJECT_DIR$/tests" />
<path value="file://$PROJECT_DIR$/jest" />
<path value="file://$PROJECT_DIR$/examples" />
</resourceRoots>
</entryData>
</entry>

1
README.md Normal file
View File

@ -0,0 +1 @@
TODO the root build scripts are yet to be updated (building/watching and serving examples...)

View File

@ -15,7 +15,7 @@ import postcss from "rollup-plugin-postcss";
import { terser } from 'rollup-plugin-terser';
/** @type {Host} */
const srcDir = host.from("tests");
const srcDir = host.from("examples");
/** @type {Host} */
const distDir = host.from("public");
const assetsGlob = `**/*.@(${[
@ -70,7 +70,7 @@ function logBundle(output, duration){
}
/**
* Build the tests
* Build the examples
* @returns {Promise<void>}
*/
async function build(){

View File

Before

Width:  |  Height:  |  Size: 6.0 KiB

After

Width:  |  Height:  |  Size: 6.0 KiB

13
jest.config.js Normal file
View File

@ -0,0 +1,13 @@
let path = require("path");
module.exports = {
moduleNameMapper: {
"@cerxes/csx": path.resolve(__dirname, 'packages/csx/dist/index.cjs.js'),
},
testEnvironment: "jest-environment-jsdom-sixteen",
setupFiles: [
],
setupFilesAfterEnv: [
],
/*maxConcurrency: 1*/
};

23
jest/.babelrc Normal file
View File

@ -0,0 +1,23 @@
{
"presets": [
["@babel/preset-env", {
"targets": {
"node": "current"
}
}]
],
"plugins": [
[ "@babel/plugin-proposal-decorators", { "legacy": true }],
[ "@babel/plugin-proposal-class-properties", { "loose": true } ],
[ "@babel/plugin-proposal-private-methods", {"loose": true } ],
[ "@babel/plugin-proposal-optional-chaining" ],
[ "@babel/plugin-proposal-nullish-coalescing-operator" ],
[ "@babel/plugin-proposal-export-namespace-from" ],
[ "@babel/plugin-proposal-export-default-from" ],
[ "../packages/babel-plugin-transform-csx/dist", {
//"pragma": "render",
//"pragmaFrag": "render",
"throwIfNamespace": false
}]
]
}

4
jest/README.md Normal file
View File

@ -0,0 +1,4 @@
TODO:
- Test key-properties behaving as it should (This is a known bug)
- Test ref-properties
- Test overriden prop/state (this is a suspected bug)

View File

@ -0,0 +1,111 @@
import { render, CustomElement, Host, defineElement, state, prop } from "@cerxes/csx";
import { testContainer } from "../utils/test-container";
import { nextAnimationFrame } from "../utils/next-animation-frame";
describe("Basic tests", () => {
/**
* Assert that a basic component renders as expected
*/
test("Simple example-component", async () => {
@defineElement('basic-component')
class BasicComponent extends CustomElement{
render(){
return (
<div className="page" style={{width: `20em`}}>
<h1>This page is an example!</h1>
<p>
These are the contents.<br/>
</p>
</div>
)
}
}
let container = testContainer(
render(<BasicComponent/>)
);
document.body.appendChild(container);// Components need to be added to the DOM or their connectecCallback will not be called
expect(
container.innerHTML
).toBe([
`<basic-component>`,
`<div class="page" style="width: 20em;"><h1>This page is an example!</h1><p>These are the contents.<br></p></div>`,
`</basic-component>`,
].join(''));
document.body.removeChild(container);
});
/**
* Assert that a component rerenders when one of it's state variables changes
*/
test("Component state", async () => {
@defineElement('state-test')
class StateComponent extends CustomElement{
@state()
loading = false;
render(){
return (<Host>
{this.loading? "loading" : "not-loading"}
</Host>)
}
}
let component = render(<StateComponent/>);
let container = testContainer(component);
document.body.appendChild(container);// Components need to be added to the DOM or their connectecCallback will not be called
// Initial
expect(container.innerHTML).toBe(`<state-test>not-loading</state-test>`);
// Change state and wait for the next animation frame to passs
component.loading = true;
await nextAnimationFrame();
// Updated by state
expect(container.innerHTML).toBe(`<state-test>loading</state-test>`);
document.body.removeChild(container);
});
/**
* Assert that a component rerenders when one of it's properties changes, and in doing so, that it's properties can be set
* through the CSX render function
*/
test("Component properties", async () => {
@defineElement('prop-test')
class PropComponent extends CustomElement{
@prop()
title = "Default title";
render(){
return (<Host>
<h1>{this.title}</h1>
</Host>)
}
}
let initialVSpec = <PropComponent />;
let component = render(initialVSpec);
let container = testContainer(component);
document.body.appendChild(container);// Components need to be added to the DOM or their connectecCallback will not be called
// Initial
expect(container.innerHTML).toBe(`<prop-test><h1>Default title</h1></prop-test>`);
// Change the prop through CSX and verify that it has been set
let updatedVSpec = <PropComponent title={"Updated title"} />;
render(updatedVSpec, {host: component, old: initialVSpec});
expect(component.title).toBe(`Updated title`);
// Wait for the next animation frame for the changes to be rendered
await nextAnimationFrame();
// Updated by state
expect(container.innerHTML).toBe(`<prop-test><h1>Updated title</h1></prop-test>`);
document.body.removeChild(container);
});
});

9
jest/postcss.config.js Normal file
View File

@ -0,0 +1,9 @@
module.exports = {
plugins: [
//require('autoprefixer'),
require('postcss-import'),
require('postcss-preset-env')({
stage: 1,
})
]
};

View File

@ -0,0 +1,215 @@
import { render } from "@cerxes/csx";
import { testContainer } from "../utils/test-container";
describe("Basic elements", () => {
test("Single element", async () => {
expect(
testContainer(
render(
<textarea />
)
).innerHTML
).toBe(
`<textarea></textarea>`
);
});
test("With text-contents", async () => {
expect(
testContainer(
render(
<div>Simple test</div>
)
).innerHTML
).toBe(
`<div>Simple test</div>`
);
});
// TODO once added: fragments
});
describe("Native Attributes", () => {
test("Button-type", async () => {
expect(
testContainer(
render(
<button type="submit">Save</button>
)
).innerHTML
).toBe(
`<button type="submit">Save</button>`
);
});
test("Image-src", async () => {
expect(
testContainer(
render(
<img src="/some-image.jpg"/>
)
).innerHTML
).toBe(
`<img src="/some-image.jpg">`
);
});
// We're testing for native functionality of class here, not className
test("Class", async () => {
expect(
testContainer(
render(
<div class="some-class"/>
)
).innerHTML
).toBe(
`<div class="some-class"></div>`
);
});
// We're testing for native functionality of style here, which should always directly set style as an attribute
// when passed in as a string
test("Style", async () => {
expect(
testContainer(
render(
<div style="width: 10em"/>
)
).innerHTML
).toBe(
`<div style="width: 10em"></div>`
);
});
});
describe("Basic Events", () => {
test("Click-event", async () => {
let clicked = false;
let clickHandler = (ev)=>{
clicked = true;
// TODO test on properties that should be on a native click event
};
/** @type {HTMLButtonElement} */
let button = render(<button type="submit" onClick={clickHandler}>Save</button>);
document.body.append(button);
button.click();
expect(clicked).toBe(true);
document.body.removeChild(button);
});
test("Focus-event", async () => {
// This test exists just to have a second event to test, ideally we would have used two-part word event
// like focusin, but unfortunatily, it does not seem to be working. (probably JSDOM related?)
let focused = false;
let focusHandler = (ev)=>{
focused = true;
};
/** @type {HTMLButtonElement} */
let input = render(<input type="text" onFocus={focusHandler}/>);
document.body.append(input);
input.focus();
expect(focused).toBe(true);
document.body.removeChild(input);
});
});
describe("Children", () => {
test("3 levels", async () => {
expect(
testContainer(
render(
<div class="container">
<button type="submit">
Save
</button>
</div>
)
).innerHTML
).toBe(
`<div class="container"><button type="submit">Save</button></div>`
);
});
test("Multiple children", async () => {
expect(
testContainer(
render(
<div class="container">
<h1>Title</h1>
<p>
Contents
</p>
<button type="submit">
Save
</button>
</div>
)
).innerHTML
).toBe([
`<div class="container">`,
`<h1>Title</h1>`,
`<p>Contents</p>`,
`<button type="submit">Save</button>`,
`</div>`
].join(''));
});
test("Prerendered element", async () => {
let element = render(<h1>Title</h1>);
expect(
testContainer(
render(
<div class="container">
{element}
</div>
)
).innerHTML
).toBe([
`<div class="container">`,
`<h1>Title</h1>`,
`</div>`
].join(''));
});
test("Prerendered text", async () => {
// TODO: CSX doesn't support fragments yet (<>I am text</>)
let text = document.createTextNode(`I am text!`);
expect(
testContainer(
render(
<div class="container">
{text}
</div>
)
).innerHTML
).toBe([
`<div class="container">`,
`I am text!`,
`</div>`
].join(''));
});
test("Nulls are ignored", async () => {
expect(
testContainer(
render(
<div class="container">
<h1>Title</h1>
{null}
<button>Save</button>
</div>
)
).innerHTML
).toBe([
`<div class="container">`,
`<h1>Title</h1>`,
`<button>Save</button>`,
`</div>`
].join(''));
});
});

View File

@ -0,0 +1,61 @@
import { render } from "@cerxes/csx";
import { testContainer } from "../utils/test-container";
describe("Key-property tests", () => {
test("Keyed list", async () => {
let renderedIndexes = [];
let initIndexes = [0, 1, 2, 3];
let makeSpec = (targetList, indexes)=>(
<ul>
{
indexes.map(index => (
<li id={`li_${index}`} key={index} ref={(el) => targetList[ index ] = el}>
{index}
</li>
))
}
</ul>
);
let initialVSpec = makeSpec(renderedIndexes, initIndexes);
let rendered = render(initialVSpec);
let container = testContainer(rendered);
expect(container.innerHTML).toBe(
[
`<ul>`,
...initIndexes.map(index=>`<li id="${`li_${index}`}">${index}</li>`),
`</ul>`
].join('')
);
expect(renderedIndexes.length).toBe(4);
for(let rendered of renderedIndexes){
expect(rendered).not.toBeUndefined();
}
let reorderedIndexes = [3,2,1,0];
let rerenderedIndexes = renderedIndexes.slice();
let updatedVSpec = makeSpec(rerenderedIndexes, reorderedIndexes);
render(updatedVSpec, {host: rendered, old: initialVSpec});// Is this host right? it seems inconsistent and the source of our bug (as it is probably also misused in custom-elements)
// Updated
expect(container.innerHTML).toBe(
[
`<ul>`,
...reorderedIndexes.map(index=>`<li id="${`li_${index}`}">${index}</li>`),
`</ul>`
].join('')
);
// Validate that items were merely re-arranged and not re-created
expect(rerenderedIndexes.length).toBe(4);
for(let i=0; i<4; ++i){
let initRendered = renderedIndexes[i];
let reorderedRendered = rerenderedIndexes[i];
expect(initRendered === reorderedRendered).toBe(true); // These should've remained the same
}
});
});

View File

@ -0,0 +1,17 @@
import { render, Host } from "@cerxes/csx";
import { testContainer } from "../utils/test-container";
describe("Ref-property tests", () => {
test("Simple ref", async () => {
let targetVar = null;
let refHandler = (el)=>targetVar = el;
let container = testContainer(render(<div id="test-div" ref={refHandler}/>));
expect(container.innerHTML).toBe(
`<div id="test-div"></div>`
);
expect(targetVar).not.toBe(null);
expect(targetVar.id).toBe('test-div')
});
});

View File

@ -0,0 +1,44 @@
import { render, Host } from "@cerxes/csx";
import { testContainer } from "../utils/test-container";
describe("Basic render-options", () => {
test("opts.host", async () => {
let startElement = render(<div />);
let container = testContainer([startElement]);
render(<Host style="width: 10em">Contents</Host>, {host: startElement});
expect(container.innerHTML).toBe(
`<div style="width: 10em">Contents</div>`
);
});
test("opts.vnode", async () => {
let container = testContainer();
let initialVSpec = (
<Host>
<h1>Initial title</h1>
</Host>
);
let updatedVSpec = (
<Host>
<h1>Updated title</h1>
</Host>
);
render(initialVSpec, {host: container});
// Initial
expect(container.innerHTML).toBe(
`<h1>Initial title</h1>`
);
render(updatedVSpec, {host: container, old: initialVSpec});
// Updated
expect(container.innerHTML).toBe(
`<h1>Updated title</h1>`
);
});
});

View File

@ -0,0 +1,7 @@
/**
* @returns {Promise<void>}
*/
export async function nextAnimationFrame(inFrame){
// Await the next animation frame
await new Promise((resolve,reject)=>requestAnimationFrame(()=>resolve()));
}

View File

@ -0,0 +1,13 @@
/**
* @returns {HTMLDivElement}
*/
export function testContainer(elements){
let container = document.createElement('div');
if(elements){
if(!(elements instanceof Array)){
elements = [elements];
}
container.append(...elements);
}
return container;
}

View File

@ -29,18 +29,21 @@
"postcss-preset-env": "latest",
"postcss-import": "latest",
"serve": "latest",
"npm-run-all": "latest"
"npm-run-all": "latest",
"jest-environment-jsdom-sixteen": "latest",
"jest": "latest"
},
"scripts": {
"dev": "npm-run-all -p watch:babel-transform-csx watch:csx watch:tests serve:tests",
"build": "npm-run-all -s build:babel-transform-csx build:csx build:tests",
"watch": "npm-run-all -p watch:babel-transform-csx watch:csx watch:tests",
"build:tests": "node -r @babel/register ./tests/cfg/rollup-build.js build",
"watch:tests": "node -r @babel/register ./tests/cfg/rollup-build.js watch",
"serve:tests": "serve public",
"test": "jest",
"dev": "npm-run-all -p watch:babel-transform-csx watch:csx watch:examples serve:examples",
"build": "npm-run-all -s build:babel-transform-csx build:csx build:examples",
"watch": "npm-run-all -p watch:babel-transform-csx watch:csx watch:examples",
"build:examples": "node -r @babel/register examples/cfg/rollup-build.js build",
"watch:examples": "node -r @babel/register examples/cfg/rollup-build.js watch",
"serve:examples": "serve public",
"build:babel-transform-csx": "cd packages/babel-plugin-transform-csx && npm run build",
"build:csx": "cd packages/csx && npm run build",
"watch:babel-transform-csx": "cd packages/babel-plugin-transform-csx && npm run watch",
"watch:csx": "cd packages/csx && npm run watch-es6"
"watch:csx": "cd packages/csx && npm run watch"
}
}

View File

@ -1,3 +0,0 @@
src
test
*.log

View File

@ -1,19 +0,0 @@
# @babel/helpers
> Collection of helper functions used by Babel transforms.
See our website [@babel/helpers](https://babeljs.io/docs/en/next/babel-helpers.html) for more information.
## Install
Using npm:
```sh
npm install --save-dev @babel/helpers
```
or using yarn:
```sh
yarn add @babel/helpers --dev
```

File diff suppressed because it is too large Load Diff

View File

@ -1,276 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.get = get;
exports.minVersion = minVersion;
exports.getDependencies = getDependencies;
exports.ensure = ensure;
exports.default = exports.list = void 0;
var _traverse = _interopRequireDefault(require("@babel/traverse"));
var t = _interopRequireWildcard(require("@babel/types"));
var _helpers = _interopRequireDefault(require("./helpers"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function makePath(path) {
const parts = [];
for (; path.parentPath; path = path.parentPath) {
parts.push(path.key);
if (path.inList) parts.push(path.listKey);
}
return parts.reverse().join(".");
}
function getHelperMetadata(file) {
const globals = new Set();
const localBindingNames = new Set();
const dependencies = new Map();
let exportName;
let exportPath;
const exportBindingAssignments = [];
const importPaths = [];
const importBindingsReferences = [];
(0, _traverse.default)(file, {
ImportDeclaration(child) {
const name = child.node.source.value;
if (!_helpers.default[name]) {
throw child.buildCodeFrameError(`Unknown helper ${name}`);
}
if (child.get("specifiers").length !== 1 || !child.get("specifiers.0").isImportDefaultSpecifier()) {
throw child.buildCodeFrameError("Helpers can only import a default value");
}
const bindingIdentifier = child.node.specifiers[0].local;
dependencies.set(bindingIdentifier, name);
importPaths.push(makePath(child));
},
ExportDefaultDeclaration(child) {
const decl = child.get("declaration");
if (decl.isFunctionDeclaration()) {
if (!decl.node.id) {
throw decl.buildCodeFrameError("Helpers should give names to their exported func declaration");
}
exportName = decl.node.id.name;
}
exportPath = makePath(child);
},
ExportAllDeclaration(child) {
throw child.buildCodeFrameError("Helpers can only export default");
},
ExportNamedDeclaration(child) {
throw child.buildCodeFrameError("Helpers can only export default");
},
Statement(child) {
if (child.isModuleDeclaration()) return;
child.skip();
}
});
(0, _traverse.default)(file, {
Program(path) {
const bindings = path.scope.getAllBindings();
Object.keys(bindings).forEach(name => {
if (name === exportName) return;
if (dependencies.has(bindings[name].identifier)) return;
localBindingNames.add(name);
});
},
ReferencedIdentifier(child) {
const name = child.node.name;
const binding = child.scope.getBinding(name, true);
if (!binding) {
globals.add(name);
} else if (dependencies.has(binding.identifier)) {
importBindingsReferences.push(makePath(child));
}
},
AssignmentExpression(child) {
const left = child.get("left");
if (!(exportName in left.getBindingIdentifiers())) return;
if (!left.isIdentifier()) {
throw left.buildCodeFrameError("Only simple assignments to exports are allowed in helpers");
}
const binding = child.scope.getBinding(exportName);
if (binding && binding.scope.path.isProgram()) {
exportBindingAssignments.push(makePath(child));
}
}
});
if (!exportPath) throw new Error("Helpers must default-export something.");
exportBindingAssignments.reverse();
return {
globals: Array.from(globals),
localBindingNames: Array.from(localBindingNames),
dependencies,
exportBindingAssignments,
exportPath,
exportName,
importBindingsReferences,
importPaths
};
}
function permuteHelperAST(file, metadata, id, localBindings, getDependency) {
if (localBindings && !id) {
throw new Error("Unexpected local bindings for module-based helpers.");
}
if (!id) return;
const {
localBindingNames,
dependencies,
exportBindingAssignments,
exportPath,
exportName,
importBindingsReferences,
importPaths
} = metadata;
const dependenciesRefs = {};
dependencies.forEach((name, id) => {
dependenciesRefs[id.name] = typeof getDependency === "function" && getDependency(name) || id;
});
const toRename = {};
const bindings = new Set(localBindings || []);
localBindingNames.forEach(name => {
let newName = name;
while (bindings.has(newName)) newName = "_" + newName;
if (newName !== name) toRename[name] = newName;
});
if (id.type === "Identifier" && exportName !== id.name) {
toRename[exportName] = id.name;
}
(0, _traverse.default)(file, {
Program(path) {
const exp = path.get(exportPath);
const imps = importPaths.map(p => path.get(p));
const impsBindingRefs = importBindingsReferences.map(p => path.get(p));
const decl = exp.get("declaration");
if (id.type === "Identifier") {
if (decl.isFunctionDeclaration()) {
exp.replaceWith(decl);
} else {
exp.replaceWith(t.variableDeclaration("var", [t.variableDeclarator(id, decl.node)]));
}
} else if (id.type === "MemberExpression") {
if (decl.isFunctionDeclaration()) {
exportBindingAssignments.forEach(assignPath => {
const assign = path.get(assignPath);
assign.replaceWith(t.assignmentExpression("=", id, assign.node));
});
exp.replaceWith(decl);
path.pushContainer("body", t.expressionStatement(t.assignmentExpression("=", id, t.identifier(exportName))));
} else {
exp.replaceWith(t.expressionStatement(t.assignmentExpression("=", id, decl.node)));
}
} else {
throw new Error("Unexpected helper format.");
}
Object.keys(toRename).forEach(name => {
path.scope.rename(name, toRename[name]);
});
for (const path of imps) path.remove();
for (const path of impsBindingRefs) {
const node = t.cloneNode(dependenciesRefs[path.node.name]);
path.replaceWith(node);
}
path.stop();
}
});
}
const helperData = Object.create(null);
function loadHelper(name) {
if (!helperData[name]) {
const helper = _helpers.default[name];
if (!helper) {
throw Object.assign(new ReferenceError(`Unknown helper ${name}`), {
code: "BABEL_HELPER_UNKNOWN",
helper: name
});
}
const fn = () => {
return t.file(helper.ast());
};
const metadata = getHelperMetadata(fn());
helperData[name] = {
build(getDependency, id, localBindings) {
const file = fn();
permuteHelperAST(file, metadata, id, localBindings, getDependency);
return {
nodes: file.program.body,
globals: metadata.globals
};
},
minVersion() {
return helper.minVersion;
},
dependencies: metadata.dependencies
};
}
return helperData[name];
}
function get(name, getDependency, id, localBindings) {
return loadHelper(name).build(getDependency, id, localBindings);
}
function minVersion(name) {
return loadHelper(name).minVersion();
}
function getDependencies(name) {
return Array.from(loadHelper(name).dependencies.values());
}
function ensure(name) {
loadHelper(name);
}
const list = Object.keys(_helpers.default).map(name => name.replace(/^_/, "")).filter(name => name !== "__esModule");
exports.list = list;
var _default = get;
exports.default = _default;

View File

@ -1,21 +0,0 @@
{
"name": "@babel/helpers",
"version": "7.7.0",
"description": "Collection of helper functions used by Babel transforms.",
"author": "Sebastian McKenzie <sebmck@gmail.com>",
"homepage": "https://babeljs.io/",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"repository": "https://github.com/babel/babel/tree/master/packages/babel-helpers",
"main": "lib/index.js",
"dependencies": {
"@babel/template": "^7.7.0",
"@babel/traverse": "^7.7.0",
"@babel/types": "^7.7.0"
},
"devDependencies": {
"@babel/helper-plugin-test-runner": "^7.0.0"
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,291 +0,0 @@
import traverse from "@babel/traverse";
import * as t from "@babel/types";
import helpers from "./helpers";
function makePath(path) {
const parts = [];
for (; path.parentPath; path = path.parentPath) {
parts.push(path.key);
if (path.inList) parts.push(path.listKey);
}
return parts.reverse().join(".");
}
/**
* Given a file AST for a given helper, get a bunch of metadata about it so that Babel can quickly render
* the helper is whatever context it is needed in.
*/
function getHelperMetadata(file) {
const globals = new Set();
const localBindingNames = new Set();
// Maps imported identifier -> helper name
const dependencies = new Map();
let exportName;
let exportPath;
const exportBindingAssignments = [];
const importPaths = [];
const importBindingsReferences = [];
traverse(file, {
ImportDeclaration(child) {
const name = child.node.source.value;
if (!helpers[name]) {
throw child.buildCodeFrameError(`Unknown helper ${name}`);
}
if (
child.get("specifiers").length !== 1 ||
!child.get("specifiers.0").isImportDefaultSpecifier()
) {
throw child.buildCodeFrameError(
"Helpers can only import a default value",
);
}
const bindingIdentifier = child.node.specifiers[0].local;
dependencies.set(bindingIdentifier, name);
importPaths.push(makePath(child));
},
ExportDefaultDeclaration(child) {
const decl = child.get("declaration");
if (decl.isFunctionDeclaration()) {
if (!decl.node.id) {
throw decl.buildCodeFrameError(
"Helpers should give names to their exported func declaration",
);
}
exportName = decl.node.id.name;
}
exportPath = makePath(child);
},
ExportAllDeclaration(child) {
throw child.buildCodeFrameError("Helpers can only export default");
},
ExportNamedDeclaration(child) {
throw child.buildCodeFrameError("Helpers can only export default");
},
Statement(child) {
if (child.isModuleDeclaration()) return;
child.skip();
},
});
traverse(file, {
Program(path) {
const bindings = path.scope.getAllBindings();
Object.keys(bindings).forEach(name => {
if (name === exportName) return;
if (dependencies.has(bindings[name].identifier)) return;
localBindingNames.add(name);
});
},
ReferencedIdentifier(child) {
const name = child.node.name;
const binding = child.scope.getBinding(name, /* noGlobal */ true);
if (!binding) {
globals.add(name);
} else if (dependencies.has(binding.identifier)) {
importBindingsReferences.push(makePath(child));
}
},
AssignmentExpression(child) {
const left = child.get("left");
if (!(exportName in left.getBindingIdentifiers())) return;
if (!left.isIdentifier()) {
throw left.buildCodeFrameError(
"Only simple assignments to exports are allowed in helpers",
);
}
const binding = child.scope.getBinding(exportName);
if (binding && binding.scope.path.isProgram()) {
exportBindingAssignments.push(makePath(child));
}
},
});
if (!exportPath) throw new Error("Helpers must default-export something.");
// Process these in reverse so that mutating the references does not invalidate any later paths in
// the list.
exportBindingAssignments.reverse();
return {
globals: Array.from(globals),
localBindingNames: Array.from(localBindingNames),
dependencies,
exportBindingAssignments,
exportPath,
exportName,
importBindingsReferences,
importPaths,
};
}
/**
* Given a helper AST and information about how it will be used, update the AST to match the usage.
*/
function permuteHelperAST(file, metadata, id, localBindings, getDependency) {
if (localBindings && !id) {
throw new Error("Unexpected local bindings for module-based helpers.");
}
if (!id) return;
const {
localBindingNames,
dependencies,
exportBindingAssignments,
exportPath,
exportName,
importBindingsReferences,
importPaths,
} = metadata;
const dependenciesRefs = {};
dependencies.forEach((name, id) => {
dependenciesRefs[id.name] =
(typeof getDependency === "function" && getDependency(name)) || id;
});
const toRename = {};
const bindings = new Set(localBindings || []);
localBindingNames.forEach(name => {
let newName = name;
while (bindings.has(newName)) newName = "_" + newName;
if (newName !== name) toRename[name] = newName;
});
if (id.type === "Identifier" && exportName !== id.name) {
toRename[exportName] = id.name;
}
traverse(file, {
Program(path) {
// We need to compute these in advance because removing nodes would
// invalidate the paths.
const exp = path.get(exportPath);
const imps = importPaths.map(p => path.get(p));
const impsBindingRefs = importBindingsReferences.map(p => path.get(p));
const decl = exp.get("declaration");
if (id.type === "Identifier") {
if (decl.isFunctionDeclaration()) {
exp.replaceWith(decl);
} else {
exp.replaceWith(
t.variableDeclaration("var", [t.variableDeclarator(id, decl.node)]),
);
}
} else if (id.type === "MemberExpression") {
if (decl.isFunctionDeclaration()) {
exportBindingAssignments.forEach(assignPath => {
const assign = path.get(assignPath);
assign.replaceWith(t.assignmentExpression("=", id, assign.node));
});
exp.replaceWith(decl);
path.pushContainer(
"body",
t.expressionStatement(
t.assignmentExpression("=", id, t.identifier(exportName)),
),
);
} else {
exp.replaceWith(
t.expressionStatement(t.assignmentExpression("=", id, decl.node)),
);
}
} else {
throw new Error("Unexpected helper format.");
}
Object.keys(toRename).forEach(name => {
path.scope.rename(name, toRename[name]);
});
for (const path of imps) path.remove();
for (const path of impsBindingRefs) {
const node = t.cloneNode(dependenciesRefs[path.node.name]);
path.replaceWith(node);
}
// We only use "traverse" for all the handy scoping helpers, so we can stop immediately without
// actually doing the traversal.
path.stop();
},
});
}
const helperData = Object.create(null);
function loadHelper(name) {
if (!helperData[name]) {
const helper = helpers[name];
if (!helper) {
throw Object.assign(new ReferenceError(`Unknown helper ${name}`), {
code: "BABEL_HELPER_UNKNOWN",
helper: name,
});
}
const fn = () => {
return t.file(helper.ast());
};
const metadata = getHelperMetadata(fn());
helperData[name] = {
build(getDependency, id, localBindings) {
const file = fn();
permuteHelperAST(file, metadata, id, localBindings, getDependency);
return {
nodes: file.program.body,
globals: metadata.globals,
};
},
minVersion() {
return helper.minVersion;
},
dependencies: metadata.dependencies,
};
}
return helperData[name];
}
export function get(
name,
getDependency?: string => ?t.Expression,
id?,
localBindings?: string[],
) {
return loadHelper(name).build(getDependency, id, localBindings);
}
export function minVersion(name: string) {
return loadHelper(name).minVersion();
}
export function getDependencies(name: string): $ReadOnlyArray<string> {
return Array.from(loadHelper(name).dependencies.values());
}
export function ensure(name: string) {
loadHelper(name);
}
export const list = Object.keys(helpers)
.map(name => name.replace(/^_/, ""))
.filter(name => name !== "__esModule");
export default get;

View File

@ -1 +0,0 @@
REPLACE_ME;

View File

@ -1,3 +0,0 @@
{
"plugins": ["./plugin"]
}

View File

@ -1,5 +0,0 @@
function _$_basic_main() { return _$_basic_dependency(); }
function _$_basic_dependency() {}
_$_basic_main;

View File

@ -1,25 +0,0 @@
const defineHelper = require("../../../helpers/define-helper").default;
const dependency = defineHelper(__dirname, "dependency", `
export default function fn() {}
`);
const main = defineHelper(__dirname, "main", `
import dep from "${dependency}";
export default function helper() {
return dep();
}
`);
module.exports = function() {
return {
visitor: {
Identifier(path) {
if (path.node.name !== "REPLACE_ME") return;
const helper = this.addHelper(main);
path.replaceWith(helper);
},
},
};
};

View File

@ -1 +0,0 @@
REPLACE_ME;

View File

@ -1,3 +0,0 @@
{
"plugins": ["./plugin"]
}

View File

@ -1,7 +0,0 @@
function _$_deep_main() { return _$_deep_dependency(); }
function _$_deep_dependency() { return _$_deep_dependencyDeep; }
function _$_deep_dependencyDeep() {}
_$_deep_main;

View File

@ -1,30 +0,0 @@
const defineHelper = require("../../../helpers/define-helper").default;
const dependencyDeep = defineHelper(__dirname, "dependencyDeep", `
export default function fn() {}
`)
const dependency = defineHelper(__dirname, "dependency", `
import f from "${dependencyDeep}";
export default function fn() { return f; }
`);
const main = defineHelper(__dirname, "main", `
import dep from "${dependency}";
export default function helper() {
return dep();
}
`);
module.exports = function() {
return {
visitor: {
Identifier(path) {
if (path.node.name !== "REPLACE_ME") return;
const helper = this.addHelper(main);
path.replaceWith(helper);
},
},
};
};

View File

@ -1 +0,0 @@
REPLACE_ME;

View File

@ -1,4 +0,0 @@
{
"plugins": ["./plugin"],
"throws": " "
}

View File

@ -1,21 +0,0 @@
const defineHelper = require("../../../helpers/define-helper").default;
const main = defineHelper(__dirname, "main", `
import dep from "(!!!)%-..a,4892 missing";
export default function helper() {
return dep();
}
`);
module.exports = function() {
return {
visitor: {
Identifier(path) {
if (path.node.name !== "REPLACE_ME") return;
const helper = this.addHelper(main);
path.replaceWith(helper);
},
},
};
};

View File

@ -1 +0,0 @@
REPLACE_ME;

View File

@ -1,3 +0,0 @@
{
"plugins": ["./plugin"]
}

View File

@ -1,7 +0,0 @@
function _$_multiple_main() { return _$_multiple_dependency() + _$_multiple_dependency2(); }
function _$_multiple_dependency2() { 1; }
function _$_multiple_dependency() { 0; }
_$_multiple_main;

View File

@ -1,30 +0,0 @@
const defineHelper = require("../../../helpers/define-helper").default;
const dependency1 = defineHelper(__dirname, "dependency1", `
export default function fn() { 0; }
`);
const dependency2 = defineHelper(__dirname, "dependency2", `
export default function fn() { 1; }
`);
const main = defineHelper(__dirname, "main", `
import dep1 from "${dependency1}";
import dep2 from "${dependency2}";
export default function helper() {
return dep1() + dep2();
}
`);
module.exports = function() {
return {
visitor: {
Identifier(path) {
if (path.node.name !== "REPLACE_ME") return;
const helper = this.addHelper(main);
path.replaceWith(helper);
},
},
};
};

View File

@ -1,3 +0,0 @@
{
"plugins": ["./plugin"]
}

View File

@ -1,9 +0,0 @@
let _foo = "main";
function _$_renameBindingEqual_main() { return _$_renameBindingEqual_dependency() + _foo; }
let foo = "dependency";
function _$_renameBindingEqual_dependency() { return foo; }
_$_renameBindingEqual_main;

View File

@ -1,28 +0,0 @@
const defineHelper = require("../../../helpers/define-helper").default;
const dependency = defineHelper(__dirname, "dependency", `
let foo = "dependency";
export default function fn() { return foo }
`);
const main = defineHelper(__dirname, "main", `
import dep from "${dependency}";
let foo = "main";
export default function helper() {
return dep() + foo;
}
`);
module.exports = function() {
return {
visitor: {
Identifier(path) {
if (path.node.name !== "REPLACE_ME") return;
const helper = this.addHelper(main);
path.replaceWith(helper);
},
},
};
};

View File

@ -1,3 +0,0 @@
REPLACE_ME;
let Promise = "I will be a good guy!";

View File

@ -1,3 +0,0 @@
{
"plugins": ["./plugin"]
}

View File

@ -1,6 +0,0 @@
function _$_renameDeepGlobal_main() { return _$_renameDeepGlobal_dependency() || Promise; }
function _$_renameDeepGlobal_dependency() { return Promise; }
_$_renameDeepGlobal_main;
let _Promise = "I will be a good guy!";

View File

@ -1,27 +0,0 @@
const defineHelper = require("../../../helpers/define-helper").default;
const dependency = defineHelper(__dirname, "dependency", `
export default function fn() {
return Promise;
}
`);
const main = defineHelper(__dirname, "main", `
import dep from "${dependency}";
export default function helper() {
return dep() || Promise;
}
`);
module.exports = function() {
return {
visitor: {
Identifier(path) {
if (path.node.name !== "REPLACE_ME") return;
const helper = this.addHelper(main);
path.replaceWith(helper);
},
},
};
};

View File

@ -1,2 +0,0 @@
REPLACE_ME_1;
REPLACE_ME_2;

View File

@ -1,3 +0,0 @@
{
"plugins": ["./plugin"]
}

Some files were not shown because too many files have changed in this diff Show More