Skip to content

Commit

Permalink
Resolve __dirname + '/some/relative/path' (OctoLinker#364)
Browse files Browse the repository at this point in the history
* Resolve `__dirname + '/some/relative/path'`

Fixes OctoLinker#359

Demos:
* https://github.com/pegjs/website/blob/5924ede36795192b77f2cb9bfe25a56e8e8b6f90/app.js#L12
* https://github.com/antoninlanglade/totopack/blob/055a725660a11d9c765434198edc7bb538ec5ccf/config/paths.js#L6

* Document Node.js relative path support

* Dedupe JS/TS patterns/classes in relative path resolver

* Match literal dot in `path.join`
  • Loading branch information
josephfrazier authored and stefanbuck committed Jun 4, 2017
1 parent cbb53a0 commit a3ec0f9
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ OctoLinker is the easiest and best way to navigate between files and projects on
- `require`
- `import`
- `export`
- `__dirname + '/some/relative/path'`
- `path.join(__dirname, '/some/relative/path')`

### package.json (npm)
- `main`
Expand Down
29 changes: 29 additions & 0 deletions lib/plugins/nodejs-relative-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { join, dirname } from 'path';
import { NODEJS_RELATIVE_PATH, NODEJS_RELATIVE_PATH_JOIN } from '../../packages/helper-grammar-regex-collection/index.js';
import JavaScript from './javascript';
import TypeScript from './typescript';

export default {
name: 'NodejsRelativePath',

resolve({ target, path }) {
return `{BASE_URL}${join(dirname(path), target)}`;
},

getPattern() {
const jsPatterns = JavaScript.getPattern().pathPatterns;
const tsPatterns = TypeScript.getPattern().pathPatterns;

const jsClasses = JavaScript.getPattern().githubClasses;
const tsClasses = TypeScript.getPattern().githubClasses;

return {
pathPatterns: jsPatterns.concat(tsPatterns),
githubClasses: jsClasses.concat(tsClasses),
};
},

getLinkRegexes() {
return [NODEJS_RELATIVE_PATH, NODEJS_RELATIVE_PATH_JOIN];
},
};
14 changes: 14 additions & 0 deletions packages/helper-grammar-regex-collection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ const captureQuotedWord = regex`
const importMembers = regex`[\r\n\s\w{},*\$]*`;
const from = regex`\s from \s`;

export const NODEJS_RELATIVE_PATH = regex`
__dirname
\s* \+ \s*
${captureQuotedWord}
`;

export const NODEJS_RELATIVE_PATH_JOIN = regex`
path\.join \s*
\( \s*
__dirname \s*
, \s*
${captureQuotedWord}
`;

export const REQUIRE = regex`
( require(\.resolve)? | proxyquire | import | require_relative )
\s* ( \s | \( ) \s*
Expand Down
16 changes: 16 additions & 0 deletions packages/helper-grammar-regex-collection/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ import assert from 'assert';
import * as REGEX from './index.js';

const fixtures = {
NODEJS_RELATIVE_PATH_JOIN: {
valid: [
['app.set("views", path.join(__dirname, "/views");', ['/views']],
],
invalid: [
'app.set("views", pathDjoin(__dirname, "/views");',
],
},
NODEJS_RELATIVE_PATH: {
valid: [
['app.set("views", __dirname + "/views");', ['/views']],
],
invalid: [
'app.set("views", path.join(__dirname, "/views");',
],
},
IMPORT: {
valid: [
'import foo from "foo"',
Expand Down
14 changes: 14 additions & 0 deletions test/plugins/nodejs-relative-path.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import assert from 'assert';
import plugin from '../../lib/plugins/nodejs-relative-path.js';

describe('nodejs-relative-path', () => {
it('resolves `__dirname + "/views"` relative to the current directory', () => {
assert.deepEqual(
plugin.resolve({
target: '/views',
path: '/pegjs/website/blob/5924ede36795192b77f2cb9bfe25a56e8e8b6f90/app.js#L12',
}),
'{BASE_URL}/pegjs/website/blob/5924ede36795192b77f2cb9bfe25a56e8e8b6f90/views',
);
});
});

0 comments on commit a3ec0f9

Please sign in to comment.