New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rust/cargo: more comprehensive support #1292
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Chawye Hsu <[email protected]>
|
||
Object.entries(json.dependencies).forEach(([name, version]) => { | ||
Object.entries(entries).forEach(([name, value]) => { | ||
const version = typeof value === "string" ? value : value.version; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regex being used (tomlRegExKeyValue
) doesn't seem to work with object values like this. I had to add another regex that handled object values like this:
export function tomlRegExObjectKeyValue(key, value) {
const regexKey = escapeRegexString(key);
const regexValue = escapeRegexString(value);
return new RegExp(`(${regexKey})\\s*=\\s*{.*=\\s*"(${regexValue})"`, 'g');
}
And then call this based on if value
is a string or object.
This is the full change:
@ -1,21 +1,27 @@
import * as TOML from '@iarna/toml';
import insertLink from '@octolinker/helper-insert-link';
-import { tomlRegExKeyValue } from '@octolinker/helper-regex-builder';
+import {
+ tomlRegExKeyValue,
+ tomlRegExObjectKeyValue,
+} from '@octolinker/helper-regex-builder';
import liveResolverQuery from '@octolinker/resolver-live-query';
-function linkDependency(blob, key, value) {
- const regex = tomlRegExKeyValue(key, value);
+function linkDependency(blob, key, value, isStringMatch) {
+ const regex = isStringMatch
+ ? tomlRegExKeyValue(key, value)
+ : tomlRegExObjectKeyValue(key, value);
return insertLink(blob, regex, this);
}
function processTOML(blob, plugin) {
let results = [];
const json = TOML.parse(blob.toString());
const entries = Object.assign(json['dependencies'], json['dev-dependencies']);
Object.entries(entries).forEach(([name, value]) => {
- const version = typeof value === "string" ? value : value.version;
- const linked = linkDependency.call(plugin, blob, name, version);
+ const isString = typeof value === 'string';
+ const version = isString ? value : value.version;
+ const linked = linkDependency.call(plugin, blob, name, version, isString);
results = results.concat(linked);
});
@ @@
@stefanbuck thoughts on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about passing the isStringMatch
to tomlRegExKeyValue
instead of creating a new regex function.
export function tomlRegExKeyValue(key, value, isString) {
const regexKey = escapeRegexString(key);
const regexValue = escapeRegexString(value);
const reStr = isString ? `(${regexKey})\\s*=\\s*"(${regexValue})"`
: `(${regexKey})\\s*=\\s*{.*=\\s*"(${regexValue})"`;
return new RegExp(reStr, 'g');
}
function linkDependency(blob, key, value, isString) {
const regex = tomlRegExKeyValue(key, value, isString);
return insertLink(blob, regex, this);
}
function processTOML(blob, plugin) {
let results = [];
const json = TOML.parse(blob.toString());
const entries = Object.assign(json['dependencies'], json['dev-dependencies']);
Object.entries(entries).forEach(([name, value]) => {
const isString = typeof value === "string";
const version = isString ? value : value.version;
const linked = linkDependency.call(plugin, blob, name, version, isString);
results = results.concat(linked);
});
return results;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late response. I think both are valid approaches and at the end it doesn't really matter. I don't see this toml regex being used in other places than plugin-rust-cargo/index.js
. Unless @xt0rted disagrees I think we can go ahead with the single function suggestions from @chawyehsu
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer the single function, thoughts? @xt0rted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer two functions since it's clearer what's goin on, but either way is fine with me.
Signed-off-by: Chawye Hsu <[email protected]>
I'm planning to make rust's/cargo's support more comprehensive, including std: https://doc.rust-lang.org/std/ use std::{fs, io};
use std::path::Path; relative mods
tokio-stream = { version = "0.1", path = "../tokio-stream" }
no version number [dependencies]
rand = { git = "https://github.com/rust-lang-nursery/rand" } Details can be found in https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html besides, OctoLinker doesn't support the following format currently: use {
minimad::{TextTemplate, TextTemplateExpander},
}; |
I created a repo for testing the linker's effect on rust/cargo: https://github.com/chawyehsu/octolinker-rust-test-cases |
Checklist: