Skip to content
This repository was archived by the owner on Dec 15, 2023. It is now read-only.

Fix expression-file by using require and path.resolve #168

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions baselines/requireJquery.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare namespace requireJquery {
function jqeury(w: any): any;

}

4 changes: 4 additions & 0 deletions baselines/simple.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare const simple: {
test: number;
};

3 changes: 1 addition & 2 deletions lib/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"lodash": "latest",
"mocha": "^3.0.2",
"react": "latest",
"shx": "^0.3.3",
"tslint": "^5.1.0",
"webpack": "^1.13.1"
},
Expand All @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/require-jquery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const jqeury = require("jquery")

exports.jqeury = jqeury;
1 change: 1 addition & 0 deletions tests/fixtures/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.test = 1
14 changes: 14 additions & 0 deletions tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') : '<none>';
Expand All @@ -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);
});
}
});