From 33941d9017ac37db78c8646dbd73c6e37138c708 Mon Sep 17 00:00:00 2001 From: kimamula Date: Mon, 18 Jan 2021 14:20:47 +0900 Subject: [PATCH] Fix #25 --- package-lock.json | 18 +++++++++++++++--- test/compile/compile.ts | 3 ++- test/index.ts | 8 ++++---- transformer.ts | 25 +++++++++++++++---------- tsconfig.json | 3 ++- 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index 41d3b88..ecf04ee 100644 --- a/package-lock.json +++ b/package-lock.json @@ -468,6 +468,18 @@ "yargs": "13.3.0", "yargs-parser": "13.1.1", "yargs-unparser": "1.6.0" + }, + "dependencies": { + "yargs-parser": { + "version": "13.1.1", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-13.1.1.tgz", + "integrity": "sha512-oVAVsHz6uFrg3XQheFII8ESO2ssAf9luWuAd6Wexsu4F3OtIW0o8IribPXYrD4WC24LWtPrJlGy87y5udK+dxQ==", + "dev": true, + "requires": { + "camelcase": "^5.0.0", + "decamelize": "^1.2.0" + } + } } }, "ms": { @@ -777,9 +789,9 @@ } }, "yargs-parser": { - "version": "13.1.1", - "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-13.1.1.tgz", - "integrity": "sha512-oVAVsHz6uFrg3XQheFII8ESO2ssAf9luWuAd6Wexsu4F3OtIW0o8IribPXYrD4WC24LWtPrJlGy87y5udK+dxQ==", + "version": "13.1.2", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-13.1.2.tgz", + "integrity": "sha512-3lbsNRf/j+A4QuSZfDRA7HRSfWrzO0YjqTJd5kjAq37Zep1CEgaYmrH9Q3GwPiB9cHyd1Y1UwggGhJGoxipbzg==", "dev": true, "requires": { "camelcase": "^5.0.0", diff --git a/test/compile/compile.ts b/test/compile/compile.ts index a9486f0..7250f77 100644 --- a/test/compile/compile.ts +++ b/test/compile/compile.ts @@ -1,10 +1,11 @@ -import * as ts from 'typescript'; +import ts from 'typescript'; import transformer from '../../transformer'; export function compile(filePaths: string[], target = ts.ScriptTarget.ES5, writeFileCallback?: ts.WriteFileCallback) { const program = ts.createProgram(filePaths, { strict: true, noEmitOnError: true, + esModuleInterop: true, target }); const transformers: ts.CustomTransformers = { diff --git a/test/index.ts b/test/index.ts index 0b19550..85a9567 100644 --- a/test/index.ts +++ b/test/index.ts @@ -1,8 +1,8 @@ import { enumerate } from '../index'; -import * as assert from 'assert'; -import * as path from 'path'; -import * as fs from 'fs'; -import * as ts from 'typescript'; +import assert from 'assert'; +import path from 'path'; +import fs from 'fs'; +import ts from 'typescript'; import { compile } from './compile/compile'; import { NS, Colors } from './external'; diff --git a/transformer.ts b/transformer.ts index 1da79fd..0b20254 100644 --- a/transformer.ts +++ b/transformer.ts @@ -1,5 +1,5 @@ -import * as ts from 'typescript'; -import * as path from 'path'; +import ts from 'typescript'; +import path from 'path'; export default function transformer(program: ts.Program): ts.TransformerFactory { return (context: ts.TransformationContext) => (file: ts.SourceFile) => visitNodeAndChildren(file, program, context); @@ -56,14 +56,19 @@ function isEnumerateCallExpression(node: ts.Node, typeChecker: ts.TypeChecker): if (typeof signature === 'undefined') { return false; } - - const { declaration } = signature; - - return !!declaration - && !ts.isJSDocSignature(declaration) - && path.join(declaration.getSourceFile().fileName) === indexTs - && !!declaration.name - && declaration.name.getText() === 'enumerate'; + const declaration = typeChecker.getResolvedSignature(node)?.declaration; + if (!declaration || ts.isJSDocSignature(declaration) || declaration.name?.getText() !== 'enumerate') { + return false; + } + try { + // require.resolve is required to resolve symlink. + // https://github.com/kimamula/ts-transformer-keys/issues/4#issuecomment-643734716 + return require.resolve(declaration.getSourceFile().fileName) === indexTs; + } catch { + // declaration.getSourceFile().fileName may not be in Node.js require stack and require.resolve may result in an error. + // https://github.com/kimamula/ts-transformer-keys/issues/47 + return false; + } } function resolveStringLiteralTypes(type: ts.Type, literals: string[]) { diff --git a/tsconfig.json b/tsconfig.json index 2a64cfd..7466726 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,8 @@ "suppressImplicitAnyIndexErrors": true, "noEmitOnError": true, "noImplicitReturns": true, - "noFallthroughCasesInSwitch": true + "noFallthroughCasesInSwitch": true, + "esModuleInterop": true }, "include": [ "transformer.ts",