Skip to content

Commit

Permalink
feat: support __dirname & __filename #18
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Apr 19, 2019
1 parent fe3fd00 commit 100047e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ $ddd = array( "a" => strlen($str), "b" => strlen($str) + 1 );
- parseFloat
- encodeURIComponent
- decodeURIComponent
- __dirname
- __filename
- Date
- Date.now
- Date.prototype.getTime
Expand Down
12 changes: 12 additions & 0 deletions src/features/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const map = {
decodeURIComponent: method('rawurldecode', false, 1)
};

const identifierMap = new Map([
['__dirname', 'dirname(__FILE__)'],
['__filename', '__FILE__']
]);

export default {

emit(hint, node, {helpers}) {
Expand Down Expand Up @@ -46,6 +51,13 @@ export default {
}
}

if (isIdentifier(node)) {
const text = (node.escapedText || node.getText()) as string;
if (identifierMap.has(text)) {
return helpers.writePunctuation(identifierMap.get(text));
}
}

return false;
}
};
2 changes: 2 additions & 0 deletions test/features/GlobalApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
$e = array( "a" => 1, "b" => 2 );
unset($e["a"]);
$f = $_SERVER["HTTP_USER_AGENT"];
$g = dirname(__FILE__);
$h = __FILE__;
2 changes: 2 additions & 0 deletions test/features/GlobalApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ const d = typeof c === 'string';
const e = {a: 1, b: 2};
delete e.a;
const f = navigator.userAgent;
const g = __dirname;
const h = __filename;
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('features', () => {

for (let i = 0; i < featureNames.length; i++) {
const featureName = featureNames[i];
// if (featureName !== 'Class') {
// if (featureName !== 'Destructuring') {
// continue;
// }
it(featureName, async function () {
Expand Down

0 comments on commit 100047e

Please sign in to comment.