Skip to content

Commit

Permalink
fix: typings break with new typescript version
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyben committed Sep 25, 2021
1 parent 0d1922d commit 7f0f774
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ export function emit(program: ts.Program, { basePath, clean, copyOtherToOutDir,
let { diagnostics, emitSkipped, emittedFiles } = program.emit(undefined, dtsInterceptor)

if (options.listEmittedFiles && emittedFiles) {
if (bundleDeclaration) emittedFiles = emittedFiles.filter((path) => !dtsCache.has(path))
if (bundleDeclaration) {
emittedFiles = emittedFiles.filter((path) => !dtsCache.has(path))
}
console.log('Emitted files:\n' + emittedFiles.join('\n'))
}

Expand Down
14 changes: 7 additions & 7 deletions src/bundle-addon/declaration-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class DeclarationCollector {
private collectExports([exportSymbol, [origSymbol, references]]: MapEntry<SymbolCollector['exportSymbols']>): void {
// We look in the first declaration to retrieve common source file,
// since merged and overloaded declarations are in the same source file.
const sourceFile = origSymbol.declarations[0].getSourceFile()
const sourceFile = origSymbol.declarations![0].getSourceFile()

const exportName = exportSymbol.escapedName
const origName = origSymbol.escapedName
Expand Down Expand Up @@ -139,7 +139,7 @@ export class DeclarationCollector {
}

// External module augmentations are not detected as external, and would be duplicated.
if (this.shouldHandleExternalAugmentation(origSymbol.declarations[0])) {
if (this.shouldHandleExternalAugmentation(origSymbol.declarations![0])) {
this.debug('export', 'is-augmentation-of-external-lib:', exportName)
this.handleSymbolFromExternalLibrary(exportSymbol, true)
return
Expand Down Expand Up @@ -169,7 +169,7 @@ export class DeclarationCollector {
// If the value was exported as is, an intermediary variable named '_default' was created by the compiler.
// todo: default namespace and type
if (origSymbol.flags & ts.SymbolFlags.Variable) {
const exportRealName = getDeclarationName(exportSymbol.declarations[0])
const exportRealName = getDeclarationName(exportSymbol.declarations![0])

this.debug('export', 'default-variable-to-declare:', exportRealName || origName)
this.declarations.registerInternal(origSymbol, {
Expand Down Expand Up @@ -386,7 +386,7 @@ export class DeclarationCollector {
// Retrieve the right name in case of default export.
const refPropOrigName =
refPropOrigSymbol.escapedName === ts.InternalSymbolName.Default
? getDeclarationName(refPropOrigSymbol.declarations[0])
? getDeclarationName(refPropOrigSymbol.declarations![0])
: refPropOrigSymbol.escapedName

if (!refPropOrigName || refPropOrigName === ts.InternalSymbolName.Default) {
Expand Down Expand Up @@ -592,11 +592,11 @@ export class DeclarationCollector {
let aliasName: ts.__String | undefined = aliasSymbol.escapedName

if (aliasName === ts.InternalSymbolName.Default) {
aliasName = getDeclarationName(aliasSymbol.declarations[0])
aliasName = getDeclarationName(aliasSymbol.declarations![0])
}

if (aliasName === ts.InternalSymbolName.Default) {
aliasName = getDeclarationName(origAliasSymbol.declarations[0])
aliasName = getDeclarationName(origAliasSymbol.declarations![0])
}

if (!aliasName || aliasName === ts.InternalSymbolName.Default) {
Expand Down Expand Up @@ -625,7 +625,7 @@ export class DeclarationCollector {
importKind: ts.SyntaxKind
moduleName: string
} {
const [firstDeclaration] = symbol.declarations
const [firstDeclaration] = symbol.declarations!

const moduleSpecifier = getModuleSpecifier(firstDeclaration)

Expand Down
6 changes: 4 additions & 2 deletions src/bundle-addon/declaration-registrar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export class DeclarationRegistrar {
symbolReplacements?: Replacement[][]
}
) {
if (!origSymbol.declarations) return

const isNamespace = origSymbol.declarations.some(ts.isSourceFile)
if (isNamespace) {
throw Error(`File ${origSymbol.escapedName} is imported as a namespace, and thus can't be bundled.`)
Expand Down Expand Up @@ -85,7 +87,7 @@ export class DeclarationRegistrar {
): void {
const name = symbol.escapedName

if (symbol.declarations.length > 1) {
if (symbol.declarations && symbol.declarations.length > 1) {
console.warn(Color.yellow(`External/Json symbol with multiple/merged declarations not supported: ${name}`))
}

Expand Down Expand Up @@ -132,7 +134,7 @@ export class DeclarationRegistrar {
}

registerGlobal(origSymbol: ts.Symbol) {
for (const declaration of origSymbol.declarations) {
for (const declaration of origSymbol.declarations || []) {
const sourceFile = declaration.getSourceFile()

let declarationText = declaration.getText(sourceFile)
Expand Down
11 changes: 5 additions & 6 deletions src/bundle-addon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,23 @@ import { print } from './printer'
import { ensureAbsolutePath } from '../utils/path'

/**
* Intercepts the declaration file to a cache instead of writing it.
* @internal
*/
export function getDtsInterceptor(dtsCache: Map<string, string>) {
export function getDtsInterceptor(dtsCache: Map<string, string>): ts.WriteFileCallback {
// https://github.com/microsoft/TypeScript/blob/v3.6.4/src/compiler/program.ts#L151-L171

const writeFile: ts.WriteFileCallback = (fileName, data, writeByteOrderMark, onError, sourceFiles) => {
return (fileName, data, writeByteOrderMark, onError, sourceFiles) => {
try {
if (fileName.endsWith(ts.Extension.Dts) || fileName.endsWith('.d.ts.map')) {
dtsCache.set(fileName, data)
} else {
ts.sys.writeFile(fileName, data, writeByteOrderMark)
}
} catch (err) {
} catch (err: any) {
if (onError) onError(err.message)
}
}

return writeFile
}

/**
Expand Down Expand Up @@ -87,7 +86,7 @@ export function bundleDts(
if (dtsOptions.listEmittedFiles) {
console.log('Emitted files:\n' + entryPoints.join('\n'))
}
} catch (error) {
} catch (error: any) {
if (!fallbackOnError) throw Color.red(error)

console.error(Color.red(error.stack))
Expand Down
8 changes: 5 additions & 3 deletions src/bundle-addon/symbol-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export class SymbolCollector {
* Retrieves unexported symbols used by exported symbols.
*/
private getReferences(origSymbol: ts.Symbol, symbolsChain: ts.Symbol[] = []): Reference[] {
if (!origSymbol.declarations) return []

// Don't search in external symbol declarations.
// We need to check every declaration because of augmentations that could lead to false negatives.
if (
Expand Down Expand Up @@ -204,7 +206,7 @@ export class SymbolCollector {

moduleSymbol.exports.forEach((symbol, name) => {
if (name === ts.InternalSymbolName.ExportStar) return
const pos = symbol.declarations[0].getStart(entryFile)
const pos = symbol.declarations![0].getStart(entryFile)
entryFilePositions.set(symbol, pos)
})
}
Expand All @@ -215,7 +217,7 @@ export class SymbolCollector {
const starExportSymbol = moduleSymbol.exports.get(ts.InternalSymbolName.ExportStar)
if (!starExportSymbol) return

for (const exportDeclaration of starExportSymbol.declarations) {
for (const exportDeclaration of starExportSymbol.declarations || []) {
if (!ts.isExportDeclaration(exportDeclaration)) continue

const resolved = this.getResolvedModule(exportDeclaration)
Expand Down Expand Up @@ -292,7 +294,7 @@ export class SymbolCollector {
const starExportSymbol = moduleSymbol.exports.get(ts.InternalSymbolName.ExportStar)
if (!starExportSymbol) return

for (const declaration of starExportSymbol.declarations) {
for (const declaration of starExportSymbol.declarations || []) {
if (!ts.isExportDeclaration(declaration)) continue

const resolved = this.getResolvedModule(declaration)
Expand Down
2 changes: 1 addition & 1 deletion src/copy-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function copyOtherFiles(program: ts.Program) {

try {
cp(srcOtherFile, destOtherFile)
} catch (error) {
} catch (error: any) {
if (error.code === 'EEXIST') console.warn(Color.yellow(error.message))
else throw error
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function readdirPaths(dir: string) {
*/
function tryIsDirectory(path: string): boolean {
const stats = fsSyncRetry(fs.lstatSync, path, ['EPERM'], 2)
return stats.isDirectory()
return stats!.isDirectory()
}

/**
Expand Down Expand Up @@ -86,7 +86,7 @@ function fsSyncRetry<T extends (path: string) => any>(

try {
return fsSyncFn(path)
} catch (err) {
} catch (err: any) {
if (!errorCodes.includes(err.code) || round > tries) throw err

fixWindowsEPERM(path)
Expand Down

0 comments on commit 7f0f774

Please sign in to comment.