Skip to content

Commit

Permalink
Fix clippy and adjust resolution algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Dec 19, 2022
1 parent 42446ba commit 645a871
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 83 deletions.
18 changes: 9 additions & 9 deletions boa_ast/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Boa's **`boa_ast`** crate implements an ECMAScript abstract syntax tree.
//!
//! # Crate Overview
//! **boa_ast** contains representations of [**Parse Nodes**][grammar] as defined by the ECMAScript
//! **`boa_ast`** contains representations of [**Parse Nodes**][grammar] as defined by the ECMAScript
//! spec. Some `Parse Node`s are not represented by Boa's AST, because a lot of grammar productions
//! are only used to throw [**Early Errors**][early], and don't influence the evaluation of the AST
//! itself.
Expand All @@ -17,14 +17,14 @@
//! Try out the most recent release with Boa's live demo [playground][boa-playground].
//!
//! # Boa Crates
//! - **boa_ast** - Boa's ECMAScript Abstract Syntax Tree.
//! - **boa_engine** - Boa's implementation of ECMAScript builtin objects and execution.
//! - **boa_gc** - Boa's garbage collector.
//! - **boa_interner** - Boa's string interner.
//! - **boa_parser** - Boa's lexer and parser.
//! - **boa_profiler** - Boa's code profiler.
//! - **boa_unicode** - Boa's Unicode identifier.
//! - **boa_icu_provider** - Boa's ICU4X data provider.
//! - **`boa_ast`** - Boa's ECMAScript Abstract Syntax Tree.
//! - **`boa_engine`** - Boa's implementation of ECMAScript builtin objects and execution.
//! - **`boa_gc`** - Boa's garbage collector.
//! - **`boa_interner`** - Boa's string interner.
//! - **`boa_parser`** - Boa's lexer and parser.
//! - **`boa_profiler`** - Boa's code profiler.
//! - **`boa_unicode`** - Boa's Unicode identifier.
//! - **`boa_icu_provider`** - Boa's ICU4X data provider.
//!
//! [grammar]: https://tc39.es/ecma262/#sec-syntactic-grammar
//! [early]: https://tc39.es/ecma262/#sec-static-semantic-rules
Expand Down
50 changes: 32 additions & 18 deletions boa_engine/src/builtins/intl/collator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,16 @@ where
.collation
.take()
.filter(|co| validate_extension(locale.id.clone(), key!("co"), co, provider))
.or_else(|| locale.extensions.unicode.keywords.get(&key!("co")).cloned())
.filter(|co| validate_extension(locale.id.clone(), key!("co"), co, provider));
.or_else(|| {
locale
.extensions
.unicode
.keywords
.get(&key!("co"))
.cloned()
.filter(|co| validate_extension(locale.id.clone(), key!("co"), co, provider))
})
.filter(|co| co != &value!("search"));

let numeric =
options.numeric.or_else(
Expand Down Expand Up @@ -251,9 +259,22 @@ impl Collator {

// 18. Let relevantExtensionKeys be %Collator%.[[RelevantExtensionKeys]].
// 19. Let r be ResolveLocale(%Collator%.[[AvailableLocales]], requestedLocales, opt, relevantExtensionKeys, localeData).
let locale =
let mut locale =
resolve_locale::<Self, _>(&requested_locales, &mut intl_options, context.icu());

let collator_locale = {
// `collator_locale` needs to be different from the resolved locale because ECMA402 doesn't
// define `search` as a resolvable extension of a locale, so we need to add that extension
// only to the locale passed to the collator.
let mut col_loc = DataLocale::from(&locale);
if usage == Usage::Search {
intl_options.service_options.collation = None;
locale.extensions.unicode.keywords.remove(key!("co"));
col_loc.set_unicode_ext(key!("co"), value!("search"));
}
col_loc
};

// 20. Set collator.[[Locale]] to r.[[locale]].

// 21. Let collation be r.[[co]].
Expand Down Expand Up @@ -282,32 +303,25 @@ impl Collator {
// i. Let dataLocale be r.[[dataLocale]].
// ii. Let dataLocaleData be localeData.[[<dataLocale>]].
// iii. Let sensitivity be dataLocaleData.[[sensitivity]].
// For now, `Collator` always uses `Strength::Tertiary` as the default.
.unwrap_or(Sensitivity::Variant);
.or_else(|| (usage == Usage::Sort).then_some(Sensitivity::Variant));

// 29. Let ignorePunctuation be ? GetOption(options, "ignorePunctuation", boolean, empty, false).
// 30. Set collator.[[IgnorePunctuation]] to ignorePunctuation.
let ignore_punctuation =
get_option::<bool>(&options, "ignorePunctuation", false, context)?.unwrap_or_default();

let (strength, case_level) = sensitivity.to_collator_options();
let (strength, case_level) = sensitivity.map(Sensitivity::to_collator_options).unzip();

// TODO: change to use `unzip` when 1.66 releases.
let (alternate_handling, max_variable) = if ignore_punctuation {
(
Some(AlternateHandling::Shifted),
Some(MaxVariable::Punctuation),
)
} else {
(None, None)
};
let (alternate_handling, max_variable) = ignore_punctuation
.then_some((AlternateHandling::Shifted, MaxVariable::Punctuation))
.unzip();

let collator = context
.icu()
.provider()
.try_new_collator(&DataLocale::from(&locale), {
.try_new_collator(&collator_locale, {
let mut options = icu_collator::CollatorOptions::new();
options.strength = Some(strength);
options.strength = strength;
options.case_level = case_level;
options.case_first = case_first;
options.numeric = Some(if numeric { Numeric::On } else { Numeric::Off });
Expand All @@ -327,7 +341,7 @@ impl Collator {
numeric,
case_first,
usage,
sensitivity,
sensitivity: sensitivity.unwrap_or(Sensitivity::Variant),
ignore_punctuation,
collator,
bound_compare: None,
Expand Down
10 changes: 5 additions & 5 deletions boa_engine/src/builtins/intl/collator/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ pub(crate) enum Sensitivity {

impl Sensitivity {
/// Converts the sensitivity option to the equivalent ICU4X collator options.
pub(crate) const fn to_collator_options(self) -> (Strength, Option<CaseLevel>) {
pub(crate) const fn to_collator_options(self) -> (Strength, CaseLevel) {
match self {
Sensitivity::Base => (Strength::Primary, None),
Sensitivity::Accent => (Strength::Secondary, None),
Sensitivity::Case => (Strength::Primary, Some(CaseLevel::On)),
Sensitivity::Variant => (Strength::Tertiary, None),
Sensitivity::Base => (Strength::Primary, CaseLevel::Off),
Sensitivity::Accent => (Strength::Secondary, CaseLevel::Off),
Sensitivity::Case => (Strength::Primary, CaseLevel::On),
Sensitivity::Variant => (Strength::Tertiary, CaseLevel::On),
}
}
}
Expand Down
18 changes: 16 additions & 2 deletions boa_engine/src/builtins/intl/locale/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
Context, JsNativeError, JsResult, JsValue,
};

use icu_collator::provider::CollationMetadataV1Marker;
use icu_locid::{
extensions::unicode::{Key, Value},
subtags::Variants,
Expand Down Expand Up @@ -178,7 +179,13 @@ pub(crate) fn best_available_locale<M: KeyedDataMarker>(
// the fallback algorithm, even if the used locale is exactly the same as the required
// locale.
match req.metadata.locale {
Some(loc) if loc == candidate => return Some(candidate.into_locale().id),
Some(loc)
if loc == candidate
// TODO: ugly hack to accept locales that fallback to "und" in the collator service
|| (loc.is_empty() && M::KEY.path() == CollationMetadataV1Marker::KEY.path()) =>
{
return Some(candidate.into_locale().id)
}
None => return Some(candidate.into_locale().id),
_ => {}
}
Expand Down Expand Up @@ -233,7 +240,14 @@ pub(crate) fn best_locale_for_provider<M: KeyedDataMarker>(
response
.metadata
.locale
.map(|dl| dl.into_locale().id)
.map(|dl| {
// TODO: ugly hack to accept locales that fallback to "und" in the collator service
if M::KEY.path() == CollationMetadataV1Marker::KEY.path() && dl.is_empty() {
candidate.clone()
} else {
dl.into_locale().id
}
})
.or(Some(candidate))
.filter(|loc| loc != &LanguageIdentifier::UND)
}
Expand Down
16 changes: 8 additions & 8 deletions boa_engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
//! Try out the most recent release with Boa's live demo [playground][boa-playground].
//!
//! # Boa Crates
//! - **boa_ast** - Boa's ECMAScript Abstract Syntax Tree.
//! - **boa_engine** - Boa's implementation of ECMAScript builtin objects and execution.
//! - **boa_gc** - Boa's garbage collector.
//! - **boa_interner** - Boa's string interner.
//! - **boa_parser** - Boa's lexer and parser.
//! - **boa_profiler** - Boa's code profiler.
//! - **boa_unicode** - Boa's Unicode identifier.
//! - **boa_icu_provider** - Boa's ICU4X data provider.
//! - **`boa_ast`** - Boa's ECMAScript Abstract Syntax Tree.
//! - **`boa_engine`** - Boa's implementation of ECMAScript builtin objects and execution.
//! - **`boa_gc`** - Boa's garbage collector.
//! - **`boa_interner`** - Boa's string interner.
//! - **`boa_parser`** - Boa's lexer and parser.
//! - **`boa_profiler`** - Boa's code profiler.
//! - **`boa_unicode`** - Boa's Unicode identifier.
//! - **`boa_icu_provider`** - Boa's ICU4X data provider.
//!
//! [whatwg]: https://console.spec.whatwg.org
//! [ecma-402]: https://tc39.es/ecma402
Expand Down
18 changes: 9 additions & 9 deletions boa_gc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Boa's **`boa_gc`** crate implements a garbage collector.
//!
//! # Crate Overview
//! **boa_gc** is a mark-sweep garbage collector that implements a Trace and Finalize trait
//! **`boa_gc`** is a mark-sweep garbage collector that implements a Trace and Finalize trait
//! for garbage collected values.
//!
//! # About Boa
Expand All @@ -11,14 +11,14 @@
//! Try out the most recent release with Boa's live demo [playground][boa-playground].
//!
//! # Boa Crates
//! - **boa_ast** - Boa's ECMAScript Abstract Syntax Tree.
//! - **boa_engine** - Boa's implementation of ECMAScript builtin objects and execution.
//! - **boa_gc** - Boa's garbage collector.
//! - **boa_interner** - Boa's string interner.
//! - **boa_parser** - Boa's lexer and parser.
//! - **boa_profiler** - Boa's code profiler.
//! - **boa_unicode** - Boa's Unicode identifier.
//! - **boa_icu_provider** - Boa's ICU4X data provider.
//! - **`boa_ast`** - Boa's ECMAScript Abstract Syntax Tree.
//! - **`boa_engine`** - Boa's implementation of ECMAScript builtin objects and execution.
//! - **`boa_gc`** - Boa's garbage collector.
//! - **`boa_interner`** - Boa's string interner.
//! - **`boa_parser`** - Boa's lexer and parser.
//! - **`boa_profiler`** - Boa's code profiler.
//! - **`boa_unicode`** - Boa's Unicode identifier.
//! - **`boa_icu_provider`** - Boa's ICU4X data provider.
//!
//! [boa-conformance]: https://boa-dev.github.io/boa/test262/
//! [boa-web]: https://boa-dev.github.io/
Expand Down
16 changes: 8 additions & 8 deletions boa_interner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
//! Try out the most recent release with Boa's live demo [playground][boa-playground].
//!
//! # Boa Crates
//! - **boa_ast** - Boa's ECMAScript Abstract Syntax Tree.
//! - **boa_engine** - Boa's implementation of ECMAScript builtin objects and execution.
//! - **boa_gc** - Boa's garbage collector.
//! - **boa_interner** - Boa's string interner.
//! - **boa_parser** - Boa's lexer and parser.
//! - **boa_profiler** - Boa's code profiler.
//! - **boa_unicode** - Boa's Unicode identifier.
//! - **boa_icu_provider** - Boa's ICU4X data provider.
//! - **`boa_ast`** - Boa's ECMAScript Abstract Syntax Tree.
//! - **`boa_engine`** - Boa's implementation of ECMAScript builtin objects and execution.
//! - **`boa_gc`** - Boa's garbage collector.
//! - **`boa_interner`** - Boa's string interner.
//! - **`boa_parser`** - Boa's lexer and parser.
//! - **`boa_profiler`** - Boa's code profiler.
//! - **`boa_unicode`** - Boa's Unicode identifier.
//! - **`boa_icu_provider`** - Boa's ICU4X data provider.
//!
//! [boa-conformance]: https://boa-dev.github.io/boa/test262/
//! [boa-web]: https://boa-dev.github.io/
Expand Down
16 changes: 8 additions & 8 deletions boa_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
//! Try out the most recent release with Boa's live demo [playground][boa-playground].
//!
//! # Boa Crates
//! - **boa_ast** - Boa's ECMAScript Abstract Syntax Tree.
//! - **boa_engine** - Boa's implementation of ECMAScript builtin objects and execution.
//! - **boa_gc** - Boa's garbage collector.
//! - **boa_interner** - Boa's string interner.
//! - **boa_parser** - Boa's lexer and parser.
//! - **boa_profiler** - Boa's code profiler.
//! - **boa_unicode** - Boa's Unicode identifier.
//! - **boa_icu_provider** - Boa's ICU4X data provider.
//! - **`boa_ast`** - Boa's ECMAScript Abstract Syntax Tree.
//! - **`boa_engine`** - Boa's implementation of ECMAScript builtin objects and execution.
//! - **`boa_gc`** - Boa's garbage collector.
//! - **`boa_interner`** - Boa's string interner.
//! - **`boa_parser`** - Boa's lexer and parser.
//! - **`boa_profiler`** - Boa's code profiler.
//! - **`boa_unicode`** - Boa's Unicode identifier.
//! - **`boa_icu_provider`** - Boa's ICU4X data provider.
//!
//! [spec]: https://tc39.es/ecma262
//! [lex]: https://tc39.es/ecma262/#sec-ecmascript-language-lexical-grammar
Expand Down
16 changes: 8 additions & 8 deletions boa_profiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
//! Try out the most recent release with Boa's live demo [playground][boa-playground].
//!
//! # Boa Crates
//! - **boa_ast** - Boa's ECMAScript Abstract Syntax Tree.
//! - **boa_engine** - Boa's implementation of ECMAScript builtin objects and execution.
//! - **boa_gc** - Boa's garbage collector.
//! - **boa_interner** - Boa's string interner.
//! - **boa_parser** - Boa's lexer and parser.
//! - **boa_profiler** - Boa's code profiler.
//! - **boa_unicode** - Boa's Unicode identifier.
//! - **boa_icu_provider** - Boa's ICU4X data provider.
//! - **`boa_ast`** - Boa's ECMAScript Abstract Syntax Tree.
//! - **`boa_engine`** - Boa's implementation of ECMAScript builtin objects and execution.
//! - **`boa_gc`** - Boa's garbage collector.
//! - **`boa_interner`** - Boa's string interner.
//! - **`boa_parser`** - Boa's lexer and parser.
//! - **`boa_profiler`** - Boa's code profiler.
//! - **`boa_unicode`** - Boa's Unicode identifier.
//! - **`boa_icu_provider`** - Boa's ICU4X data provider.
//!
//! [profiler-md]: https://github.com/boa-dev/boa/blob/main/docs/profiling.md
//! [boa-conformance]: https://boa-dev.github.io/boa/test262/
Expand Down
16 changes: 8 additions & 8 deletions boa_unicode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
//! Try out the most recent release with Boa's live demo [playground][boa-playground].
//!
//! # Boa Crates
//! - **boa_ast** - Boa's ECMAScript Abstract Syntax Tree.
//! - **boa_engine** - Boa's implementation of ECMAScript builtin objects and execution.
//! - **boa_gc** - Boa's garbage collector.
//! - **boa_interner** - Boa's string interner.
//! - **boa_parser** - Boa's lexer and parser.
//! - **boa_profiler** - Boa's code profiler.
//! - **boa_unicode** - Boa's Unicode identifier.
//! - **boa_icu_provider** - Boa's ICU4X data provider.
//! - **`boa_ast`** - Boa's ECMAScript Abstract Syntax Tree.
//! - **`boa_engine`** - Boa's implementation of ECMAScript builtin objects and execution.
//! - **`boa_gc`** - Boa's garbage collector.
//! - **`boa_interner`** - Boa's string interner.
//! - **`boa_parser`** - Boa's lexer and parser.
//! - **`boa_profiler`** - Boa's code profiler.
//! - **`boa_unicode`** - Boa's Unicode identifier.
//! - **`boa_icu_provider`** - Boa's ICU4X data provider.
//!
//! [uax31]: http://unicode.org/reports/tr31
//! [boa-conformance]: https://boa-dev.github.io/boa/test262/
Expand Down

0 comments on commit 645a871

Please sign in to comment.