Skip to content

Commit 77fb38a

Browse files
committed
Merge branch 'master' into gh-592
2 parents 5c0d402 + 3e30b75 commit 77fb38a

File tree

156 files changed

+5650
-925
lines changed

Some content is hidden

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

156 files changed

+5650
-925
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
},
4343
"homepage": "https://github.com/sveltejs/svelte#README",
4444
"devDependencies": {
45+
"@types/mocha": "^2.2.41",
46+
"@types/node": "^7.0.22",
4547
"acorn": "^4.0.4",
4648
"babel": "^6.23.0",
4749
"babel-core": "^6.23.1",
@@ -74,9 +76,11 @@
7476
"rollup-plugin-commonjs": "^7.0.0",
7577
"rollup-plugin-json": "^2.1.0",
7678
"rollup-plugin-node-resolve": "^2.0.0",
79+
"rollup-plugin-typescript": "^0.8.1",
7780
"rollup-watch": "^3.2.2",
7881
"source-map": "^0.5.6",
79-
"source-map-support": "^0.4.8"
82+
"source-map-support": "^0.4.8",
83+
"typescript": "^2.3.2"
8084
},
8185
"nyc": {
8286
"include": [

rename.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const glob = require( 'glob' );
2+
const fs = require( 'fs' );
3+
4+
glob.sync( 'src/**/*.js' ).forEach( file => {
5+
console.log( file );
6+
const js = fs.readFileSync( file, 'utf-8' );
7+
fs.writeFileSync( file.replace( /\.js$/, '.ts' ), js );
8+
fs.unlinkSync( file );
9+
});

rollup/rollup.config.main.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
1+
import path from 'path';
12
import nodeResolve from 'rollup-plugin-node-resolve';
23
import commonjs from 'rollup-plugin-commonjs';
34
import json from 'rollup-plugin-json';
4-
import buble from 'rollup-plugin-buble';
5+
import typescript from 'rollup-plugin-typescript';
6+
7+
const src = path.resolve( 'src' );
58

69
export default {
7-
entry: 'src/index.js',
10+
entry: 'src/index.ts',
811
moduleName: 'svelte',
912
targets: [
1013
{ dest: 'compiler/svelte.js', format: 'umd' }
1114
],
1215
plugins: [
16+
{
17+
resolveId ( importee, importer ) {
18+
// bit of a hack — TypeScript only really works if it can resolve imports,
19+
// but they misguidedly chose to reject imports with file extensions. This
20+
// means we need to resolve them here
21+
if ( importer && importer.startsWith( src ) && importee[0] === '.' && path.extname( importee ) === '' ) {
22+
return path.resolve( path.dirname( importer ), `${importee}.ts` );
23+
}
24+
}
25+
},
1326
nodeResolve({ jsnext: true, module: true }),
1427
commonjs(),
1528
json(),
16-
buble({
29+
typescript({
1730
include: 'src/**',
1831
exclude: 'src/shared/**',
19-
target: {
20-
node: 4
21-
}
32+
typescript: require( 'typescript' )
2233
})
2334
],
2435
sourceMap: true

rollup/rollup.config.ssr.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ export default {
2020
}
2121
})
2222
],
23-
external: [ path.resolve( 'src/index.js' ), 'fs', 'path' ],
23+
external: [ path.resolve( 'src/index.ts' ), 'fs', 'path' ],
2424
paths: {
25-
[ path.resolve( 'src/index.js' ) ]: '../compiler/svelte.js'
25+
[ path.resolve( 'src/index.ts' ) ]: '../compiler/svelte.js'
2626
},
2727
sourceMap: true
2828
};

src/generators/Generator.js renamed to src/generators/Generator.ts

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,45 @@
11
import MagicString, { Bundle } from 'magic-string';
22
import { walk } from 'estree-walker';
3-
import isReference from '../utils/isReference.js';
4-
import flattenReference from '../utils/flattenReference.js';
5-
import globalWhitelist from '../utils/globalWhitelist.js';
6-
import reservedNames from '../utils/reservedNames.js';
7-
import namespaces from '../utils/namespaces.js';
8-
import { removeNode, removeObjectKey } from '../utils/removeNode.js';
9-
import getIntro from './shared/utils/getIntro.js';
10-
import getOutro from './shared/utils/getOutro.js';
11-
import processCss from './shared/processCss.js';
12-
import annotateWithScopes from '../utils/annotateWithScopes.js';
3+
import isReference from '../utils/isReference';
4+
import flattenReference from '../utils/flattenReference';
5+
import globalWhitelist from '../utils/globalWhitelist';
6+
import reservedNames from '../utils/reservedNames';
7+
import namespaces from '../utils/namespaces';
8+
import { removeNode, removeObjectKey } from '../utils/removeNode';
9+
import getIntro from './shared/utils/getIntro';
10+
import getOutro from './shared/utils/getOutro';
11+
import processCss from './shared/processCss';
12+
import annotateWithScopes from '../utils/annotateWithScopes';
13+
import DomBlock from './dom/Block';
14+
import SsrBlock from './server-side-rendering/Block';
15+
import { Node, Parsed, CompileOptions } from '../interfaces';
1316

1417
const test = typeof global !== 'undefined' && global.__svelte_test;
1518

1619
export default class Generator {
17-
constructor ( parsed, source, name, options ) {
20+
parsed: Parsed;
21+
source: string;
22+
name: string;
23+
options: CompileOptions;
24+
25+
imports: Node[];
26+
helpers: Set<string>;
27+
components: Set<string>;
28+
events: Set<string>;
29+
transitions: Set<string>;
30+
importedComponents: Map<string, string>;
31+
32+
bindingGroups: string[];
33+
expectedProperties: Set<string>;
34+
css: string;
35+
cssId: string;
36+
usesRefs: boolean;
37+
38+
importedNames: Set<string>;
39+
aliases: Map<string, string>;
40+
usedNames: Set<string>;
41+
42+
constructor ( parsed: Parsed, source: string, name: string, options: CompileOptions ) {
1843
this.parsed = parsed;
1944
this.source = source;
2045
this.name = name;
@@ -39,33 +64,33 @@ export default class Generator {
3964
this.usesRefs = false;
4065

4166
// allow compiler to deconflict user's `import { get } from 'whatever'` and
42-
// Svelte's builtin `import { get, ... } from 'svelte/shared.js'`;
67+
// Svelte's builtin `import { get, ... } from 'svelte/shared.ts'`;
4368
this.importedNames = new Set();
4469
this.aliases = new Map();
45-
this._usedNames = new Set( [ name ] );
70+
this.usedNames = new Set( [ name ] );
4671
}
4772

48-
addSourcemapLocations ( node ) {
73+
addSourcemapLocations ( node: Node ) {
4974
walk( node, {
50-
enter: node => {
75+
enter: ( node: Node ) => {
5176
this.code.addSourcemapLocation( node.start );
5277
this.code.addSourcemapLocation( node.end );
5378
}
5479
});
5580
}
5681

57-
alias ( name ) {
82+
alias ( name: string ) {
5883
if ( !this.aliases.has( name ) ) {
5984
this.aliases.set( name, this.getUniqueName( name ) );
6085
}
6186

6287
return this.aliases.get( name );
6388
}
6489

65-
contextualise ( block, expression, context, isEventHandler ) {
90+
contextualise ( block: DomBlock | SsrBlock, expression: Node, context: string, isEventHandler: boolean ) {
6691
this.addSourcemapLocations( expression );
6792

68-
const usedContexts = [];
93+
const usedContexts: string[] = [];
6994

7095
const { code, helpers } = this;
7196
const { contexts, indexes } = block;
@@ -76,7 +101,7 @@ export default class Generator {
76101
const self = this;
77102

78103
walk( expression, {
79-
enter ( node, parent, key ) {
104+
enter ( node: Node, parent: Node, key: string ) {
80105
if ( /^Function/.test( node.type ) ) lexicalDepth += 1;
81106

82107
if ( node._scope ) {
@@ -138,7 +163,7 @@ export default class Generator {
138163
}
139164
},
140165

141-
leave ( node ) {
166+
leave ( node: Node ) {
142167
if ( /^Function/.test( node.type ) ) lexicalDepth -= 1;
143168
if ( node._scope ) scope = scope.parent;
144169
}
@@ -151,16 +176,16 @@ export default class Generator {
151176
};
152177
}
153178

154-
findDependencies ( contextDependencies, indexes, expression ) {
179+
findDependencies ( contextDependencies: Map<string, string[]>, indexes: Map<string, string>, expression: Node ) {
155180
if ( expression._dependencies ) return expression._dependencies;
156181

157182
let scope = annotateWithScopes( expression );
158-
const dependencies = [];
183+
const dependencies: string[] = [];
159184

160185
const generator = this; // can't use arrow functions, because of this.skip()
161186

162187
walk( expression, {
163-
enter ( node, parent ) {
188+
enter ( node: Node, parent: Node ) {
164189
if ( node._scope ) {
165190
scope = node._scope;
166191
return;
@@ -180,7 +205,7 @@ export default class Generator {
180205
}
181206
},
182207

183-
leave ( node ) {
208+
leave ( node: Node ) {
184209
if ( node._scope ) scope = scope.parent;
185210
}
186211
});
@@ -196,22 +221,22 @@ export default class Generator {
196221

197222
generate ( result, options, { name, format } ) {
198223
if ( this.imports.length ) {
199-
const statements = [];
224+
const statements: string[] = [];
200225

201226
this.imports.forEach( ( declaration, i ) => {
202227
if ( format === 'es' ) {
203228
statements.push( this.source.slice( declaration.start, declaration.end ) );
204229
return;
205230
}
206231

207-
const defaultImport = declaration.specifiers.find( x => x.type === 'ImportDefaultSpecifier' || x.type === 'ImportSpecifier' && x.imported.name === 'default' );
208-
const namespaceImport = declaration.specifiers.find( x => x.type === 'ImportNamespaceSpecifier' );
209-
const namedImports = declaration.specifiers.filter( x => x.type === 'ImportSpecifier' && x.imported.name !== 'default' );
232+
const defaultImport = declaration.specifiers.find( ( x: Node ) => x.type === 'ImportDefaultSpecifier' || x.type === 'ImportSpecifier' && x.imported.name === 'default' );
233+
const namespaceImport = declaration.specifiers.find( ( x: Node ) => x.type === 'ImportNamespaceSpecifier' );
234+
const namedImports = declaration.specifiers.filter( ( x: Node ) => x.type === 'ImportSpecifier' && x.imported.name !== 'default' );
210235

211236
const name = ( defaultImport || namespaceImport ) ? ( defaultImport || namespaceImport ).local.name : `__import${i}`;
212237
declaration.name = name; // hacky but makes life a bit easier later
213238

214-
namedImports.forEach( specifier => {
239+
namedImports.forEach( ( specifier: Node ) => {
215240
statements.push( `var ${specifier.local.name} = ${name}.${specifier.imported.name}` );
216241
});
217242

@@ -230,7 +255,7 @@ export default class Generator {
230255

231256
const compiled = new Bundle({ separator: '' });
232257

233-
function addString ( str ) {
258+
function addString ( str: string ) {
234259
compiled.addSource({
235260
content: new MagicString( str )
236261
});
@@ -250,7 +275,7 @@ export default class Generator {
250275
});
251276
}
252277

253-
parts.forEach( str => {
278+
parts.forEach( ( str: string ) => {
254279
const chunk = str.replace( pattern, '' );
255280
if ( chunk ) addString( chunk );
256281

@@ -274,11 +299,11 @@ export default class Generator {
274299
};
275300
}
276301

277-
getUniqueName ( name ) {
302+
getUniqueName ( name: string ) {
278303
if ( test ) name = `${name}$`;
279304
let alias = name;
280-
for ( let i = 1; reservedNames.has( alias ) || this.importedNames.has( alias ) || this._usedNames.has( alias ); alias = `${name}_${i++}` );
281-
this._usedNames.add( alias );
305+
for ( let i = 1; reservedNames.has( alias ) || this.importedNames.has( alias ) || this.usedNames.has( alias ); alias = `${name}_${i++}` );
306+
this.usedNames.add( alias );
282307
return alias;
283308
}
284309

@@ -287,13 +312,13 @@ export default class Generator {
287312
return name => {
288313
if ( test ) name = `${name}$`;
289314
let alias = name;
290-
for ( let i = 1; reservedNames.has( alias ) || this.importedNames.has( alias ) || this._usedNames.has( alias ) || localUsedNames.has( alias ); alias = `${name}_${i++}` );
315+
for ( let i = 1; reservedNames.has( alias ) || this.importedNames.has( alias ) || this.usedNames.has( alias ) || localUsedNames.has( alias ); alias = `${name}_${i++}` );
291316
localUsedNames.add( alias );
292317
return alias;
293318
};
294319
}
295320

296-
parseJs ( ssr ) {
321+
parseJs ( ssr: boolean = false ) {
297322
const { source } = this;
298323
const { js } = this.parsed;
299324

@@ -315,23 +340,23 @@ export default class Generator {
315340
removeNode( this.code, js.content, node );
316341
imports.push( node );
317342

318-
node.specifiers.forEach( specifier => {
343+
node.specifiers.forEach( ( specifier: Node ) => {
319344
this.importedNames.add( specifier.local.name );
320345
});
321346
}
322347
}
323348

324-
const defaultExport = body.find( node => node.type === 'ExportDefaultDeclaration' );
349+
const defaultExport = body.find( ( node: Node ) => node.type === 'ExportDefaultDeclaration' );
325350

326351
if ( defaultExport ) {
327-
defaultExport.declaration.properties.forEach( prop => {
352+
defaultExport.declaration.properties.forEach( ( prop: Node ) => {
328353
templateProperties[ prop.key.name ] = prop;
329354
});
330355
}
331356

332357
[ 'helpers', 'events', 'components', 'transitions' ].forEach( key => {
333358
if ( templateProperties[ key ] ) {
334-
templateProperties[ key ].value.properties.forEach( prop => {
359+
templateProperties[ key ].value.properties.forEach( ( prop: node ) => {
335360
this[ key ].add( prop.key.name );
336361
});
337362
}
@@ -340,11 +365,11 @@ export default class Generator {
340365
if ( templateProperties.computed ) {
341366
const dependencies = new Map();
342367

343-
templateProperties.computed.value.properties.forEach( prop => {
368+
templateProperties.computed.value.properties.forEach( ( prop: Node ) => {
344369
const key = prop.key.name;
345370
const value = prop.value;
346371

347-
const deps = value.params.map( param => param.type === 'AssignmentPattern' ? param.left.name : param.name );
372+
const deps = value.params.map( ( param: Node ) => param.type === 'AssignmentPattern' ? param.left.name : param.name );
348373
dependencies.set( key, deps );
349374
});
350375

@@ -362,7 +387,7 @@ export default class Generator {
362387
computations.push({ key, deps });
363388
}
364389

365-
templateProperties.computed.value.properties.forEach( prop => visit( prop.key.name ) );
390+
templateProperties.computed.value.properties.forEach( ( prop: Node ) => visit( prop.key.name ) );
366391
}
367392

368393
if ( templateProperties.namespace ) {
@@ -374,7 +399,7 @@ export default class Generator {
374399

375400
if ( templateProperties.components ) {
376401
let hasNonImportedComponent = false;
377-
templateProperties.components.value.properties.forEach( property => {
402+
templateProperties.components.value.properties.forEach( ( property: Node ) => {
378403
const key = property.key.name;
379404
const value = source.slice( property.value.start, property.value.end );
380405
if ( this.importedNames.has( value ) ) {

0 commit comments

Comments
 (0)