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 compatibility/arrow-functions-feature lint #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/linter/src/plugin/compatibility/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::definition::PluginDefinition;
use crate::plugin::compatibility::rules::php74::arrow_functions_feature::ArrowFunctionsFeatureRule;
use crate::plugin::compatibility::rules::php74::null_coalesce_assignment_feature::NullCoalesceAssignmentFeatureRule;
use crate::plugin::compatibility::rules::php80::named_arguments_feature::NamedArgumentsFeatureRule;
use crate::plugin::compatibility::rules::php80::promoted_properties_feature::PromotedPropertiesFeatureRule;
Expand Down Expand Up @@ -26,6 +27,7 @@ impl Plugin for CompatibilityPlugin {
fn get_rules(&self) -> Vec<Box<dyn Rule>> {
vec![
// PHP 7.4
Box::new(ArrowFunctionsFeatureRule),
Box::new(NullCoalesceAssignmentFeatureRule),
// PHP 8.0
Box::new(NamedArgumentsFeatureRule),
Expand Down
1 change: 1 addition & 0 deletions crates/linter/src/plugin/compatibility/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod php74 {
pub mod arrow_functions_feature;
pub mod null_coalesce_assignment_feature;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use indoc::indoc;

use mago_ast::ast::*;
use mago_php_version::PHPVersion;
use mago_reporting::*;
use mago_span::*;
use mago_walker::Walker;

use crate::context::LintContext;
use crate::definition::RuleDefinition;
use crate::definition::RuleUsageExample;
use crate::rule::Rule;

#[derive(Clone, Copy, Debug)]
pub struct ArrowFunctionsFeatureRule;

impl Rule for ArrowFunctionsFeatureRule {
fn get_definition(&self) -> RuleDefinition {
RuleDefinition::enabled("Arrow Functions Feature", Level::Error)
.with_maximum_supported_php_version(PHPVersion::PHP73)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.with_maximum_supported_php_version(PHPVersion::PHP73)
.with_maximum_supported_php_version(PHPVersion::PHP74)

.with_description(indoc! {"
Flags any usage of the `fn` keyword for arrow functions, which was introduced in PHP 7.4.

In environments running older versions of PHP, you can use the `use` keyword to import variables instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In environments running older versions of PHP, you can use the `use` keyword to import variables instead.
In environments running older versions of PHP, you can use an anonymous function
with `use` keyword anonymous functions combined with the `use` keyword to import variables instead.

"})
.with_example(RuleUsageExample::valid(
"Using the `use` keyword for arrow functions",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Using the `use` keyword for arrow functions",
"Using an anonymous function with `use` keyword",

indoc! {r#"
<?php

$y = 2;

// Works in all PHP versions (pre-7.4 included):
$fn = function ($x) use ($y) {
return $x + $y;
};
"#},
))
.with_example(RuleUsageExample::invalid(
"Using the `fn` keyword for arrow functions",
indoc! {r#"
<?php

$y = 2;

// Only valid in PHP 7.4+:
$fn = fn ($x) => $x + $y;
"#},
))
}
}

impl<'a> Walker<LintContext<'a>> for ArrowFunctionsFeatureRule {
fn walk_in_arrow_function<'ast>(&self, arrow_function: &'ast ArrowFunction, context: &mut LintContext<'a>) {
let issue = Issue::new(
context.level(),
"The `fn` keyword for arrow functions is only available in PHP 7.4 and later.",
)
.with_annotation(
Annotation::primary(arrow_function.span()).with_message("Arrow function uses `fn` keyword."),
);

context.report(issue);
}
}
1 change: 1 addition & 0 deletions crates/php-version/src/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ pub enum Feature {
PropertyHooks,
JsonValidate,
ClosureInConstantExpressions,
ArrowFunctions,
}
3 changes: 2 additions & 1 deletion crates/php-version/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ impl PHPVersion {
Feature::NullCoalesceAssign
| Feature::ParameterContravariance
| Feature::ReturnCovariance
| Feature::PregUnmatchedAsNull => self.0 >= 0x070400,
| Feature::PregUnmatchedAsNull
| Feature::ArrowFunctions => self.0 >= 0x070400,
Feature::NonCapturingCatches
| Feature::NativeUnionTypes
| Feature::LessOverridenParametersWithVariadic
Expand Down