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

fix: update typesVersion field spec to match docs #3

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ pub struct PackageJson {
#[builder(default, setter(into, strip_option))]
pub version: Option<String>,

/// Version must be parseable by node-semver, which is bundled with npm as a
/// dependency.
/// Put a description in it. It's a string.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change this?

/// This helps people discover your package, as it's listed in npm search.
#[serde(default, skip_serializing_if = "Option::is_none")]
#[builder(default, setter(into, strip_option))]
pub description: Option<String>,
Expand Down Expand Up @@ -165,7 +165,7 @@ pub struct PackageJson {
skip_serializing_if = "Option::is_none"
)]
#[builder(default, setter(into, strip_option))]
pub types_versions: Option<IndexMap<String, String>>,
pub types_versions: Option<IndexMap<String, TypesVersion>>,

/// Specify either a single file or an array of filenames to put in place for
/// the man program to find.
Expand Down Expand Up @@ -395,6 +395,20 @@ pub enum Repository {
},
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum TypesVersion {
Path(String),
PathMapping(IndexMap<String, TypesVersionPaths>),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum TypesVersionPaths {
Path(String),
PathList(Vec<String>),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum Workspaces {
Expand Down
13 changes: 13 additions & 0 deletions tests/fixtures/5/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"types": "dist/index.d.ts",
"typesVersions": {
"*": {
"one": [
"dist/one/index.d.ts"
],
"two": [
"dist/two/index.d.ts"
]
}
}
}
22 changes: 22 additions & 0 deletions tests/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ fn parse_package_json_file_with_dependencies() {
"###);
}

#[test]
fn parse_packages_json_file_with_typescript_fields() {
let contents = read_to_string("./tests/fixtures/5/package.json").unwrap();
let package_json = PackageJson::try_from(contents).unwrap();

insta::assert_json_snapshot!(package_json, @r###"
{
"types": "dist/index.d.ts",
"typesVersions": {
"*": {
"one": [
"dist/one/index.d.ts"
],
"two": [
"dist/two/index.d.ts"
]
}
}
}
"###);
}

#[test]
fn create_package_json_file_with_builder_pattern() {
let mut additional_fields: AdditionalFields = IndexMap::new();
Expand Down
Loading