Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
feat(rome_service, rome_js_parser): add parse options (#4587)
Browse files Browse the repository at this point in the history
* feat(rome_service, rome_js_parser): add parse options

* chore: update all the API calls! Damn what a chore...

* fix: doc tests
  • Loading branch information
ematipico authored Jun 20, 2023
1 parent 2b76136 commit a34cdcc
Show file tree
Hide file tree
Showing 41 changed files with 349 additions and 126 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions crates/rome_formatter/src/comments/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ mod tests {
DecoratedComment, SourceComment,
};
use crate::{TextSize, TransformSourceMap, TransformSourceMapBuilder};
use rome_js_parser::parse_module;
use rome_js_parser::{parse_module, JsParserOptions};
use rome_js_syntax::{
JsIdentifierExpression, JsLanguage, JsParameters, JsParenthesizedExpression,
JsPropertyObjectMember, JsReferenceIdentifier, JsShorthandPropertyObjectMember,
Expand Down Expand Up @@ -850,7 +850,7 @@ b;"#;

let source_map = source_map_builder.finish();

let root = parse_module(source).syntax();
let root = parse_module(source, JsParserOptions::default()).syntax();

// A lot of code that simply removes the parenthesized expression and moves the parens
// trivia to the identifiers leading / trailing trivia.
Expand Down Expand Up @@ -1011,7 +1011,7 @@ b;"#;
Vec<DecoratedComment<JsLanguage>>,
CommentsMap<SyntaxElementKey, SourceComment<JsLanguage>>,
) {
let tree = parse_module(source);
let tree = parse_module(source, JsParserOptions::default());

let style = TestCommentStyle::default();
let builder = CommentsBuilderVisitor::new(&style, source_map);
Expand Down
16 changes: 12 additions & 4 deletions crates/rome_js_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ mod tests {
use rome_diagnostics::category;
use rome_diagnostics::termcolor::NoColor;
use rome_diagnostics::{Diagnostic, DiagnosticExt, PrintDiagnostic, Severity};
use rome_js_parser::parse;
use rome_js_parser::{parse, JsParserOptions};
use rome_js_syntax::{JsFileSource, TextRange, TextSize};
use std::slice;

Expand All @@ -186,7 +186,7 @@ mod tests {
}
}"#;

let parsed = parse(SOURCE, JsFileSource::tsx());
let parsed = parse(SOURCE, JsFileSource::tsx(), JsParserOptions::default());

let mut error_ranges: Vec<TextRange> = Vec::new();
let mut options = AnalyzerOptions::default();
Expand Down Expand Up @@ -283,7 +283,11 @@ mod tests {
}
";

let parsed = parse(SOURCE, JsFileSource::js_module());
let parsed = parse(
SOURCE,
JsFileSource::js_module(),
JsParserOptions::default(),
);

let mut lint_ranges: Vec<TextRange> = Vec::new();
let mut parse_ranges: Vec<TextRange> = Vec::new();
Expand Down Expand Up @@ -365,7 +369,11 @@ mod tests {
a == b;
";

let parsed = parse(SOURCE, JsFileSource::js_module());
let parsed = parse(
SOURCE,
JsFileSource::js_module(),
JsParserOptions::default(),
);

let filter = AnalysisFilter {
categories: RuleCategories::SYNTAX,
Expand Down
7 changes: 6 additions & 1 deletion crates/rome_js_analyze/src/react/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,16 @@ pub fn is_binding_react_stable(
#[cfg(test)]
mod test {
use super::*;
use rome_js_parser::JsParserOptions;
use rome_js_syntax::JsFileSource;

#[test]
pub fn ok_react_stable_captures() {
let r = rome_js_parser::parse("const ref = useRef();", JsFileSource::js_module());
let r = rome_js_parser::parse(
"const ref = useRef();",
JsFileSource::js_module(),
JsParserOptions::default(),
);
let node = r
.syntax()
.descendants()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,14 @@ fn get_boolean_value(node: AnyJsLiteralExpression) -> bool {

#[cfg(test)]
mod tests {
use rome_js_parser::JsParserOptions;
use rome_js_syntax::{AnyJsLiteralExpression, JsFileSource};
use rome_rowan::SyntaxNodeCast;

use super::get_boolean_value;

fn assert_boolean_value(code: &str, value: bool) {
let source = rome_js_parser::parse(code, JsFileSource::tsx());
let source = rome_js_parser::parse(code, JsFileSource::tsx(), JsParserOptions::default());

if source.has_errors() {
panic!("syntax error")
Expand Down
25 changes: 21 additions & 4 deletions crates/rome_js_analyze/src/utils/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::rename::*;
use crate::utils::batch::JsBatchMutation;
use rome_js_parser::JsParserOptions;
use rome_js_semantic::{semantic_model, SemanticModelOptions};
use rome_js_syntax::JsSyntaxNode;
use rome_js_syntax::{
Expand All @@ -12,7 +13,11 @@ use std::{any::type_name, fmt::Debug};
/// Search and renames alls bindings where the name contains "a" replacing it to "b".
/// Asserts the renaming worked.
pub fn assert_rename_binding_a_to_b_ok(before: &str, expected: &str) {
let r = rome_js_parser::parse(before, JsFileSource::js_module());
let r = rome_js_parser::parse(
before,
JsFileSource::js_module(),
JsParserOptions::default(),
);
let model = semantic_model(&r.tree(), SemanticModelOptions::default());

let bindings: Vec<JsIdentifierBinding> = r
Expand Down Expand Up @@ -44,7 +49,11 @@ pub fn assert_rename_binding_a_to_b_ok(before: &str, expected: &str) {
/// Search and renames one binding named "a" to "b".
/// Asserts the renaming fails.
pub fn assert_rename_binding_a_to_b_nok(before: &str) {
let r = rome_js_parser::parse(before, JsFileSource::js_module());
let r = rome_js_parser::parse(
before,
JsFileSource::js_module(),
JsParserOptions::default(),
);
let model = semantic_model(&r.tree(), SemanticModelOptions::default());

let binding_a = r
Expand All @@ -64,7 +73,11 @@ pub fn assert_remove_identifier_a_ok<Anc: AstNode<Language = JsLanguage> + Debug
before: &str,
expected: &str,
) {
let r = rome_js_parser::parse(before, JsFileSource::js_module());
let r = rome_js_parser::parse(
before,
JsFileSource::js_module(),
JsParserOptions::default(),
);

let identifiers_a: Vec<JsSyntaxNode> = r
.syntax()
Expand Down Expand Up @@ -147,7 +160,11 @@ macro_rules! assert_remove_ok {

#[test]
pub fn ok_find_attributes_by_name() {
let r = rome_js_parser::parse(r#"<a a="A" c="C" b="B" />"#, JsFileSource::jsx());
let r = rome_js_parser::parse(
r#"<a a="A" c="C" b="B" />"#,
JsFileSource::jsx(),
JsParserOptions::default(),
);
let list = r
.syntax()
.descendants()
Expand Down
43 changes: 37 additions & 6 deletions crates/rome_js_analyze/tests/spec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use rome_diagnostics::{DiagnosticExt, Error, PrintDiagnostic, Severity};
use rome_js_parser::{
parse,
test_utils::{assert_errors_are_absent, has_bogus_nodes_or_empty_slots},
JsParserOptions,
};
use rome_js_syntax::{JsFileSource, JsLanguage};
use rome_service::configuration::to_analyzer_configuration;
Expand Down Expand Up @@ -74,6 +75,7 @@ fn run_test(input: &'static str, _: &str, _: &str, _: &str) {
file_name,
input_file,
CheckActionType::Lint,
JsParserOptions::default(),
);
}

Expand All @@ -90,6 +92,7 @@ fn run_test(input: &'static str, _: &str, _: &str, _: &str) {
file_name,
input_file,
CheckActionType::Lint,
JsParserOptions::default(),
)
};

Expand All @@ -116,6 +119,7 @@ impl CheckActionType {
}
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn write_analysis_to_snapshot(
snapshot: &mut String,
input_code: &str,
Expand All @@ -124,8 +128,9 @@ pub(crate) fn write_analysis_to_snapshot(
file_name: &str,
input_file: &Path,
check_action_type: CheckActionType,
parser_options: JsParserOptions,
) -> usize {
let parsed = parse(input_code, source_type);
let parsed = parse(input_code, source_type, parser_options.clone());
let root = parsed.tree();

let mut diagnostics = Vec::new();
Expand Down Expand Up @@ -174,11 +179,23 @@ pub(crate) fn write_analysis_to_snapshot(
for action in event.actions() {
if check_action_type.is_suppression() {
if action.is_suppression() {
check_code_action(input_file, input_code, source_type, &action);
check_code_action(
input_file,
input_code,
source_type,
&action,
parser_options.clone(),
);
diag = diag.add_code_suggestion(CodeSuggestionAdvice::from(action));
}
} else if !action.is_suppression() {
check_code_action(input_file, input_code, source_type, &action);
check_code_action(
input_file,
input_code,
source_type,
&action,
parser_options.clone(),
);
diag = diag.add_code_suggestion(CodeSuggestionAdvice::from(action));
}
}
Expand All @@ -191,11 +208,23 @@ pub(crate) fn write_analysis_to_snapshot(
for action in event.actions() {
if check_action_type.is_suppression() {
if action.category.matches("quickfix.suppressRule") {
check_code_action(input_file, input_code, source_type, &action);
check_code_action(
input_file,
input_code,
source_type,
&action,
parser_options.clone(),
);
code_fixes.push(code_fix_to_string(input_code, action));
}
} else if !action.category.matches("quickfix.suppressRule") {
check_code_action(input_file, input_code, source_type, &action);
check_code_action(
input_file,
input_code,
source_type,
&action,
parser_options.clone(),
);
code_fixes.push(code_fix_to_string(input_code, action));
}
}
Expand Down Expand Up @@ -274,6 +303,7 @@ fn check_code_action(
source: &str,
source_type: JsFileSource,
action: &AnalyzerAction<JsLanguage>,
options: JsParserOptions,
) {
let (_, text_edit) = action.mutation.as_text_edits().unwrap_or_default();

Expand All @@ -298,7 +328,7 @@ fn check_code_action(
}

// Re-parse the modified code and panic if the resulting tree has syntax errors
let re_parse = parse(&output, source_type);
let re_parse = parse(&output, source_type, options);
assert_errors_are_absent(&re_parse, path);
}

Expand Down Expand Up @@ -361,6 +391,7 @@ pub(crate) fn run_suppression_test(input: &'static str, _: &str, _: &str, _: &st
file_name,
input_file,
CheckActionType::Suppression,
JsParserOptions::default(),
);

insta::with_settings!({
Expand Down
4 changes: 2 additions & 2 deletions crates/rome_js_formatter/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ impl FormatRule<SourceComment<JsLanguage>> for FormatJsLeadingComment {
/// # Examples
///
/// ```
/// # use rome_js_parser::parse_module;
/// # use rome_js_parser::{JsParserOptions, parse_module};
/// # use rome_js_syntax::JsLanguage;
/// # use rome_rowan::{Direction, SyntaxTriviaPieceComments};
/// use rome_js_formatter::comments::is_doc_comment;
///
/// # fn parse_comment(source: &str) -> SyntaxTriviaPieceComments<JsLanguage> {
/// # let root = parse_module(source).tree();
/// # let root = parse_module(source, JsParserOptions::default()).tree();
/// # root
/// # .eof_token()
/// # .expect("Root to have an EOF token")
Expand Down
14 changes: 7 additions & 7 deletions crates/rome_js_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ mod tests {

use crate::context::JsFormatOptions;
use rome_formatter::IndentStyle;
use rome_js_parser::{parse, parse_script};
use rome_js_parser::{parse, parse_script, JsParserOptions};
use rome_js_syntax::JsFileSource;
use rome_rowan::{TextRange, TextSize};

Expand Down Expand Up @@ -578,7 +578,7 @@ while(
let range_start = TextSize::try_from(input.find("let").unwrap() - 2).unwrap();
let range_end = TextSize::try_from(input.find("const").unwrap()).unwrap();

let tree = parse_script(input);
let tree = parse_script(input, JsParserOptions::default());
let result = format_range(
JsFormatOptions::new(JsFileSource::js_script())
.with_indent_style(IndentStyle::Space(4)),
Expand Down Expand Up @@ -611,7 +611,7 @@ function() {
let range_start = TextSize::try_from(input.find("const").unwrap()).unwrap();
let range_end = TextSize::try_from(input.find('}').unwrap()).unwrap();

let tree = parse_script(input);
let tree = parse_script(input, JsParserOptions::default());
let result = format_range(
JsFormatOptions::new(JsFileSource::js_script())
.with_indent_style(IndentStyle::Space(4)),
Expand Down Expand Up @@ -639,7 +639,7 @@ function() {
let range_start = TextSize::from(5);
let range_end = TextSize::from(5);

let tree = parse_script(input);
let tree = parse_script(input, JsParserOptions::default());
let result = format_range(
JsFormatOptions::new(JsFileSource::js_script())
.with_indent_style(IndentStyle::Space(4)),
Expand Down Expand Up @@ -668,7 +668,7 @@ function() {
}"#
);

let tree = parse_script(input);
let tree = parse_script(input, JsParserOptions::default());
let result = format_range(
JsFormatOptions::new(JsFileSource::js_script())
.with_indent_style(IndentStyle::Space(4)),
Expand Down Expand Up @@ -700,7 +700,7 @@ function() {

debug_assert_eq!(&input[range], r#" quux (); //"#);

let tree = parse_script(input);
let tree = parse_script(input, JsParserOptions::default());
let result = format_range(
JsFormatOptions::new(JsFileSource::js_script())
.with_indent_style(IndentStyle::Space(4)),
Expand All @@ -721,7 +721,7 @@ function() {
let src = "statement();";

let syntax = JsFileSource::js_module();
let tree = parse(src, syntax);
let tree = parse(src, syntax, JsParserOptions::default());

let result = format_range(
JsFormatOptions::new(syntax),
Expand Down
5 changes: 3 additions & 2 deletions crates/rome_js_formatter/src/parentheses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,7 @@ pub(crate) fn debug_assert_is_parent(node: &JsSyntaxNode, parent: &JsSyntaxNode)
pub(crate) mod tests {
use super::NeedsParentheses;
use crate::transform;
use rome_js_parser::JsParserOptions;
use rome_js_syntax::{JsFileSource, JsLanguage};
use rome_rowan::AstNode;

Expand All @@ -1050,7 +1051,7 @@ pub(crate) mod tests {
index: Option<usize>,
source_type: JsFileSource,
) {
let parse = rome_js_parser::parse(input, source_type);
let parse = rome_js_parser::parse(input, source_type, JsParserOptions::default());

let diagnostics = parse.diagnostics();
assert!(
Expand Down Expand Up @@ -1091,7 +1092,7 @@ pub(crate) mod tests {
index: Option<usize>,
source_type: JsFileSource,
) {
let parse = rome_js_parser::parse(input, source_type);
let parse = rome_js_parser::parse(input, source_type, JsParserOptions::default());

let diagnostics = parse.diagnostics();
assert!(
Expand Down
Loading

0 comments on commit a34cdcc

Please sign in to comment.