From 2a0b8dec6b86f22d1201ac7046396f052dfb3cdf Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 26 May 2017 12:24:56 +0200 Subject: [PATCH] Fix typing errors --- src/generators/dom/interfaces.ts | 3 +- src/generators/shared/utils/getGlobals.ts | 9 ++++-- src/generators/shared/utils/getIntro.ts | 35 +++++++++++++++++------ tsconfig.json | 4 +-- 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/generators/dom/interfaces.ts b/src/generators/dom/interfaces.ts index 6385d9356677..a58069da9006 100644 --- a/src/generators/dom/interfaces.ts +++ b/src/generators/dom/interfaces.ts @@ -1,4 +1,5 @@ export interface State { + name: string; namespace: string; parentNode: string; isTopLevel: boolean @@ -7,4 +8,4 @@ export interface State { inEachBlock?: boolean; allUsedContexts?: string[]; usesComponent?: boolean; -} \ No newline at end of file +} diff --git a/src/generators/shared/utils/getGlobals.ts b/src/generators/shared/utils/getGlobals.ts index 0d3e7bad142c..3ba58cf29aa1 100644 --- a/src/generators/shared/utils/getGlobals.ts +++ b/src/generators/shared/utils/getGlobals.ts @@ -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 => { @@ -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 ]; diff --git a/src/generators/shared/utils/getIntro.ts b/src/generators/shared/utils/getIntro.ts index 6b9bf9781520..dab45cec2051 100644 --- a/src/generators/shared/utils/getIntro.ts +++ b/src/generators/shared/utils/getIntro.ts @@ -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 ); @@ -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( ', ' )} ], ` : ''; @@ -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' ); @@ -34,7 +53,7 @@ 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` ); } @@ -42,7 +61,7 @@ function getIifeIntro ( options, imports ) { 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` ); } @@ -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( ', ' )} ` : ''; } diff --git a/tsconfig.json b/tsconfig.json index f81940868c10..d39fe1e232cf 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "types" : ["node"], + "types" : ["node", "mocha"], "noImplicitAny": true, "diagnostics": true, "noImplicitThis": true, @@ -15,4 +15,4 @@ "exclude": [ "node_modules" ] -} \ No newline at end of file +}