diff --git a/baselines/requireJquery.d.ts b/baselines/requireJquery.d.ts new file mode 100644 index 0000000..7d80676 --- /dev/null +++ b/baselines/requireJquery.d.ts @@ -0,0 +1,5 @@ +declare namespace requireJquery { + function jqeury(w: any): any; + +} + diff --git a/baselines/simple.d.ts b/baselines/simple.d.ts new file mode 100644 index 0000000..00f3327 --- /dev/null +++ b/baselines/simple.d.ts @@ -0,0 +1,4 @@ +declare const simple: { + test: number; +}; + diff --git a/lib/run.ts b/lib/run.ts index 95ef897..e20be44 100644 --- a/lib/run.ts +++ b/lib/run.ts @@ -83,8 +83,7 @@ try { const filename = args['expression-file']; name = path.basename(filename, path.extname(filename)).replace(/[^A-Za-z0-9]/g, '_'); (module as any).paths.unshift(process.cwd() + '/node_modules'); - const fileContent = fs.readFileSync(filename, "utf-8"); - result = guess.generateIdentifierDeclarationFile(name, eval(fileContent)); + result = guess.generateIdentifierDeclarationFile(name, require(path.resolve(filename))); } else if (args.identifier) { if (args.name) throw new ArgsError('Cannot use --name with --identifier'); if (args.module || args.expression) throw new ArgsError('Cannot specify more than one input'); diff --git a/package.json b/package.json index 3f242d8..8621532 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "lodash": "latest", "mocha": "^3.0.2", "react": "latest", + "shx": "^0.3.3", "tslint": "^5.1.0", "webpack": "^1.13.1" }, @@ -30,7 +31,7 @@ ], "preferGlobal": true, "scripts": { - "test": "mocha bin/tests/test.js", + "test": "shx cp -r ./tests/fixtures/ ./bin/tests && mocha bin/tests/test.js", "build": "tsc", "build-browser": "tsc -p browser && webpack", "lint": "tslint --project tsconfig.json --format stylish" diff --git a/tests/fixtures/require-jquery.js b/tests/fixtures/require-jquery.js new file mode 100644 index 0000000..cf23b31 --- /dev/null +++ b/tests/fixtures/require-jquery.js @@ -0,0 +1,3 @@ +const jqeury = require("jquery") + +exports.jqeury = jqeury; diff --git a/tests/fixtures/simple.js b/tests/fixtures/simple.js new file mode 100644 index 0000000..d154606 --- /dev/null +++ b/tests/fixtures/simple.js @@ -0,0 +1 @@ +exports.test = 1 diff --git a/tests/test.ts b/tests/test.ts index 9e4c985..650a54e 100644 --- a/tests/test.ts +++ b/tests/test.ts @@ -44,6 +44,11 @@ const expressions: { [s: string]: any } = { overriddenToString, }; +const expressionFiles: { [s: string]: any } = { + simple: path.join(__dirname, "fixtures", "simple.js"), + requireJquery: path.join(__dirname, "fixtures", "require-jquery.js"), +}; + function checkDeclarationBaseline(name: string, content: string) { const filename = path.join(__dirname, `../../baselines/${name}`); const existing = fs.existsSync(filename) ? fs.readFileSync(filename, 'utf-8') : ''; @@ -70,3 +75,12 @@ describe("Expression tests", () => { }); } }); + +describe("Expression file test", () => { + for (const key of Object.keys(expressionFiles)) { + it(`Generates the same declaration for ${key}`, () => { + const result = tsg.generateIdentifierDeclarationFile(key!, require(expressionFiles[key!])); + checkDeclarationBaseline(`${key}.d.ts`, result); + }); + } +});