Skip to content

Commit

Permalink
Merge pull request #26 from kimamula/fix-25
Browse files Browse the repository at this point in the history
Fix #25
  • Loading branch information
kimamula authored Jan 18, 2021
2 parents e1a4eac + 33941d9 commit ec07712
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
18 changes: 15 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion test/compile/compile.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
8 changes: 4 additions & 4 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
25 changes: 15 additions & 10 deletions transformer.ts
Original file line number Diff line number Diff line change
@@ -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<ts.SourceFile> {
return (context: ts.TransformationContext) => (file: ts.SourceFile) => visitNodeAndChildren(file, program, context);
Expand Down Expand Up @@ -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[]) {
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"suppressImplicitAnyIndexErrors": true,
"noEmitOnError": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
"noFallthroughCasesInSwitch": true,
"esModuleInterop": true
},
"include": [
"transformer.ts",
Expand Down

0 comments on commit ec07712

Please sign in to comment.