Skip to content

Commit

Permalink
Refactor helper-insert-link package (OctoLinker#436)
Browse files Browse the repository at this point in the history
* Remove data attribute from link

* Prepare tests for insert-link return value

* Implement return value

* Add data-attribute on link element

* Fix json plugins
  • Loading branch information
stefanbuck authored Jan 31, 2018
1 parent e490c86 commit 48440ac
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 124 deletions.
27 changes: 21 additions & 6 deletions packages/core/octo-linker.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,24 @@ function initialize(self) {
clickHandler(self._pluginManager);
}

function insertDataAttr(matches) {
matches.forEach(({ data, link }) => {
for (const key in data) {
if (data.hasOwnProperty(key)) {
link.dataset[key] = data[key];
}
}
});
}

function run(self) {
if (!self._blobReader.hasBlobs()) {
return false;
}

self._blobReader.read();

let matches = [];
self._blobReader.forEach(blob => {
const plugins = self._pluginManager.get(blob.path, blob.el.classList);

Expand All @@ -33,17 +44,21 @@ function run(self) {

plugins.forEach(plugin => {
if (plugin.parseBlob) {
plugin.parseBlob(blob);
matches = matches.concat(plugin.parseBlob(blob));
} else if (plugin.getLinkRegexes) {
[].concat(plugin.getLinkRegexes(blob)).forEach(regex => {
insertLink(blob.el, regex, {
pluginName: plugin.name,
target: '$1',
path: blob.path,
});
matches = matches.concat(
insertLink(blob.el, regex, {
pluginName: plugin.name,
target: '$1',
path: blob.path,
}),
);
});
}
});

insertDataAttr(matches);
});
}

Expand Down
6 changes: 3 additions & 3 deletions packages/core/plugins/bower-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function linkDependency(blob, key, value) {
const isValidSemver = isSemver(value);
const regex = jsonRegExKeyValue(key, value);

insertLink(blob.el, regex, {
return insertLink(blob.el, regex, {
pluginName: 'BowerManifest',
target: isValidSemver ? '$1' : '$2',
type: isValidSemver ? 'liveResolverQuery' : 'git',
Expand All @@ -21,7 +21,7 @@ function linkDependency(blob, key, value) {
function linkFile(blob, key, value) {
const regex = jsonRegExValue(key, value);

insertLink(blob.el, regex, {
return insertLink(blob.el, regex, {
pluginName: 'BowerManifest',
type: 'file',
path: blob.path,
Expand Down Expand Up @@ -52,7 +52,7 @@ export default {
},

parseBlob(blob) {
processJSON(blob, {
return processJSON(blob, {
'$.dependencies': linkDependency,
'$.devDependencies': linkDependency,
'$.resolutions': linkDependency,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugins/composer-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function linkDependency(blob, key, value) {

const regex = jsonRegExKeyValue(key, value);

insertLink(blob.el, regex, {
return insertLink(blob.el, regex, {
pluginName: 'Composer',
target: '$1',
});
Expand All @@ -31,7 +31,7 @@ export default {
},

parseBlob(blob) {
processJSON(blob, {
return processJSON(blob, {
'$.require': linkDependency,
'$.require-dev': linkDependency,
'$.conflict': linkDependency,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugins/dot-net-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import nugetResolver from '../resolver/nuget';
function linkDependency(blob, key, value) {
const regex = jsonRegExKeyValue(key, value);

insertLink(blob.el, regex, {
return insertLink(blob.el, regex, {
pluginName: 'DotNetCore',
target: '$1',
});
Expand All @@ -27,7 +27,7 @@ export default {
},

parseBlob(blob) {
processJSON(blob, {
return processJSON(blob, {
'$.dependencies': linkDependency,
'$.tools': linkDependency,
'$.frameworks.*.dependencies': linkDependency,
Expand Down
11 changes: 9 additions & 2 deletions packages/core/plugins/helper/process-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ import jsonPath from 'JSONPath';
export default function(blob, config) {
const json = blob.toJSON();

let results = [];

Object.entries(config).forEach(([path, linker]) => {
jsonPath({ json, path, resultType: 'all' }).forEach(result => {
if (typeof result.value === 'string') {
return linker(blob, result.parentProperty, result.value);
results = results.concat(
linker(blob, result.parentProperty, result.value),
);
return;
}

for (const [key, value] of Object.entries(result.value)) {
linker(blob, key, value);
results = results.concat(linker(blob, key, value));
}
});
});

return results;
}
6 changes: 3 additions & 3 deletions packages/core/plugins/npm-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function linkDependency(blob, key, value) {
const isValidSemver = isSemver(value);
const regex = jsonRegExKeyValue(key, value);

insertLink(blob.el, regex, {
return insertLink(blob.el, regex, {
pluginName: 'NpmManifest',
target: isValidSemver ? '$1' : '$2',
type: isValidSemver ? 'liveResolverQuery' : 'git',
Expand All @@ -21,7 +21,7 @@ function linkDependency(blob, key, value) {
function linkFile(blob, key, value) {
const regex = jsonRegExValue(key, value);

insertLink(blob.el, regex, {
return insertLink(blob.el, regex, {
pluginName: 'NpmManifest',
type: 'file',
path: blob.path,
Expand Down Expand Up @@ -52,7 +52,7 @@ export default {
},

parseBlob(blob) {
processJSON(blob, {
return processJSON(blob, {
'$.dependencies': linkDependency,
'$.devDependencies': linkDependency,
'$.peerDependencies': linkDependency,
Expand Down
58 changes: 30 additions & 28 deletions packages/helper-insert-link/__tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`insert-link adds the given data-* attributes 1`] = `
Object {
"bar": "baz",
"value": "foo",
}
`;

exports[`insert-link can handle if value is empty string 1`] = `
Object {
"value": "",
}
`;

exports[`insert-link can handle if value is null 1`] = `
Object {
"value": "null",
}
`;

exports[`insert-link can handle if value is undefined 1`] = `
Object {
"value": "undefined",
}
`;

exports[`insert-link does not remove closing parentheses from commented out require() calls 1`] = `
<div>
// var faker =
Expand All @@ -43,12 +18,41 @@ exports[`insert-link does not remove closing parentheses from commented out requ
</div>
`;

exports[`insert-link replace placeholder with capture group value 1`] = `
exports[`insert-link returns adds the given data-* attributes 1`] = `
Object {
"bar": "baz",
"value": "foo",
}
`;

exports[`insert-link returns replace placeholder with capture group value 1`] = `
Object {
"value": "go/foo.txt",
}
`;

exports[`insert-link returns returns an array 1`] = `
Array [
Object {
"data": Object {},
"link": <a
class="octolinker-link"
>
<span>
bar
</span>
</a>,
},
]
`;

exports[`insert-link returns wraps the second regex match 1`] = `
Object {
"value": "baz",
"xx": "yy",
}
`;

exports[`insert-link wraps a nested element 1`] = `
<div>
Expand Down Expand Up @@ -279,8 +283,6 @@ exports[`insert-link wraps the second regex match 1`] = `
</i>
<a
class="octolinker-link"
data-value="baz"
data-xx="yy"
>
<span>
baz
Expand Down
Loading

0 comments on commit 48440ac

Please sign in to comment.