8000 Simpler codegen by Rich-Harris · Pull Request #559 · sveltejs/svelte · GitHub
[go: up one dir, main page]

Skip to content

Simpler codegen #559

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

Merged
merged 8 commits into from
May 4, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
collapse constructor block into builders.main
  • Loading branch information
Rich-Harris committed May 3, 2017
commit a5f7fe79ea70c2c56e6bb02136f36d945108c4f2
70 changes: 20 additions & 50 deletions src/generators/dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,55 +151,6 @@ export default function dom ( parsed, source, options ) {
` );
}

const constructorBlock = new CodeBuilder();

constructorBlock.addLine( `options = options || {};` );
if ( generator.usesRefs ) constructorBlock.addLine( `this.refs = {};` );

constructorBlock.addLine(
`this._state = ${templateProperties.data ? `${generator.helper( 'assign' )}( ${generator.alias( 'template' )}.data(), options.data )` : `options.data || {}`};`
);

if ( !generator.builders.metaBindings.isEmpty() ) {
constructorBlock.addBlock( generator.builders.metaBindings );
}

if ( computations.length ) {
constructorBlock.addLine(
`${generator.alias( 'recompute' )}( this._state, this._state, {}, true );`
);
}

if ( options.dev ) {
generator.expectedProperties.forEach( prop => {
constructorBlock.addLine(
`if ( !( '${prop}' in this._state ) ) console.warn( "Component was created without expected data property '${prop}'" );`
);
});

constructorBlock.addBlock(
`if ( !options.target && !options._root ) throw new Error( "'target' is a required option" );`
);
}

if ( generator.bindingGroups.length ) {
constructorBlock.addLine( `this._bindingGroups = [ ${Array( generator.bindingGroups.length ).fill( '[]' ).join( ', ' )} ];` );
}

constructorBlock.addBlock( deindent`
this._observers = {
pre: Object.create( null ),
post: Object.create( null )
};

this._handlers = Object.create( null );

this._root = options._root || this;
this._yield = options._yield;

${builders.init}
` );

const sharedPath = options.shared === true ? 'svelte/shared.js' : options.shared;

const prototypeBase = `${name}.prototype` + ( templateProperties.methods ? `, ${generator.alias( 'template' )}.methods` : '' );
Expand All @@ -215,7 +166,26 @@ export default function dom ( parsed, source, options ) {
// TODO deprecate component.teardown()
builders.main.addBlock( deindent`
function ${name} ( options ) {
${constructorBlock}
options = options || {};
${options.dev && `if ( !options.target && !options._root ) throw new Error( "'target' is a required option" );`}
${generator.usesRefs && `this.refs = {};`}
this._state = ${templateProperties.data ? `${generator.helper( 'assign' )}( ${generator.alias( 'template' )}.data(), options.data )` : `options.data || {}`};
${generator.builders.metaBindings}
${computations.length && `${generator.alias( 'recompute' )}( this._state, this._state, {}, true );`}
${options.dev && Array.from( generator.expectedProperties ).map( prop => `if ( !( '${prop}' in this._state ) ) console.warn( "Component was created without expected data property '${prop}'" );`)}
${generator.bindingGroups.length && `this._bindingGroups = [ ${Array( generator.bindingGroups.length ).fill( '[]' ).join( ', ' )} ];`}

this._observers = {
pre: Object.create( null ),
post: Object.create( null )
};

this._handlers = Object.create( null );

this._root = options._root || this;
this._yield = options._yield;

${builders.init}
}

${generator.helper( 'assign' )}( ${prototypeBase}, ${proto});
Expand Down
6 changes: 5 additions & 1 deletion src/utils/deindent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ export default function deindent ( strings, ...values ) {
let trailingIndentation = getTrailingIndentation( result );

for ( let i = 1; i < strings.length; i += 1 ) {
const expression = values[ i - 1 ];
let expression = values[ i - 1 ];
const string = strings[i].replace( pattern, '' );

if ( Array.isArray( expression ) ) {
expression = expression.length ? expression.join( '\n' ) : null;
}

if ( expression || expression === '' ) {
const value = String( expression ).replace( /\n/g, `\n${trailingIndentation}` );
result += value + string;
Expand Down
0