Skip to content

Commit

Permalink
Ruby require_relative fixes (OctoLinker#1014)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt <[email protected]>
  • Loading branch information
MatthewDG and Matt authored Sep 1, 2020
1 parent 55ea9c8 commit 710f36e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
3 changes: 3 additions & 0 deletions e2e/fixtures/ruby/mystic-wind.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# @OctoLinkerResolve(https://github.com/rack/rack)
require "rack"

# @OctoLinkerResolve(<root>/ruby/relative/foo.rb)
require_relative "relative/foo"
Empty file.
8 changes: 7 additions & 1 deletion packages/helper-grammar-regex-collection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ export const NODEJS_RELATIVE_PATH_JOIN = regex`
`;

export const REQUIRE = regex`
( require(\.resolve)? | proxyquire | import | require_relative )
( require(\.resolve)? | proxyquire | import )
\s* ( \s | \( ) \s*
${captureJsQuotedWord}
`;

export const REQUIRE_RELATIVE = regex`
( require_relative )
\s* ( \s | \( ) \s*
${captureJsQuotedWord}
`;
Expand Down
13 changes: 12 additions & 1 deletion packages/helper-grammar-regex-collection/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ const fixtures = {
['./foo', './bar'],
],
['require "foo"', ['foo']],
['require_relative "foo"', ['foo']],
// require.resolve
['require.resolve "foo"', ['foo']],
['require.resolve("foo")', ['foo']],
Expand Down Expand Up @@ -174,6 +173,18 @@ const fixtures = {
'var foo = import.resolve("foo")var bar = import.resolve("bar")',
],
},
REQUIRE_RELATIVE: {
valid: [
['require_relative "foo"', ['foo']],
['require_relative "channel_prefix"', ['channel_prefix']],
],
invalid: [
'require_relative(foo)',
'require_relative"foo"',
'require_relative (foo)',
'require_relative("fo o")',
],
},
GEM: {
valid: [
['gem "foo"', ['foo']],
Expand Down
2 changes: 1 addition & 1 deletion packages/helper-insert-link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default function (blob, regex, plugin, meta = {}) {
endPosInBlob,
values,
}) => {
let urls = plugin.resolve(blob.path, values, meta);
let urls = plugin.resolve(blob.path, values, meta, regex);
if (Array.isArray(urls)) {
urls = urls.filter(Boolean);
}
Expand Down
27 changes: 21 additions & 6 deletions packages/plugin-ruby/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import { join } from 'path';
import { REQUIRE } from '@octolinker/helper-grammar-regex-collection';
import {
REQUIRE,
REQUIRE_RELATIVE,
} from '@octolinker/helper-grammar-regex-collection';
import liveResolverQuery from '@octolinker/resolver-live-query';

export default {
name: 'Ruby',

resolve(path, [target]) {
resolve(path, [target], meta, regex) {
const isPath = !!target.match(/\//);

// https://github.com/github/pages-gem/blob/master/lib/github-pages/dependencies.rb

if (isPath) {
const basePath = join(path.split('/lib/')[0], 'lib');
const isRequireRelative = regex.toString() === REQUIRE_RELATIVE.toString();

if (isRequireRelative) {
let resolvedPath = path;
const splitPath = path.split('/');
const currentFile = splitPath[splitPath.length - 1];
resolvedPath = resolvedPath.replace(currentFile, `${target}.rb`);
return `{BASE_URL}${resolvedPath}`;
}

if (isPath) {
let splitPath = path.split('/lib/');
if (splitPath.length < 2) {
splitPath = path.split('/test/');
}
const basePath = join(splitPath[0], 'lib');
return `{BASE_URL}${join(basePath, `${target}.rb`)}`;
}

Expand All @@ -27,6 +42,6 @@ export default {
},

getLinkRegexes() {
return REQUIRE;
return [REQUIRE, REQUIRE_RELATIVE];
},
};

0 comments on commit 710f36e

Please sign in to comment.