Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

chawyehsu
Copy link

Checklist:

  • If this PR is a new feature, please provide at least one example link
  • Make sure all of the significant new logic is covered by tests


Object.entries(json.dependencies).forEach(([name, version]) => {
Object.entries(entries).forEach(([name, value]) => {
const version = typeof value === "string" ? value : value.version;
Copy link
Member

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?

Copy link
Author

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;
}

Copy link
Member

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

Copy link
Author

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

Copy link
Member

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]>
@chawyehsu
Copy link
Author

chawyehsu commented May 14, 2021

I'm planning to make rust's/cargo's support more comprehensive, including std and relative modules. The question is I don't quite understand how to handle relative modules, and dependencies without specifying the version.

std: https://doc.rust-lang.org/std/

use std::{fs, io};
use std::path::Path;

relative mods

  1. in Cargo.toml
    path = "../tokio-stream" to use relative crate as dependencey, documented in https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-path-dependencies
tokio-stream = { version = "0.1", path = "../tokio-stream" }
  1. in .rs source file
    crate:: to import mods from the current crate root
use crate::io;
use crate::path::Path;

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},
};

@chawyehsu chawyehsu changed the title Cargo: Support TOML inline table and cargo dev-dependencies rust/cargo: more comprehensive support May 14, 2021
@chawyehsu
Copy link
Author

I created a repo for testing the linker's effect on rust/cargo: https://github.com/chawyehsu/octolinker-rust-test-cases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants