Fix typing errors

pull/596/head
Marvin Hagemeister 7 years ago
parent 84a1571eed
commit 61cbb25aef

@ -42,6 +42,8 @@
}, },
"homepage": "https://github.com/sveltejs/svelte#README", "homepage": "https://github.com/sveltejs/svelte#README",
"devDependencies": { "devDependencies": {
"@types/mocha": "^2.2.41",
"@types/node": "^7.0.22",
"acorn": "^4.0.4", "acorn": "^4.0.4",
"babel": "^6.23.0", "babel": "^6.23.0",
"babel-core": "^6.23.1", "babel-core": "^6.23.1",

@ -1,4 +1,5 @@
export interface State { export interface State {
name: string;
namespace: string; namespace: string;
parentNode: string; parentNode: string;
isTopLevel: boolean isTopLevel: boolean
@ -7,4 +8,4 @@ export interface State {
inEachBlock?: boolean; inEachBlock?: boolean;
allUsedContexts?: string[]; allUsedContexts?: string[];
usesComponent?: boolean; usesComponent?: boolean;
} }

@ -1,4 +1,9 @@
export default function getGlobals ( imports, { globals, onerror, onwarn } ) { import { Declaration, Options } from './getIntro';
export type Globals = (id: string) => any;
export default function getGlobals ( imports: Declaration[], options: Options ) {
const { globals, onerror, onwarn } = options;
const globalFn = getGlobalFn( globals ); const globalFn = getGlobalFn( globals );
return imports.map( x => { return imports.map( x => {
@ -33,7 +38,7 @@ export default function getGlobals ( imports, { globals, onerror, onwarn } ) {
}); });
} }
function getGlobalFn ( globals ) { function getGlobalFn ( globals: any ): Globals {
if ( typeof globals === 'function' ) return globals; if ( typeof globals === 'function' ) return globals;
if ( typeof globals === 'object' ) { if ( typeof globals === 'object' ) {
return id => globals[ id ]; return id => globals[ id ];

@ -1,7 +1,26 @@
import deindent from '../../../utils/deindent.js'; import deindent from '../../../utils/deindent.js';
import getGlobals from './getGlobals'; import getGlobals, { Globals } from './getGlobals';
export type ModuleFormat = "es" | "amd" | "cjs" | "iife" | "umd" | "eval";
export interface Options {
name: string;
amd?: {
id?: string;
};
globals: Globals | object;
onerror: (err: Error) => void;
onwarn: (obj: Error | { message: string }) => void;
}
export interface Declaration {
name: string;
source: {
value: string;
};
}
export default function getIntro ( format: string, options, imports ) { export default function getIntro ( format: ModuleFormat, options: Options, imports: Declaration[] ) {
if ( format === 'es' ) return ''; if ( format === 'es' ) return '';
if ( format === 'amd' ) return getAmdIntro( options, imports ); if ( format === 'amd' ) return getAmdIntro( options, imports );
if ( format === 'cjs' ) return getCjsIntro( options, imports ); if ( format === 'cjs' ) return getCjsIntro( options, imports );
@ -12,7 +31,7 @@ export default function getIntro ( format: string, options, imports ) {
throw new Error( `Not implemented: ${format}` ); throw new Error( `Not implemented: ${format}` );
} }
function getAmdIntro ( options, imports ) { function getAmdIntro ( options: Options, imports: Declaration[] ) {
const sourceString = imports.length ? const sourceString = imports.length ?
`[ ${imports.map( declaration => `'${removeExtension( declaration.source.value )}'` ).join( ', ' )} ], ` : `[ ${imports.map( declaration => `'${removeExtension( declaration.source.value )}'` ).join( ', ' )} ], ` :
''; '';
@ -22,7 +41,7 @@ function getAmdIntro ( options, imports ) {
return `define(${id ? ` '${id}', ` : ''}${sourceString}function (${paramString( imports )}) { 'use strict';\n\n`; return `define(${id ? ` '${id}', ` : ''}${sourceString}function (${paramString( imports )}) { 'use strict';\n\n`;
} }
function getCjsIntro ( options, imports ) { function getCjsIntro ( options: Options, imports: Declaration[] ) {
const requireBlock = imports const requireBlock = imports
.map( declaration => `var ${declaration.name} = require( '${declaration.source.value}' );` ) .map( declaration => `var ${declaration.name} = require( '${declaration.source.value}' );` )
.join( '\n\n' ); .join( '\n\n' );
@ -34,7 +53,7 @@ function getCjsIntro ( options, imports ) {
return `'use strict';\n\n`; return `'use strict';\n\n`;
} }
function getIifeIntro ( options, imports ) { function getIifeIntro ( options: Options, imports: Declaration[] ) {
if ( !options.name ) { if ( !options.name ) {
throw new Error( `Missing required 'name' option for IIFE export` ); throw new Error( `Missing required 'name' option for IIFE export` );
} }
@ -42,7 +61,7 @@ function getIifeIntro ( options, imports ) {
return `var ${options.name} = (function (${paramString( imports )}) { 'use strict';\n\n`; return `var ${options.name} = (function (${paramString( imports )}) { 'use strict';\n\n`;
} }
function getUmdIntro ( options, imports ) { function getUmdIntro ( options: Options, imports: Declaration[] ) {
if ( !options.name ) { if ( !options.name ) {
throw new Error( `Missing required 'name' option for UMD export` ); throw new Error( `Missing required 'name' option for UMD export` );
} }
@ -61,11 +80,11 @@ function getUmdIntro ( options, imports ) {
}(this, (function (${paramString( imports )}) { 'use strict';` + '\n\n'; }(this, (function (${paramString( imports )}) { 'use strict';` + '\n\n';
} }
function getEvalIntro ( options, imports ) { function getEvalIntro ( options: Options, imports: Declaration[] ) {
return `(function (${paramString( imports )}) { 'use strict';\n\n`; return `(function (${paramString( imports )}) { 'use strict';\n\n`;
} }
function paramString ( imports ) { function paramString ( imports: Declaration[] ) {
return imports.length ? ` ${imports.map( dep => dep.name ).join( ', ' )} ` : ''; return imports.length ? ` ${imports.map( dep => dep.name ).join( ', ' )} ` : '';
} }

@ -1,6 +1,5 @@
{ {
"compilerOptions": { "compilerOptions": {
"types" : ["node"],
"noImplicitAny": true, "noImplicitAny": true,
"diagnostics": true, "diagnostics": true,
"noImplicitThis": true, "noImplicitThis": true,
@ -15,4 +14,4 @@
"exclude": [ "exclude": [
"node_modules" "node_modules"
] ]
} }

Loading…
Cancel
Save