Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cargo clippy and cargo fmt to CI #34625

Merged
merged 1 commit into from Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 40 additions & 2 deletions .github/workflows/build_test_deploy.yml
Expand Up @@ -88,6 +88,44 @@ jobs:
- run: ./scripts/check-manifests.js
- run: yarn lint

rust-check:
runs-on: ubuntu-latest
needs: build
steps:
- name: Install
uses: actions-rs/toolchain@v1
if: ${{ steps.docs-change.outputs.DOCS_CHANGE != 'docs only change' }}
with:
profile: minimal
toolchain: nightly-2021-11-15
components: rustfmt, clippy

- name: Cache cargo registry
uses: actions/cache@v2
if: ${{ steps.docs-change.outputs.DOCS_CHANGE != 'docs only change' }}
with:
path: ~/.cargo/registry
key: stable-ubuntu-clippy-cargo-registry

- name: Cache cargo index
uses: actions/cache@v2
if: ${{ steps.docs-change.outputs.DOCS_CHANGE != 'docs only change' }}
with:
path: ~/.cargo/git
key: stable-ubuntu-clippy-cargo-index

- uses: actions/cache@v2
id: restore-build
with:
path: ./*
key: ${{ github.sha }}-${{ github.run_number }}-${{ github.run_attempt }}

- name: Check
run: |
cargo fmt -- --check
cargo clippy --all -- -D warnings
working-directory: packages/next-swc

checkPrecompiled:
name: Check Pre-compiled
runs-on: ubuntu-latest
Expand Down Expand Up @@ -610,14 +648,14 @@ jobs:
toolchain: nightly-2021-11-15

- name: Cache cargo registry
uses: actions/cache@v1
uses: actions/cache@v2
if: ${{ steps.docs-change.outputs.DOCS_CHANGE != 'docs only change' }}
with:
path: ~/.cargo/registry
key: stable-ubuntu-18.04-cargo-registry

- name: Cache cargo index
uses: actions/cache@v1
uses: actions/cache@v2
if: ${{ steps.docs-change.outputs.DOCS_CHANGE != 'docs only change' }}
with:
path: ~/.cargo/git
Expand Down
90 changes: 45 additions & 45 deletions packages/next-swc/crates/core/src/amp_attributes.rs
@@ -1,63 +1,63 @@
use swc_atoms::JsWord;
use swc_ecmascript::ast::{
Ident, JSXAttr, JSXAttrName, JSXAttrOrSpread, JSXElementName, JSXOpeningElement,
Ident, JSXAttr, JSXAttrName, JSXAttrOrSpread, JSXElementName, JSXOpeningElement,
};
use swc_ecmascript::visit::Fold;

pub fn amp_attributes() -> impl Fold {
AmpAttributePatcher::default()
AmpAttributePatcher::default()
}

#[derive(Debug, Default)]
struct AmpAttributePatcher {}

impl Fold for AmpAttributePatcher {
fn fold_jsx_opening_element(&mut self, node: JSXOpeningElement) -> JSXOpeningElement {
let JSXOpeningElement {
name,
mut attrs,
span,
self_closing,
type_args,
} = node;
let n = name.clone();

if let JSXElementName::Ident(Ident { sym, .. }) = name {
if sym.starts_with("amp-") {
for i in 0..attrs.len() {
if let JSXAttrOrSpread::JSXAttr(JSXAttr {
name:
JSXAttrName::Ident(Ident {
sym,
span: s,
optional: o,
}),
fn fold_jsx_opening_element(&mut self, node: JSXOpeningElement) -> JSXOpeningElement {
let JSXOpeningElement {
name,
mut attrs,
span,
value,
}) = &attrs[i]
{
if sym as &str == "className" {
attrs[i] = JSXAttrOrSpread::JSXAttr(JSXAttr {
name: JSXAttrName::Ident(Ident {
sym: JsWord::from("class"),
span: s.clone(),
optional: o.clone(),
}),
span: span.clone(),
value: value.clone(),
})
self_closing,
type_args,
} = node;
let n = name.clone();

if let JSXElementName::Ident(Ident { sym, .. }) = name {
if sym.starts_with("amp-") {
for i in &mut attrs {
if let JSXAttrOrSpread::JSXAttr(JSXAttr {
name:
JSXAttrName::Ident(Ident {
sym,
span: s,
optional: o,
}),
span,
value,
}) = &i
{
if sym as &str == "className" {
*i = JSXAttrOrSpread::JSXAttr(JSXAttr {
name: JSXAttrName::Ident(Ident {
sym: JsWord::from("class"),
span: *s,
optional: *o,
}),
span: *span,
value: value.clone(),
})
}
}
}
}
}
}
}
}

JSXOpeningElement {
name: n,
attrs,
span,
self_closing,
type_args,
JSXOpeningElement {
name: n,
attrs,
span,
self_closing,
type_args,
}
}
}
}
16 changes: 6 additions & 10 deletions packages/next-swc/crates/core/src/auto_cjs/mod.rs
Expand Up @@ -18,17 +18,13 @@ struct CjsFinder {
/// does not support changing configuration based on content of the file.
impl Visit for CjsFinder {
fn visit_member_expr(&mut self, e: &MemberExpr) {
match &*e.obj {
Expr::Ident(obj) => match &e.prop {
MemberProp::Ident(prop) => {
if &*obj.sym == "module" && &*prop.sym == "exports" {
self.found = true;
return;
}
if let Expr::Ident(obj) = &*e.obj {
if let MemberProp::Ident(prop) = &e.prop {
if &*obj.sym == "module" && &*prop.sym == "exports" {
self.found = true;
return;
}
_ => {}
},
_ => {}
}
}

e.obj.visit_with(self);
Expand Down
Expand Up @@ -4,10 +4,7 @@ use swc_ecmascript::utils::HANDLER;
use swc_ecmascript::visit::{noop_fold_type, Fold};

pub fn disallow_re_export_all_in_page(is_page_file: bool) -> impl Fold {
Optional::new(
DisallowReExportAllInPage,
is_page_file
)
Optional::new(DisallowReExportAllInPage, is_page_file)
}

struct DisallowReExportAllInPage;
Expand Down
25 changes: 13 additions & 12 deletions packages/next-swc/crates/core/src/hook_optimizer.rs
Expand Up @@ -75,8 +75,8 @@ impl HookOptimizer {
if let Expr::Call(c) = &*init.as_deref().unwrap() {
if let Callee::Expr(i) = &c.callee {
if let Expr::Ident(Ident { sym, .. }) = &**i {
if self.hooks.contains(&sym) {
let name = get_object_pattern(&a);
if self.hooks.contains(sym) {
let name = get_object_pattern(a);
return VarDeclarator {
name,
init: init_clone,
Expand All @@ -89,7 +89,7 @@ impl HookOptimizer {
}
}

return decl;
decl
}
}

Expand All @@ -98,15 +98,16 @@ fn get_object_pattern(array_pattern: &ArrayPat) -> Pat {
.elems
.iter()
.enumerate()
.filter_map(|(i, elem)| match elem {
Some(elem) => Some(ObjectPatProp::KeyValue(KeyValuePatProp {
key: PropName::Num(Number {
value: i as f64,
span: DUMMY_SP,
}),
value: Box::new(elem.clone()),
})),
None => None,
.filter_map(|(i, elem)| {
elem.as_ref().map(|elem| {
ObjectPatProp::KeyValue(KeyValuePatProp {
key: PropName::Num(Number {
value: i as f64,
span: DUMMY_SP,
}),
value: Box::new(elem.clone()),
})
})
})
.collect();

Expand Down
12 changes: 8 additions & 4 deletions packages/next-swc/crates/core/src/lib.rs
Expand Up @@ -27,7 +27,7 @@ DEALINGS IN THE SOFTWARE.
*/

#![recursion_limit = "2048"]
//#![deny(clippy::all)]
#![deny(clippy::all)]

use auto_cjs::contains_cjs;
use either::Either;
Expand Down Expand Up @@ -112,15 +112,19 @@ pub fn custom_before_pass(
#[cfg(not(target_arch = "wasm32"))]
let relay_plugin = {
if let Some(config) = &opts.relay {
Either::Left(relay::relay(config, file.name.clone(), opts.pages_dir.clone()))
Either::Left(relay::relay(
config,
file.name.clone(),
opts.pages_dir.clone(),
))
} else {
Either::Right(noop())
}
};

chain!(
disallow_re_export_all_in_page::disallow_re_export_all_in_page(opts.is_page_file),
styled_jsx::styled_jsx(cm.clone(), file.name.clone()),
styled_jsx::styled_jsx(cm, file.name.clone()),
hook_optimizer::hook_optimizer(),
match &opts.styled_components {
Some(config) => {
Expand Down Expand Up @@ -173,7 +177,7 @@ impl TransformOptions {
let should_enable_commonjs =
self.swc.config.module.is_none() && fm.src.contains("module.exports") && {
let syntax = self.swc.config.jsc.syntax.unwrap_or_default();
let target = self.swc.config.jsc.target.unwrap_or(EsVersion::latest());
let target = self.swc.config.jsc.target.unwrap_or_else(EsVersion::latest);
let lexer = Lexer::new(syntax, target, StringInput::from(&*fm), None);
let mut p = Parser::new_from(lexer);
p.parse_module()
Expand Down
4 changes: 2 additions & 2 deletions packages/next-swc/crates/core/src/next_dynamic.rs
Expand Up @@ -74,7 +74,7 @@ impl Fold for NextDynamicPatcher {
if let Callee::Expr(i) = &expr.callee {
if let Expr::Ident(identifier) = &**i {
if self.dynamic_bindings.contains(&identifier.to_id()) {
if expr.args.len() == 0 {
if expr.args.is_empty() {
HANDLER.with(|handler| {
handler
.struct_span_err(
Expand Down Expand Up @@ -116,7 +116,7 @@ impl Fold for NextDynamicPatcher {
expr.args[0].expr = expr.args[0].expr.clone().fold_with(self);
self.is_next_dynamic_first_arg = false;

if let None = self.dynamically_imported_specifier {
if self.dynamically_imported_specifier.is_none() {
return expr;
}

Expand Down