Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into p/literal
Browse files Browse the repository at this point in the history
  • Loading branch information
leaysgur committed Dec 17, 2024
2 parents 94fc3df + de8a86e commit 6b00b5f
Show file tree
Hide file tree
Showing 40 changed files with 2,185 additions and 477 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release_napi_parser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ jobs:

### install musl dependencies ###
#
- uses: goto-bus-stop/setup-zig@222d316fd35ce56a4141fe4a869e1aeefc7f3590 # v1.4.0
- uses: mlugg/setup-zig@a67e68dc5c8281d9608136d3d7ca1b282213e4ac # v1.2.1
if: ${{ contains(matrix.target, 'musl') }}
with:
version: 0.11.0
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release_napi_transform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ jobs:

### install musl dependencies ###

- uses: goto-bus-stop/setup-zig@222d316fd35ce56a4141fe4a869e1aeefc7f3590 # v1.4.0
- uses: mlugg/setup-zig@a67e68dc5c8281d9608136d3d7ca1b282213e4ac # v1.2.1
if: ${{ contains(matrix.target, 'musl') }}
with:
version: 0.11.0
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub trait GenExpr: GetSpan {

impl Gen for Program<'_> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
p.is_jsx = self.source_type.is_jsx();

if let Some(hashbang) = &self.hashbang {
hashbang.print(p, ctx);
}
Expand Down Expand Up @@ -2976,6 +2978,10 @@ impl Gen for TSTypeParameterDeclaration<'_> {
p.print_soft_newline();
p.dedent();
p.print_indent();
} else if p.is_jsx {
// `<T,>() => {}`
// ^ We need a comma here, otherwise it will be regarded as a JSX element.
p.print_str(",");
}
p.print_ascii_byte(b'>');
}
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ pub struct Codegen<'a> {
need_space_before_dot: usize,
print_next_indent_as_space: bool,
binary_expr_stack: Vec<BinaryExpressionVisitor<'a>>,
/// Indicates the output is JSX type, it is set in [`Program::gen`] and the result
/// is obtained by [`oxc_span::SourceType::is_jsx`]
is_jsx: bool,

/// For avoiding `;` if the previous statement ends with `}`.
needs_semicolon: bool,
Expand Down Expand Up @@ -170,6 +173,7 @@ impl<'a> Codegen<'a> {
start_of_stmt: 0,
start_of_arrow_expr: 0,
start_of_default_export: 0,
is_jsx: false,
indent: 0,
quote: b'"',
print_comments,
Expand Down
19 changes: 18 additions & 1 deletion crates/oxc_codegen/tests/integration/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,24 @@ pub fn test(source_text: &str, expected: &str) {
}

pub fn test_options(source_text: &str, expected: &str, options: CodegenOptions) {
let source_type = SourceType::jsx();
test_options_with_source_type(source_text, expected, SourceType::jsx(), options);
}

pub fn test_tsx(source_text: &str, expected: &str) {
test_options_with_source_type(
source_text,
expected,
SourceType::tsx(),
CodegenOptions::default(),
);
}

pub fn test_options_with_source_type(
source_text: &str,
expected: &str,
source_type: SourceType,
options: CodegenOptions,
) {
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse();
let result = CodeGenerator::new().with_options(options).build(&ret.program).code;
Expand Down
8 changes: 7 additions & 1 deletion crates/oxc_codegen/tests/integration/ts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use oxc_codegen::CodegenOptions;

use crate::{snapshot, snapshot_options};
use crate::{snapshot, snapshot_options, tester::test_tsx};

#[test]
fn ts() {
Expand Down Expand Up @@ -99,3 +99,9 @@ export { default as name16 } from "module-name";
&CodegenOptions { minify: true, ..CodegenOptions::default() },
);
}

#[test]
fn tsx() {
test_tsx("<T,>() => {}", "<T,>() => {};\n");
test_tsx("<T, B>() => {}", "<\n\tT,\n\tB\n>() => {};\n");
}
6 changes: 6 additions & 0 deletions crates/oxc_data_structures/src/stack/non_empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ impl<T> DerefMut for NonEmptyStack<T> {
}
}

impl<T: Default> Default for NonEmptyStack<T> {
fn default() -> Self {
Self::new(T::default())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
24 changes: 13 additions & 11 deletions crates/oxc_ecmascript/src/constant_evaluation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,19 @@ pub trait ConstantEvaluation<'a> {
})
}
BinaryOperator::BitwiseAnd | BinaryOperator::BitwiseOR | BinaryOperator::BitwiseXOR => {
if left.is_big_int_literal() && right.is_big_int_literal() {
let left_bigint = self.get_side_free_bigint_value(left);
let right_bigint = self.get_side_free_bigint_value(right);
if let (Some(left_val), Some(right_val)) = (left_bigint, right_bigint) {
let result_val: BigInt = match operator {
BinaryOperator::BitwiseAnd => left_val & right_val,
BinaryOperator::BitwiseOR => left_val | right_val,
BinaryOperator::BitwiseXOR => left_val ^ right_val,
_ => unreachable!(),
};
return Some(ConstantValue::BigInt(result_val));
}
}
let left_num = self.get_side_free_number_value(left);
let right_num = self.get_side_free_number_value(right);
if let (Some(left_val), Some(right_val)) = (left_num, right_num) {
Expand All @@ -332,17 +345,6 @@ pub trait ConstantEvaluation<'a> {
};
return Some(ConstantValue::Number(result_val));
}
let left_bitint = self.get_side_free_bigint_value(left);
let right_bitint = self.get_side_free_bigint_value(right);
if let (Some(left_val), Some(right_val)) = (left_bitint, right_bitint) {
let result_val: BigInt = match operator {
BinaryOperator::BitwiseAnd => left_val & right_val,
BinaryOperator::BitwiseOR => left_val | right_val,
BinaryOperator::BitwiseXOR => left_val ^ right_val,
_ => unreachable!(),
};
return Some(ConstantValue::BigInt(result_val));
}
None
}
_ => None,
Expand Down
132 changes: 132 additions & 0 deletions crates/oxc_language_server/src/capabilities.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
use tower_lsp::lsp_types::{
ClientCapabilities, CodeActionKind, CodeActionOptions, CodeActionProviderCapability, OneOf,
ServerCapabilities, TextDocumentSyncCapability, TextDocumentSyncKind, WorkDoneProgressOptions,
WorkspaceFoldersServerCapabilities, WorkspaceServerCapabilities,
};

pub const CODE_ACTION_KIND_SOURCE_FIX_ALL_OXC: CodeActionKind =
CodeActionKind::new("source.fixAll.oxc");

pub struct Capabilities {
pub code_action_provider: bool,
}

impl From<ClientCapabilities> for Capabilities {
fn from(value: ClientCapabilities) -> Self {
// check if the client support some code action literal support
let code_action_provider = value.text_document.is_some_and(|capability| {
capability.code_action.is_some_and(|code_action| {
code_action.code_action_literal_support.is_some_and(|literal_support| {
!literal_support.code_action_kind.value_set.is_empty()
})
})
});

Self { code_action_provider }
}
}

impl From<Capabilities> for ServerCapabilities {
fn from(value: Capabilities) -> Self {
Self {
text_document_sync: Some(TextDocumentSyncCapability::Kind(TextDocumentSyncKind::FULL)),
workspace: Some(WorkspaceServerCapabilities {
workspace_folders: Some(WorkspaceFoldersServerCapabilities {
supported: Some(true),
change_notifications: Some(OneOf::Left(true)),
}),
file_operations: None,
}),
code_action_provider: if value.code_action_provider {
Some(CodeActionProviderCapability::Options(CodeActionOptions {
code_action_kinds: Some(vec![
CodeActionKind::QUICKFIX,
CODE_ACTION_KIND_SOURCE_FIX_ALL_OXC,
]),
work_done_progress_options: WorkDoneProgressOptions {
work_done_progress: None,
},
resolve_provider: None,
}))
} else {
None
},
..ServerCapabilities::default()
}
}
}

#[cfg(test)]
mod test {
use tower_lsp::lsp_types::{
ClientCapabilities, CodeActionClientCapabilities, CodeActionKindLiteralSupport,
CodeActionLiteralSupport, TextDocumentClientCapabilities,
};

use super::Capabilities;

#[test]
fn test_code_action_provider_vscode() {
let client_capabilities = ClientCapabilities {
text_document: Some(TextDocumentClientCapabilities {
code_action: Some(CodeActionClientCapabilities {
code_action_literal_support: Some(CodeActionLiteralSupport {
code_action_kind: CodeActionKindLiteralSupport {
// this is from build (see help, about):
// Version: 1.95.3 (user setup)
// Commit: f1a4fb101478ce6ec82fe9627c43efbf9e98c813
value_set: vec![
#[allow(clippy::manual_string_new)]
"".into(),
"quickfix".into(),
"refactor".into(),
"refactor.extract".into(),
"refactor.inline".into(),
"refactor.rewrite".into(),
"source".into(),
"source.organizeImports".into(),
],
},
}),
..CodeActionClientCapabilities::default()
}),
..TextDocumentClientCapabilities::default()
}),
..ClientCapabilities::default()
};

let capabilities = Capabilities::from(client_capabilities);

assert!(capabilities.code_action_provider);
}

#[test]
fn test_code_action_provider_intellij() {
let client_capabilities = ClientCapabilities {
text_document: Some(TextDocumentClientCapabilities {
code_action: Some(CodeActionClientCapabilities {
code_action_literal_support: Some(CodeActionLiteralSupport {
code_action_kind: CodeActionKindLiteralSupport {
// this is from build (see help, about):
// Build #IU-243.22562.145, built on December 8, 2024
value_set: vec![
"quickfix".into(),
#[allow(clippy::manual_string_new)]
"".into(),
"source".into(),
"refactor".into(),
],
},
}),
..CodeActionClientCapabilities::default()
}),
..TextDocumentClientCapabilities::default()
}),
..ClientCapabilities::default()
};

let capabilities = Capabilities::from(client_capabilities);

assert!(capabilities.code_action_provider);
}
}
53 changes: 8 additions & 45 deletions crates/oxc_language_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,21 @@ use tokio::sync::{Mutex, OnceCell, RwLock, SetError};
use tower_lsp::{
jsonrpc::{Error, ErrorCode, Result},
lsp_types::{
CodeAction, CodeActionKind, CodeActionOptions, CodeActionOrCommand, CodeActionParams,
CodeActionProviderCapability, CodeActionResponse, ConfigurationItem, Diagnostic,
DidChangeConfigurationParams, DidChangeTextDocumentParams, DidChangeWatchedFilesParams,
DidCloseTextDocumentParams, DidOpenTextDocumentParams, DidSaveTextDocumentParams,
InitializeParams, InitializeResult, InitializedParams, NumberOrString, OneOf, Position,
Range, ServerCapabilities, ServerInfo, TextDocumentSyncCapability, TextDocumentSyncKind,
TextEdit, Url, WorkDoneProgressOptions, WorkspaceEdit, WorkspaceFoldersServerCapabilities,
WorkspaceServerCapabilities,
CodeAction, CodeActionKind, CodeActionOrCommand, CodeActionParams, CodeActionResponse,
ConfigurationItem, Diagnostic, DidChangeConfigurationParams, DidChangeTextDocumentParams,
DidChangeWatchedFilesParams, DidCloseTextDocumentParams, DidOpenTextDocumentParams,
DidSaveTextDocumentParams, InitializeParams, InitializeResult, InitializedParams,
NumberOrString, Position, Range, ServerInfo, TextEdit, Url, WorkspaceEdit,
},
Client, LanguageServer, LspService, Server,
};

use oxc_linter::{FixKind, LinterBuilder, Oxlintrc};

use crate::capabilities::{Capabilities, CODE_ACTION_KIND_SOURCE_FIX_ALL_OXC};
use crate::linter::{DiagnosticReport, ServerLinter};

mod capabilities;
mod linter;

type FxDashMap<K, V> = DashMap<K, V, FxBuildHasher>;
Expand Down Expand Up @@ -88,9 +87,6 @@ enum SyntheticRunLevel {
OnType,
}

const CODE_ACTION_KIND_SOURCE_FIX_ALL_OXC: CodeActionKind =
CodeActionKind::new("source.fixAll.oxc");

#[tower_lsp::async_trait]
impl LanguageServer for Backend {
async fn initialize(&self, params: InitializeParams) -> Result<InitializeResult> {
Expand All @@ -106,45 +102,12 @@ impl LanguageServer for Backend {
*self.options.lock().await = value;
}

// check if the client support some code action literal support
let code_action_provider = if params.capabilities.text_document.is_some_and(|capability| {
capability.code_action.is_some_and(|code_action| {
code_action.code_action_literal_support.is_some_and(|literal_support| {
!literal_support.code_action_kind.value_set.is_empty()
})
})
}) {
Some(CodeActionProviderCapability::Options(CodeActionOptions {
code_action_kinds: Some(vec![
CodeActionKind::QUICKFIX,
CODE_ACTION_KIND_SOURCE_FIX_ALL_OXC,
]),
work_done_progress_options: WorkDoneProgressOptions { work_done_progress: None },
resolve_provider: None,
}))
} else {
None
};

let oxlintrc = self.init_linter_config().await;
self.init_ignore_glob(oxlintrc).await;
Ok(InitializeResult {
server_info: Some(ServerInfo { name: "oxc".into(), version: None }),
offset_encoding: None,
capabilities: ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Kind(
TextDocumentSyncKind::FULL,
)),
workspace: Some(WorkspaceServerCapabilities {
workspace_folders: Some(WorkspaceFoldersServerCapabilities {
supported: Some(true),
change_notifications: Some(OneOf::Left(true)),
}),
file_operations: None,
}),
code_action_provider,
..ServerCapabilities::default()
},
capabilities: Capabilities::from(params.capabilities).into(),
})
}

Expand Down
Loading

0 comments on commit 6b00b5f

Please sign in to comment.