Skip to content

Commit

Permalink
fix: update typesVersion field spec to match docs (#3)
Browse files Browse the repository at this point in the history
Co-authored-by: Aryslan Yakshibaev <Aryslan.Yakshibaev@kaspersky.com>
  • Loading branch information
consoleaf and Aryslan Yakshibaev authored Sep 25, 2024
1 parent dd44cd1 commit 2ce6417
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
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.
/// 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

0 comments on commit 2ce6417

Please sign in to comment.