Run rustfmt and keep it running on CI (#2023)

Thought we did this awhile back, but looks like we forgot to do so!
This commit is contained in:
Alex Crichton 2020-03-02 11:44:14 -06:00 committed by GitHub
parent 15e9c54a20
commit 381660c49b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 120 additions and 62 deletions

View File

@ -5,6 +5,12 @@ trigger:
- refs/tags/*
jobs:
- job: test_formatting
displayName: "Run cargo fmt"
steps:
- template: ci/azure-install-rust.yml
- script: cargo fmt --all -- --check
- job: test_wasm_bindgen
displayName: "Run wasm-bindgen crate tests (unix)"
steps:

View File

@ -42,7 +42,8 @@ impl TryToTokens for ast::Program {
DescribeImport {
kind: &i.kind,
unstable_api: i.unstable_api,
}.to_tokens(tokens);
}
.to_tokens(tokens);
// If there is a js namespace, check that name isn't a type. If it is,
// this import might be a method on that type.

View File

@ -157,7 +157,6 @@ impl<T: Hash> fmt::Display for ShortHash<T> {
}
}
/// Create syn attribute for `#[cfg(web_sys_unstable_apis)]` and a doc comment.
pub fn unstable_api_attrs() -> proc_macro2::TokenStream {
quote::quote!(

View File

@ -136,10 +136,11 @@ impl WasmBindgenDescriptorsSection {
let (id, import_id) =
module.add_import_func("__wbindgen_placeholder__", &import_name, ty);
module.funcs.get_mut(id).name = Some(import_name);
self.closure_imports.insert(import_id, descriptor.clone().unwrap_closure());
self.closure_imports
.insert(import_id, descriptor.clone().unwrap_closure());
self.cached_closures.insert(descriptor, id);
id
},
}
};
let local = match &mut module.funcs.get_mut(func).kind {

View File

@ -216,10 +216,7 @@ impl<'a> Context<'a> {
js.push_str("const __exports = {};\n");
js.push_str("let wasm;\n");
init = self.gen_init(needs_manual_start, None)?;
footer.push_str(&format!(
"{} = Object.assign(init, __exports);\n",
global
));
footer.push_str(&format!("{} = Object.assign(init, __exports);\n", global));
}
// With normal CommonJS node we need to defer requiring the wasm
@ -319,7 +316,7 @@ impl<'a> Context<'a> {
let mut imports = String::new();
if self.config.omit_imports {
return Ok(imports)
return Ok(imports);
}
match &self.config.mode {
@ -759,7 +756,10 @@ impl<'a> Context<'a> {
return;
}
assert!(!self.config.anyref);
self.global(&format!("const heap = new Array({}).fill(undefined);", INITIAL_HEAP_OFFSET));
self.global(&format!(
"const heap = new Array({}).fill(undefined);",
INITIAL_HEAP_OFFSET
));
self.global(&format!("heap.push({});", INITIAL_HEAP_VALUES.join(", ")));
}
@ -2440,7 +2440,6 @@ impl<'a> Context<'a> {
dtor = dtor,
call = call,
))
} else {
self.expose_make_closure()?;

View File

@ -187,7 +187,10 @@ impl Default for BindgenAttrs {
// sanity check that we call `check_used` an appropriate number of
// times.
ATTRS.with(|state| state.parsed.set(state.parsed.get() + 1));
BindgenAttrs { attrs: Vec::new(), unstable_api_attr: None, }
BindgenAttrs {
attrs: Vec::new(),
unstable_api_attr: None,
}
}
}

View File

@ -14,7 +14,9 @@ impl A {
pub fn foo(&self) {}
pub fn ret_bool(&self) -> bool { true }
pub fn ret_bool(&self) -> bool {
true
}
pub fn take_bool(&self, _: bool) {}
pub fn take_many(&self, _: bool, _: f64, _: u32) {}
}

View File

@ -72,26 +72,27 @@ fn main() -> Result<()> {
let source = read_source_from_path("webidls/enabled")?;
let unstable_source = read_source_from_path("webidls/unstable")?;
let bindings = match wasm_bindgen_webidl::compile(&source.contents, &unstable_source.contents, allowed) {
Ok(bindings) => bindings,
Err(e) => {
if let Some(err) = e.downcast_ref::<wasm_bindgen_webidl::WebIDLParseError>() {
if let Some(pos) = source.resolve_offset(err.0) {
let ctx = format!(
"compiling WebIDL into wasm-bindgen bindings in file \
let bindings =
match wasm_bindgen_webidl::compile(&source.contents, &unstable_source.contents, allowed) {
Ok(bindings) => bindings,
Err(e) => {
if let Some(err) = e.downcast_ref::<wasm_bindgen_webidl::WebIDLParseError>() {
if let Some(pos) = source.resolve_offset(err.0) {
let ctx = format!(
"compiling WebIDL into wasm-bindgen bindings in file \
\"{}\", line {} column {}",
pos.filename,
pos.line + 1,
pos.col + 1
);
return Err(e.context(ctx));
} else {
return Err(e.context("compiling WebIDL into wasm-bindgen bindings"));
pos.filename,
pos.line + 1,
pos.col + 1
);
return Err(e.context(ctx));
} else {
return Err(e.context("compiling WebIDL into wasm-bindgen bindings"));
}
}
return Err(e.context("compiling WebIDL into wasm-bindgen bindings"));
}
return Err(e.context("compiling WebIDL into wasm-bindgen bindings"));
}
};
};
let out_dir = env::var("OUT_DIR").context("reading OUT_DIR environment variable")?;
let out_file_path = path::Path::new(&out_dir).join("bindings.rs");

View File

@ -5,10 +5,10 @@ use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
pub mod anchor_element;
pub mod blob;
pub mod body_element;
pub mod br_element;
pub mod button_element;
pub mod blob;
pub mod console;
pub mod div_element;
pub mod element;

View File

@ -10,8 +10,8 @@
//!
//! @see https://github.com/rustwasm/wasm-bindgen/issues/1005
use wasm_bindgen::{JsCast, JsValue};
use web_sys::{WebGl2RenderingContext, WebGlRenderingContext, WebSocket};
use wasm_bindgen::{JsValue, JsCast};
// Ensure that our whitelisted WebGlRenderingContext methods compile with immutable slices.
fn test_webgl_rendering_context_immutable_slices() {
@ -60,9 +60,9 @@ fn test_webgl_rendering_context_immutable_slices() {
fn test_webgl2_rendering_context_immutable_slices() {
let gl = JsValue::null().unchecked_into::<WebGl2RenderingContext>();
gl.tex_image_3d_with_opt_u8_array(0, 0, 0, 0, 0, 0, 0, 0, 0, Some(&[1]));
gl.tex_sub_image_3d_with_opt_u8_array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Some(&[1]));
gl.compressed_tex_image_3d_with_u8_array(0, 0, 0, 0, 0, 0, 0, &[1]);
gl.tex_image_3d_with_opt_u8_array(0, 0, 0, 0, 0, 0, 0, 0, 0, Some(&[1]));
gl.tex_sub_image_3d_with_opt_u8_array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Some(&[1]));
gl.compressed_tex_image_3d_with_u8_array(0, 0, 0, 0, 0, 0, 0, &[1]);
}
// Ensure that our whitelisted WebSocket methods compile with immutable slices.

View File

@ -26,7 +26,8 @@ fn main() {
} else {
(idl, String::new())
};
let mut generated_rust = wasm_bindgen_webidl::compile(&stable_source, &experimental_source, None).unwrap();
let mut generated_rust =
wasm_bindgen_webidl::compile(&stable_source, &experimental_source, None).unwrap();
generated_rust.insert_str(
0,

View File

@ -20,7 +20,10 @@ use weedle::CallbackInterfaceDefinition;
use weedle::{DictionaryDefinition, PartialDictionaryDefinition};
use super::Result;
use crate::{util::{self, camel_case_ident}, ApiStability};
use crate::{
util::{self, camel_case_ident},
ApiStability,
};
/// Collection of constructs that may use partial.
#[derive(Default)]
@ -139,7 +142,11 @@ pub(crate) trait FirstPass<'src, Ctx> {
}
impl<'src> FirstPass<'src, ApiStability> for [weedle::Definition<'src>] {
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, stability: ApiStability) -> Result<()> {
fn first_pass(
&'src self,
record: &mut FirstPassRecord<'src>,
stability: ApiStability,
) -> Result<()> {
for def in self {
def.first_pass(record, stability)?;
}
@ -149,7 +156,11 @@ impl<'src> FirstPass<'src, ApiStability> for [weedle::Definition<'src>] {
}
impl<'src> FirstPass<'src, ApiStability> for weedle::Definition<'src> {
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, stability: ApiStability) -> Result<()> {
fn first_pass(
&'src self,
record: &mut FirstPassRecord<'src>,
stability: ApiStability,
) -> Result<()> {
use weedle::Definition::*;
match self {
@ -172,15 +183,16 @@ impl<'src> FirstPass<'src, ApiStability> for weedle::Definition<'src> {
}
impl<'src> FirstPass<'src, ApiStability> for weedle::DictionaryDefinition<'src> {
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, stability: ApiStability) -> Result<()> {
fn first_pass(
&'src self,
record: &mut FirstPassRecord<'src>,
stability: ApiStability,
) -> Result<()> {
if util::is_chrome_only(&self.attributes) {
return Ok(());
}
let dictionary_data = record
.dictionaries
.entry(self.identifier.0)
.or_default();
let dictionary_data = record.dictionaries.entry(self.identifier.0).or_default();
dictionary_data.definition = Some(self);
dictionary_data.stability = stability;
@ -207,7 +219,11 @@ impl<'src> FirstPass<'src, ()> for weedle::PartialDictionaryDefinition<'src> {
}
impl<'src> FirstPass<'src, ApiStability> for weedle::EnumDefinition<'src> {
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, stability: ApiStability) -> Result<()> {
fn first_pass(
&'src self,
record: &mut FirstPassRecord<'src>,
stability: ApiStability,
) -> Result<()> {
if util::is_chrome_only(&self.attributes) {
return Ok(());
}
@ -325,7 +341,11 @@ fn first_pass_operation<'src>(
}
impl<'src> FirstPass<'src, ApiStability> for weedle::InterfaceDefinition<'src> {
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, stability: ApiStability) -> Result<()> {
fn first_pass(
&'src self,
record: &mut FirstPassRecord<'src>,
stability: ApiStability,
) -> Result<()> {
if util::is_chrome_only(&self.attributes) {
return Ok(());
}
@ -410,7 +430,11 @@ fn process_interface_attribute<'src>(
}
impl<'src> FirstPass<'src, ApiStability> for weedle::PartialInterfaceDefinition<'src> {
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, stability: ApiStability) -> Result<()> {
fn first_pass(
&'src self,
record: &mut FirstPassRecord<'src>,
stability: ApiStability,
) -> Result<()> {
if util::is_chrome_only(&self.attributes) {
return Ok(());
}
@ -425,7 +449,7 @@ impl<'src> FirstPass<'src, ApiStability> for weedle::PartialInterfaceDefinition<
for member in &self.members.body {
member.first_pass(record, (self.identifier.0, stability))?;
}
Ok(())
}
}
@ -513,7 +537,9 @@ impl<'src> FirstPass<'src, &'src str> for weedle::interface::OperationInterfaceM
}
}
impl<'src> FirstPass<'src, (&'src str, ApiStability)> for weedle::interface::AttributeInterfaceMember<'src> {
impl<'src> FirstPass<'src, (&'src str, ApiStability)>
for weedle::interface::AttributeInterfaceMember<'src>
{
fn first_pass(
&'src self,
record: &mut FirstPassRecord<'src>,
@ -530,14 +556,18 @@ impl<'src> FirstPass<'src, (&'src str, ApiStability)> for weedle::interface::Att
.attributes
.push(AttributeInterfaceData {
definition: self,
stability: ctx.1
stability: ctx.1,
});
Ok(())
}
}
impl<'src> FirstPass<'src, ApiStability> for weedle::InterfaceMixinDefinition<'src> {
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, stability: ApiStability) -> Result<()> {
fn first_pass(
&'src self,
record: &mut FirstPassRecord<'src>,
stability: ApiStability,
) -> Result<()> {
if util::is_chrome_only(&self.attributes) {
return Ok(());
}
@ -558,7 +588,11 @@ impl<'src> FirstPass<'src, ApiStability> for weedle::InterfaceMixinDefinition<'s
}
impl<'src> FirstPass<'src, ApiStability> for weedle::PartialInterfaceMixinDefinition<'src> {
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, stability: ApiStability) -> Result<()> {
fn first_pass(
&'src self,
record: &mut FirstPassRecord<'src>,
stability: ApiStability,
) -> Result<()> {
if util::is_chrome_only(&self.attributes) {
return Ok(());
}
@ -604,7 +638,9 @@ impl<'src> FirstPass<'src, (&'src str, ApiStability)> for weedle::mixin::MixinMe
}
}
impl<'src> FirstPass<'src, (&'src str, ApiStability)> for weedle::mixin::OperationMixinMember<'src> {
impl<'src> FirstPass<'src, (&'src str, ApiStability)>
for weedle::mixin::OperationMixinMember<'src>
{
fn first_pass(
&'src self,
record: &mut FirstPassRecord<'src>,
@ -629,7 +665,9 @@ impl<'src> FirstPass<'src, (&'src str, ApiStability)> for weedle::mixin::Operati
}
}
impl<'src> FirstPass<'src, (&'src str, ApiStability)> for weedle::mixin::AttributeMixinMember<'src> {
impl<'src> FirstPass<'src, (&'src str, ApiStability)>
for weedle::mixin::AttributeMixinMember<'src>
{
fn first_pass(
&'src self,
record: &mut FirstPassRecord<'src>,
@ -645,7 +683,7 @@ impl<'src> FirstPass<'src, (&'src str, ApiStability)> for weedle::mixin::Attribu
.attributes
.push(AttributeMixinData {
definition: self,
stability: ctx.1
stability: ctx.1,
});
Ok(())
}

View File

@ -97,7 +97,11 @@ fn parse_source(source: &str) -> Result<Vec<weedle::Definition>> {
}
/// Parse a string of WebIDL source text into a wasm-bindgen AST.
fn parse(webidl_source: &str, unstable_source: &str, allowed_types: Option<&[&str]>) -> Result<Program> {
fn parse(
webidl_source: &str,
unstable_source: &str,
allowed_types: Option<&[&str]>,
) -> Result<Program> {
let mut first_pass_record: FirstPassRecord = Default::default();
first_pass_record.builtin_idents = builtin_idents();
first_pass_record.immutable_slice_whitelist = immutable_slice_whitelist();
@ -164,7 +168,11 @@ fn parse(webidl_source: &str, unstable_source: &str, allowed_types: Option<&[&st
/// Compile the given WebIDL source text into Rust source text containing
/// `wasm-bindgen` bindings to the things described in the WebIDL.
pub fn compile(webidl_source: &str, experimental_source: &str, allowed_types: Option<&[&str]>) -> Result<String> {
pub fn compile(
webidl_source: &str,
experimental_source: &str,
allowed_types: Option<&[&str]>,
) -> Result<String> {
let ast = parse(webidl_source, experimental_source, allowed_types)?;
Ok(compile_ast(ast))
}
@ -785,7 +793,9 @@ impl<'src> FirstPassRecord<'src> {
OperationId::IndexingDeleter => Some(format!("The indexing deleter\n\n")),
};
let attrs = data.definition_attributes;
for mut method in self.create_imports(attrs, kind, id, op_data, data.stability.is_unstable()) {
for mut method in
self.create_imports(attrs, kind, id, op_data, data.stability.is_unstable())
{
let mut doc = doc.clone();
self.append_required_features_doc(&method, &mut doc, &[]);
method.doc_comment = doc;

View File

@ -40,10 +40,7 @@ pub async fn run(repo: String) -> Result<JsValue, JsValue> {
let url = format!("https://api.github.com/repos/{}/branches/master", repo);
let request = Request::new_with_str_and_init(
&url,
&opts,
)?;
let request = Request::new_with_str_and_init(&url, &opts)?;
request
.headers()