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

Fix some typing errors #596

Merged
merged 1 commit into from
May 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
},
"homepage": "https://github.com/sveltejs/svelte#README",
"devDependencies": {
"@types/mocha": "^2.2.41",
"@types/node": "^7.0.22",
"acorn": "^4.0.4",
"babel": "^6.23.0",
"babel-core": "^6.23.1",
Expand Down
3 changes: 2 additions & 1 deletion src/generators/dom/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface State {
name: string;
namespace: string;
parentNode: string;
isTopLevel: boolean
Expand All @@ -7,4 +8,4 @@ export interface State {
inEachBlock?: boolean;
allUsedContexts?: string[];
usesComponent?: boolean;
}
}
9 changes: 7 additions & 2 deletions src/generators/shared/utils/getGlobals.ts
Original file line number Diff line number Diff line change
@@ -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 );

return imports.map( x => {
Expand Down Expand Up @@ -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 === 'object' ) {
return id => globals[ id ];
Expand Down
35 changes: 27 additions & 8 deletions src/generators/shared/utils/getIntro.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
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 === 'amd' ) return getAmdIntro( options, imports );
if ( format === 'cjs' ) return getCjsIntro( options, imports );
Expand All @@ -12,7 +31,7 @@ export default function getIntro ( format: string, options, imports ) {
throw new Error( `Not implemented: ${format}` );
}

function getAmdIntro ( options, imports ) {
function getAmdIntro ( options: Options, imports: Declaration[] ) {
const sourceString = imports.length ?
`[ ${imports.map( declaration => `'${removeExtension( declaration.source.value )}'` ).join( ', ' )} ], ` :
'';
Expand All @@ -22,7 +41,7 @@ function getAmdIntro ( options, imports ) {
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
.map( declaration => `var ${declaration.name} = require( '${declaration.source.value}' );` )
.join( '\n\n' );
Expand All @@ -34,15 +53,15 @@ function getCjsIntro ( options, imports ) {
return `'use strict';\n\n`;
}

function getIifeIntro ( options, imports ) {
function getIifeIntro ( options: Options, imports: Declaration[] ) {
if ( !options.name ) {
throw new Error( `Missing required 'name' option for IIFE export` );
}

return `var ${options.name} = (function (${paramString( imports )}) { 'use strict';\n\n`;
}

function getUmdIntro ( options, imports ) {
function getUmdIntro ( options: Options, imports: Declaration[] ) {
if ( !options.name ) {
throw new Error( `Missing required 'name' option for UMD export` );
}
Expand All @@ -61,11 +80,11 @@ function getUmdIntro ( options, imports ) {
}(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`;
}

function paramString ( imports ) {
function paramString ( imports: Declaration[] ) {
return imports.length ? ` ${imports.map( dep => dep.name ).join( ', ' )} ` : '';
}

Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"compilerOptions": {
"types" : ["node"],
"noImplicitAny": true,
"diagnostics": true,
"noImplicitThis": true,
Expand All @@ -15,4 +14,4 @@
"exclude": [
"node_modules"
]
}
}