Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simpler codegen #673

Merged
merged 12 commits into from
Jun 26, 2017
3 changes: 2 additions & 1 deletion mocha.opts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test/test.js
--bail
test/test.js
66 changes: 28 additions & 38 deletions src/generators/dom/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import CodeBuilder from '../../utils/CodeBuilder';
import deindent from '../../utils/deindent';
import { DomGenerator } from './index';
import { Node } from '../../interfaces';
import shared from './shared';

export interface BlockOptions {
name: string;
Expand Down Expand Up @@ -61,9 +62,6 @@ export default class Block {
variables: Map<string, string>;
getUniqueName: (name: string) => string;

component: string;
target: string;

hasUpdateMethod: boolean;
autofocus: string;

Expand Down Expand Up @@ -110,10 +108,6 @@ export default class Block {
this.variables = new Map();
this.getUniqueName = this.generator.getUniqueNameMaker(options.params);

// unique names
this.component = this.getUniqueName('component');
this.target = this.getUniqueName('target');

this.hasUpdateMethod = false; // determined later
}

Expand All @@ -134,14 +128,12 @@ export default class Block {

this.addVariable(name);
this.builders.create.addLine(`${name} = ${renderStatement};`);
this.builders.claim.addLine(`${name} = ${claimStatement};`)
this.builders.claim.addLine(`${name} = ${claimStatement};`);

this.mount(name, parentNode);

if (isToplevel) {
this.builders.unmount.addLine(
`${this.generator.helper('detachNode')}( ${name} );`
);
this.builders.unmount.addLine(`@detachNode( ${name} );`);
}
}

Expand Down Expand Up @@ -186,14 +178,9 @@ export default class Block {

mount(name: string, parentNode: string) {
if (parentNode) {
this.builders.mount.addLine(
`${this.generator.helper('appendNode')}( ${name}, ${parentNode} );`
);
this.builders.mount.addLine(`@appendNode( ${name}, ${parentNode} );`);
} else {
this.builders.mount.addLine(
`${this.generator.helper('insertNode')}( ${name}, ${this
.target}, anchor );`
);
this.builders.mount.addLine(`@insertNode( ${name}, #target, anchor );`);
}
}

Expand Down Expand Up @@ -229,11 +216,11 @@ export default class Block {

if (this.first) {
properties.addBlock(`first: null,`);
this.builders.hydrate.addLine( `this.first = ${this.first};` );
this.builders.hydrate.addLine(`this.first = ${this.first};`);
}

if (this.builders.create.isEmpty()) {
properties.addBlock(`create: ${this.generator.helper('noop')},`);
properties.addBlock(`create: @noop,`);
} else {
properties.addBlock(deindent`
create: function () {
Expand All @@ -245,7 +232,7 @@ export default class Block {

if (this.generator.hydratable) {
if (this.builders.claim.isEmpty()) {
properties.addBlock(`claim: ${this.generator.helper('noop')},`);
properties.addBlock(`claim: @noop,`);
} else {
properties.addBlock(deindent`
claim: function ( nodes ) {
Expand All @@ -265,18 +252,18 @@ export default class Block {
}

if (this.builders.mount.isEmpty()) {
properties.addBlock(`mount: ${this.generator.helper('noop')},`);
properties.addBlock(`mount: @noop,`);
} else {
properties.addBlock(deindent`
mount: function ( ${this.target}, anchor ) {
mount: function ( #target, anchor ) {
${this.builders.mount}
},
`);
}

if (this.hasUpdateMethod) {
if (this.builders.update.isEmpty()) {
properties.addBlock(`update: ${this.generator.helper('noop')},`);
properties.addBlock(`update: @noop,`);
} else {
properties.addBlock(deindent`
update: function ( changed, ${this.params.join(', ')} ) {
Expand All @@ -289,20 +276,20 @@ export default class Block {
if (this.hasIntroMethod) {
if (hasIntros) {
properties.addBlock(deindent`
intro: function ( ${this.target}, anchor ) {
intro: function ( #target, anchor ) {
if ( ${introing} ) return;
${introing} = true;
${hasOutros && `${outroing} = false;`}

${this.builders.intro}

this.mount( ${this.target}, anchor );
this.mount( #target, anchor );
},
`);
} else {
properties.addBlock(deindent`
intro: function ( ${this.target}, anchor ) {
this.mount( ${this.target}, anchor );
intro: function ( #target, anchor ) {
this.mount( #target, anchor );
},
`);
}
Expand Down Expand Up @@ -331,7 +318,7 @@ export default class Block {
}

if (this.builders.unmount.isEmpty()) {
properties.addBlock(`unmount: ${this.generator.helper('noop')},`);
properties.addBlock(`unmount: @noop,`);
} else {
properties.addBlock(deindent`
unmount: function () {
Expand All @@ -341,7 +328,7 @@ export default class Block {
}

if (this.builders.destroy.isEmpty()) {
properties.addBlock(`destroy: ${this.generator.helper('noop')}`);
properties.addBlock(`destroy: @noop`);
} else {
properties.addBlock(deindent`
destroy: function () {
Expand All @@ -351,22 +338,25 @@ export default class Block {
}

return deindent`
function ${this.name} ( ${this.params.join(', ')}, ${this.component}${this
.key
function ${this.name} ( ${this.params.join(', ')}, #component${this.key
? `, ${localKey}`
: ''} ) {
${this.variables.size > 0 && (
`var ${Array.from(this.variables.keys()).map(key => {
const init = this.variables.get(key);
return init !== undefined ? `${key} = ${init}` : key;
}).join(', ')};`)}
${this.variables.size > 0 &&
`var ${Array.from(this.variables.keys())
.map(key => {
const init = this.variables.get(key);
return init !== undefined ? `${key} = ${init}` : key;
})
.join(', ')};`}

${!this.builders.init.isEmpty() && this.builders.init}

return {
${properties}
};
}
`;
`.replace(/(\\)?#(\w*)/g, (match, escaped, name) => {
return escaped ? match.slice(1) : this.alias(name);
});
}
}
Loading