Skip to content

Commit dc48ae6

Browse files
authored
Merge pull request #813 from sveltejs/tidy-up
more consistent style for generated code
2 parents 0e80248 + e993ae0 commit dc48ae6

File tree

62 files changed

+1357
-1388
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1357
-1388
lines changed

src/generators/Generator.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,10 @@ export default class Generator {
210210
}
211211

212212
if (globalWhitelist.has(name)) {
213-
code.prependRight(node.start, `( '${name}' in state ? state.`);
213+
code.prependRight(node.start, `('${name}' in state ? state.`);
214214
code.appendLeft(
215215
node.object ? node.object.end : node.end,
216-
` : ${name} )`
216+
` : ${name})`
217217
);
218218
} else {
219219
code.prependRight(node.start, `state.`);
@@ -337,7 +337,7 @@ export default class Generator {
337337

338338
if (defaultImport) {
339339
statements.push(
340-
`${name} = ( ${name} && ${name}.__esModule ) ? ${name}['default'] : ${name};`
340+
`${name} = (${name} && ${name}.__esModule) ? ${name}['default'] : ${name};`
341341
);
342342
}
343343
});
@@ -629,8 +629,8 @@ export default class Generator {
629629
// user code gets wrapped in an IIFE
630630
if (js.content.body.length) {
631631
const prefix = hasDefaultExport
632-
? `var ${this.alias('template')} = (function () {`
633-
: `(function () {`;
632+
? `var ${this.alias('template')} = (function() {`
633+
: `(function() {`;
634634
this.code
635635
.prependRight(js.content.start, prefix)
636636
.appendLeft(js.content.end, '}());');

src/generators/dom/Block.ts

+19-21
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export default class Block {
135135
this.mount(name, parentNode);
136136

137137
if (isToplevel) {
138-
this.builders.unmount.addLine(`@detachNode( ${name} );`);
138+
this.builders.unmount.addLine(`@detachNode(${name});`);
139139
}
140140
}
141141

@@ -180,9 +180,9 @@ export default class Block {
180180

181181
mount(name: string, parentNode: string) {
182182
if (parentNode) {
183-
this.builders.mount.addLine(`@appendNode( ${name}, ${parentNode} );`);
183+
this.builders.mount.addLine(`@appendNode(${name}, ${parentNode});`);
184184
} else {
185-
this.builders.mount.addLine(`@insertNode( ${name}, #target, anchor );`);
185+
this.builders.mount.addLine(`@insertNode(${name}, #target, anchor);`);
186186
}
187187
}
188188

@@ -225,7 +225,7 @@ export default class Block {
225225
properties.addBlock(`create: @noop,`);
226226
} else {
227227
properties.addBlock(deindent`
228-
create: function () {
228+
create: function() {
229229
${this.builders.create}
230230
${!this.builders.hydrate.isEmpty() && `this.hydrate();`}
231231
},
@@ -237,7 +237,7 @@ export default class Block {
237237
properties.addBlock(`claim: @noop,`);
238238
} else {
239239
properties.addBlock(deindent`
240-
claim: function ( nodes ) {
240+
claim: function(nodes) {
241241
${this.builders.claim}
242242
${!this.builders.hydrate.isEmpty() && `this.hydrate();`}
243243
},
@@ -247,7 +247,7 @@ export default class Block {
247247

248248
if (!this.builders.hydrate.isEmpty()) {
249249
properties.addBlock(deindent`
250-
hydrate: function ( nodes ) {
250+
hydrate: function(nodes) {
251251
${this.builders.hydrate}
252252
},
253253
`);
@@ -257,7 +257,7 @@ export default class Block {
257257
properties.addBlock(`mount: @noop,`);
258258
} else {
259259
properties.addBlock(deindent`
260-
mount: function ( #target, anchor ) {
260+
mount: function(#target, anchor) {
261261
${this.builders.mount}
262262
},
263263
`);
@@ -268,7 +268,7 @@ export default class Block {
268268
properties.addBlock(`update: @noop,`);
269269
} else {
270270
properties.addBlock(deindent`
271-
update: function ( changed, ${this.params.join(', ')} ) {
271+
update: function(changed, ${this.params.join(', ')}) {
272272
${this.builders.update}
273273
},
274274
`);
@@ -278,20 +278,20 @@ export default class Block {
278278
if (this.hasIntroMethod) {
279279
if (hasIntros) {
280280
properties.addBlock(deindent`
281-
intro: function ( #target, anchor ) {
282-
if ( ${introing} ) return;
281+
intro: function(#target, anchor) {
282+
if (${introing}) return;
283283
${introing} = true;
284284
${hasOutros && `${outroing} = false;`}
285285
286286
${this.builders.intro}
287287
288-
this.mount( #target, anchor );
288+
this.mount(#target, anchor);
289289
},
290290
`);
291291
} else {
292292
properties.addBlock(deindent`
293-
intro: function ( #target, anchor ) {
294-
this.mount( #target, anchor );
293+
intro: function(#target, anchor) {
294+
this.mount(#target, anchor);
295295
},
296296
`);
297297
}
@@ -300,8 +300,8 @@ export default class Block {
300300
if (this.hasOutroMethod) {
301301
if (hasOutros) {
302302
properties.addBlock(deindent`
303-
outro: function ( ${this.alias('outrocallback')} ) {
304-
if ( ${outroing} ) return;
303+
outro: function(${this.alias('outrocallback')}) {
304+
if (${outroing}) return;
305305
${outroing} = true;
306306
${hasIntros && `${introing} = false;`}
307307
@@ -312,7 +312,7 @@ export default class Block {
312312
`);
313313
} else {
314314
properties.addBlock(deindent`
315-
outro: function ( outrocallback ) {
315+
outro: function(outrocallback) {
316316
outrocallback();
317317
},
318318
`);
@@ -323,7 +323,7 @@ export default class Block {
323323
properties.addBlock(`unmount: @noop,`);
324324
} else {
325325
properties.addBlock(deindent`
326-
unmount: function () {
326+
unmount: function() {
327327
${this.builders.unmount}
328328
},
329329
`);
@@ -333,16 +333,14 @@ export default class Block {
333333
properties.addBlock(`destroy: @noop`);
334334
} else {
335335
properties.addBlock(deindent`
336-
destroy: function () {
336+
destroy: function() {
337337
${this.builders.destroy}
338338
}
339339
`);
340340
}
341341

342342
return deindent`
343-
function ${this.name} ( ${this.params.join(', ')}, #component${this.key
344-
? `, ${localKey}`
345-
: ''} ) {
343+
function ${this.name}(${this.params.join(', ')}, #component${this.key ? `, ${localKey}` : ''}) {
346344
${this.variables.size > 0 &&
347345
`var ${Array.from(this.variables.keys())
348346
.map(key => {

src/generators/dom/index.ts

+37-41
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ export default function dom(
9191

9292
const condition = `isInitial || ${deps.map(dep => `changed.${dep}`).join(' || ')}`;
9393

94-
const statement = `if ( @differs( ( state.${key} = @template.computed.${key}( ${deps
94+
const statement = `if (@differs((state.${key} = @template.computed.${key}(${deps
9595
.map(dep => `state.${dep}`)
96-
.join(', ')} ) ), oldState.${key} ) ) changed.${key} = true;`;
96+
.join(', ')})), oldState.${key})) changed.${key} = true;`;
9797

9898
computationBuilder.addConditional(condition, statement);
9999
});
@@ -105,8 +105,8 @@ export default function dom(
105105

106106
if (generator.needsEncapsulateHelper) {
107107
builder.addBlock(deindent`
108-
function @encapsulateStyles ( node ) {
109-
@setAttribute( node, '${generator.stylesheet.id}', '' );
108+
function @encapsulateStyles(node) {
109+
@setAttribute(node, "${generator.stylesheet.id}", "");
110110
}
111111
`);
112112
}
@@ -118,11 +118,11 @@ export default function dom(
118118

119119
if (styles && generator.options.css !== false && !generator.customElement) {
120120
builder.addBlock(deindent`
121-
function @add_css () {
122-
var style = @createElement( 'style' );
121+
function @add_css() {
122+
var style = @createElement("style");
123123
style.id = '${generator.stylesheet.id}-style';
124124
style.textContent = ${styles};
125-
@appendNode( style, document.head );
125+
@appendNode(style, document.head);
126126
}
127127
`);
128128
}
@@ -149,30 +149,28 @@ export default function dom(
149149

150150
const constructorBody = deindent`
151151
${options.dev && !generator.customElement &&
152-
`if ( !options || (!options.target && !options._root) ) throw new Error( "'target' is a required option" );`}
152+
`if (!options || (!options.target && !options._root)) throw new Error("'target' is a required option");`}
153153
this.options = options;
154154
${generator.usesRefs && `this.refs = {};`}
155155
this._state = ${templateProperties.data
156-
? `@assign( @template.data(), options.data )`
156+
? `@assign(@template.data(), options.data)`
157157
: `options.data || {}`};
158158
${generator.metaBindings}
159-
${computations.length && `this._recompute( {}, this._state, {}, true );`}
159+
${computations.length && `this._recompute({}, this._state, {}, true);`}
160160
${options.dev &&
161161
Array.from(generator.expectedProperties).map(
162162
prop =>
163-
`if ( !( '${prop}' in this._state ) ) console.warn( "Component was created without expected data property '${prop}'" );`
163+
`if (!('${prop}' in this._state)) console.warn("Component was created without expected data property '${prop}'");`
164164
)}
165165
${generator.bindingGroups.length &&
166-
`this._bindingGroups = [ ${Array(generator.bindingGroups.length)
167-
.fill('[]')
168-
.join(', ')} ];`}
166+
`this._bindingGroups = [${Array(generator.bindingGroups.length).fill('[]').join(', ')}];`}
169167
170168
this._observers = {
171-
pre: Object.create( null ),
172-
post: Object.create( null )
169+
pre: Object.create(null),
170+
post: Object.create(null)
173171
};
174172
175-
this._handlers = Object.create( null );
173+
this._handlers = Object.create(null);
176174
${templateProperties.ondestroy && `this._handlers.destroy = [@template.ondestroy]`}
177175
178176
this._root = options._root || this;
@@ -186,13 +184,13 @@ export default function dom(
186184
${css && `this.shadowRoot.innerHTML = \`<style>${options.dev ? `${css}\n/*# sourceMappingURL=${cssMap.toUrl()} */` : css}</style>\`;`}
187185
` :
188186
(generator.stylesheet.hasStyles && options.css !== false &&
189-
`if ( !document.getElementById( '${generator.stylesheet.id}-style' ) ) @add_css();`)
187+
`if (!document.getElementById("${generator.stylesheet.id}-style")) @add_css();`)
190188
}
191189
192-
${templateProperties.oncreate && `var oncreate = @template.oncreate.bind( this );`}
190+
${templateProperties.oncreate && `var oncreate = @template.oncreate.bind(this);`}
193191
194192
${(templateProperties.oncreate || generator.hasComponents || generator.hasComplexBindings || generator.hasIntroTransitions) && deindent`
195-
if ( !options._root ) {
193+
if (!options._root) {
196194
this._oncreate = [${templateProperties.oncreate && `oncreate`}];
197195
${(generator.hasComponents || generator.hasComplexBindings) && `this._beforecreate = [];`}
198196
${(generator.hasComponents || generator.hasIntroTransitions) && `this._aftercreate = [];`}
@@ -205,22 +203,22 @@ export default function dom(
205203
206204
${generator.slots.size && `this.slots = {};`}
207205
208-
this._fragment = @create_main_fragment( this._state, this );
206+
this._fragment = @create_main_fragment(this._state, this);
209207
210-
if ( options.target ) {
208+
if (options.target) {
211209
${generator.hydratable
212210
? deindent`
213-
var nodes = @children( options.target );
214-
options.hydrate ? this._fragment.claim( nodes ) : this._fragment.create();
215-
nodes.forEach( @detachNode );
211+
var nodes = @children(options.target);
212+
options.hydrate ? this._fragment.claim(nodes) : this._fragment.create();
213+
nodes.forEach(@detachNode);
216214
` :
217215
deindent`
218-
${options.dev && `if ( options.hydrate ) throw new Error( 'options.hydrate only works if the component was compiled with the \`hydratable: true\` option' );`}
216+
${options.dev && `if (options.hydrate) throw new Error("options.hydrate only works if the component was compiled with the \`hydratable: true\` option");`}
219217
this._fragment.create();
220218
`}
221219
${generator.customElement ?
222-
`this._mount( options.target, options.anchor || null );` :
223-
`this._fragment.${block.hasIntroMethod ? 'intro' : 'mount'}( options.target, options.anchor || null );`}
220+
`this._mount(options.target, options.anchor || null);` :
221+
`this._fragment.${block.hasIntroMethod ? 'intro' : 'mount'}(options.target, options.anchor || null);`}
224222
225223
${(generator.hasComponents || generator.hasComplexBindings || templateProperties.oncreate || generator.hasIntroTransitions) && deindent`
226224
${generator.hasComponents && `this._lock = true;`}
@@ -263,13 +261,13 @@ export default function dom(
263261
});
264262
}`}
265263
266-
attributeChangedCallback ( attr, oldValue, newValue ) {
264+
attributeChangedCallback(attr, oldValue, newValue) {
267265
this.set({ [attr]: newValue });
268266
}
269267
}
270268
271-
customElements.define('${generator.tag}', ${name});
272-
@assign( ${prototypeBase}, ${proto}, {
269+
customElements.define("${generator.tag}", ${name});
270+
@assign(${prototypeBase}, ${proto}, {
273271
_mount(target, anchor) {
274272
this._fragment.${block.hasIntroMethod ? 'intro' : 'mount'}(this.shadowRoot, null);
275273
target.insertBefore(this, anchor);
@@ -282,32 +280,32 @@ export default function dom(
282280
`);
283281
} else {
284282
builder.addBlock(deindent`
285-
function ${name} ( options ) {
283+
function ${name}(options) {
286284
${constructorBody}
287285
}
288286
289-
@assign( ${prototypeBase}, ${proto});
287+
@assign(${prototypeBase}, ${proto});
290288
`);
291289
}
292290

293291
// TODO deprecate component.teardown()
294292
builder.addBlock(deindent`
295293
${options.dev && deindent`
296-
${name}.prototype._checkReadOnly = function _checkReadOnly ( newState ) {
294+
${name}.prototype._checkReadOnly = function _checkReadOnly(newState) {
297295
${Array.from(generator.readonly).map(
298296
prop =>
299-
`if ( '${prop}' in newState && !this._updatingReadonlyProperty ) throw new Error( "Cannot set read-only property '${prop}'" );`
297+
`if ('${prop}' in newState && !this._updatingReadonlyProperty) throw new Error("Cannot set read-only property '${prop}'");`
300298
)}
301299
};
302300
`}
303301
304302
${computations.length ? deindent`
305-
${name}.prototype._recompute = function _recompute ( changed, state, oldState, isInitial ) {
303+
${name}.prototype._recompute = function _recompute(changed, state, oldState, isInitial) {
306304
${computationBuilder}
307305
}
308306
` : (!sharedPath && `${name}.prototype._recompute = @noop;`)}
309307
310-
${templateProperties.setup && `@template.setup( ${name} );`}
308+
${templateProperties.setup && `@template.setup(${name});`}
311309
`);
312310

313311
const usedHelpers = new Set();
@@ -340,7 +338,7 @@ export default function dom(
340338

341339
else if (format === 'cjs') {
342340
const SHARED = '__shared';
343-
let requires = `var ${SHARED} = require( ${JSON.stringify(sharedPath)} );`;
341+
let requires = `var ${SHARED} = require(${JSON.stringify(sharedPath)});`;
344342
used.forEach(name => {
345343
const alias = generator.alias(name);
346344
requires += `\nvar ${alias} = ${SHARED}.${name};`;
@@ -390,9 +388,7 @@ export default function dom(
390388
// special case
391389
const global = `_svelteTransitionManager`;
392390

393-
result += `\n\nvar ${generator.alias(
394-
'transitionManager'
395-
)} = window.${global} || ( window.${global} = ${code});`;
391+
result += `\n\nvar ${generator.alias('transitionManager')} = window.${global} || (window.${global} = ${code});`;
396392
} else {
397393
const alias = generator.alias(expression.id.name);
398394
if (alias !== expression.id.name)

0 commit comments

Comments
 (0)