tengattack / eslint-plugin-php-markup

A eslint plugin to process PHP markup
8 stars 2 forks source link

Removing the line instead of changing to 0 #1

Closed kkmuffme closed 5 years ago

kkmuffme commented 5 years ago

Hey,

would it be possible to remove things within <?php ... ?> completely instead of changing them to 0 if the the <?php is preceeded by whitespace or newline?

e.g.

function valueAddedToCart( final_result ) {
    <?php 
    if ( $someVar == $otherVar ) { ?>
        fbq( 'track', 'AddToCart', {
            'content_ids': '<?php echo $product_id; ?>',
        });
    <?php } ?>
}

here line 2 & 7 will throw errors in eslint (for many rules) bc the php-markup changes them to 0 like:

function valueAddedToCart( final_result ) {
    0
        fbq( 'track', 'AddToCart', {
            'content_ids': '0',
        });
    0
}

if instead it would only change things in <?php ... ?> to 0 that are NOT preceeded by whitespace/newline it would look like:

function valueAddedToCart( final_result ) {
        fbq( 'track', 'AddToCart', {
            'content_ids': '0',
        });
}

which then would give correct results

tengattack commented 5 years ago

Which version of eslint and eslint-plugin-html do you use? BTW, could you provide your .eslintrc file?

tengattack commented 5 years ago

now https://github.com/tengattack/eslint-plugin-php-markup/commit/944053167491bcf1c0e285d98e99040633357f8d can do that through eslintrc's settings:

{
  // ...
  "settings": {
    "php/markup-replacement": {"php": "", "=": "0"},
  },
  // ...
}
kkmuffme commented 5 years ago

Thanks for the reply & suggested quick fix.

However, the issue is a general issue, so I think it would be great to have the whitespace option I suggested. The reason is that e.g. lots of (open source) software (e.g. Wordpress + plugins for example) do not allow the use of php short tags. Furthermore it means rewriting loads of code that use normal tags.

By checking (or providing a option "remove-php-after-newline-or-whitespace" or something like this, however you want to name it) for newline/whitespace before the <?php tag and then replacing it with blank (incl. trailing whitespace/newline after ?>) it could easily reach a perfect check without the need to change the php tags.

Here is my versions/config:

{
    "plugins": [
        "html",
        "php-markup"
    ],
    "settings": {
        "html/indent": "+tab",
        "html/report-bad-indent": "error"
    },
    "env": {
        "browser": true,
        "worker": true,
        "phantomjs": true,
        "qunit": true,
        "jquery": true,
        "serviceworker": true,
        "es6": true
    },
    "extends": "eslint:recommended",
    "globals": {
        "_": false,
        "e": false,
        "Backbone": false,
        "JSON": false,
        "wp": false,
        "ajax_url": true,
        "ajaxurl": true,
        "fbq": true,
        "dataLayer": true
    },
    "rules": {
        "dot-notation": [
            "error",
            {
                "allowKeywords": true,
                "allowPattern": "^[a-z]+(_[a-z]+)+$"
            }
        ],
        "array-bracket-spacing": [
            "error",
            "always"
        ],
        "linebreak-style": [
            "error",
            "unix"
        ],
        "lines-around-comment": [
            "error",
            {
                "beforeLineComment": true,
                "allowBlockStart": true,
                "allowBlockEnd": true,
                "allowClassStart": true,
                "allowObjectStart": true,
                "allowArrayStart": true,
                "allowArrayEnd": true
            }
        ],
        "one-var-declaration-per-line": [
            "error",
            "initializations"
        ],
        "accessor-pairs": "error",
        "arrow-spacing": [
            "error",
            {
                "before": true,
                "after": true
            }
        ],
        "block-spacing": [
            "error",
            "always"
        ],
        "brace-style": [
            "error",
            "1tbs",
            {
                "allowSingleLine": true
            }
        ],
        "comma-dangle": [
            "error",
            {
                "arrays": "never",
                "objects": "never",
                "imports": "never",
                "exports": "never",
                "functions": "never"
            }
        ],
        "comma-spacing": [
            "error",
            {
                "before": false,
                "after": true
            }
        ],
        "comma-style": [
            "error",
            "last"
        ],
        "curly": [
            "error",
            "all"
        ],
        "dot-location": [
            "error",
            "property"
        ],
        "eol-last": "error",
        "eqeqeq": [
            "error",
            "smart"
        ],
        "func-call-spacing": [
            "error",
            "never"
        ],
        "generator-star-spacing": [
            "error",
            {
                "before": true,
                "after": true
            }
        ],
        "indent": [
            "error",
            "tab"
        ],
        "key-spacing": [
            "error",
            {
                "beforeColon": false,
                "afterColon": true
            }
        ],
        "keyword-spacing": [
            "error",
            {
                "before": true,
                "after": true
            }
        ],
        "no-eval": "error",
        "no-extend-native": "error",
        "no-extra-bind": "error",
        "no-extra-parens": [
            "error",
            "functions"
        ],
        "no-iterator": "error",
        "no-label-var": "error",
        "no-labels": [
            "error",
            {
                "allowLoop": false,
                "allowSwitch": false
            }
        ],
        "no-lone-blocks": "error",
        "no-mixed-operators": [
            "error",
            {
                "groups": [
                    [
                        "==",
                        "!=",
                        "===",
                        "!==",
                        ">",
                        ">=",
                        "<",
                        "<="
                    ],
                    [
                        "&&",
                        "||"
                    ],
                    [
                        "in",
                        "instanceof"
                    ]
                ],
                "allowSamePrecedence": true
            }
        ],
        "no-mixed-spaces-and-tabs": "error",
        "no-multi-spaces": "error",
        "no-multi-str": "error",
        "no-multiple-empty-lines": [
            "error",
            {
                "max": 1,
                "maxEOF": 0
            }
        ],
        "no-new": "error",
        "no-new-func": "error",
        "no-new-object": "error",
        "no-new-require": "error",
        "no-new-symbol": "error",
        "no-new-wrappers": "error",
        "no-octal-escape": "error",
        "no-path-concat": "error",
        "no-proto": "error",
        "no-return-assign": [
            "error",
            "except-parens"
        ],
        "no-return-await": "error",
        "no-self-compare": "error",
        "no-sequences": "error",
        "no-shadow-restricted-names": "error",
        "no-template-curly-in-string": "error",
        "no-throw-literal": "error",
        "no-trailing-spaces": "error",
        "no-undef-init": "error",
        "no-unmodified-loop-condition": "error",
        "no-unneeded-ternary": [
            "error",
            {
                "defaultAssignment": false
            }
        ],
        "no-unused-expressions": [
            "error",
            {
                "allowShortCircuit": true,
                "allowTernary": true,
                "allowTaggedTemplates": true
            }
        ],
        "no-use-before-define": [
            "error",
            {
                "functions": false,
                "classes": false,
                "variables": false
            }
        ],
        "no-useless-call": "error",
        "no-useless-computed-key": "error",
        "no-useless-constructor": "error",
        "no-useless-escape": "error",
        "no-useless-rename": "error",
        "no-useless-return": "error",
        "no-whitespace-before-property": "error",
        "no-with": "error",
        "operator-linebreak": [
            "error",
            "none"
        ],
        "padded-blocks": [
            "error",
            {
                "blocks": "never",
                "switches": "never",
                "classes": "never"
            }
        ],
        "quotes": [
            "error",
            "single",
            {
                "avoidEscape": true,
                "allowTemplateLiterals": true
            }
        ],
        "semi": [
            "error",
            "always"
        ],
        "semi-spacing": [
            "error",
            {
                "before": false,
                "after": true
            }
        ],
        "space-before-blocks": [
            "error",
            "always"
        ],
        "space-before-function-paren": [
            "error",
            "never"
        ],
        "space-in-parens": [
            "error",
            "always",
            {
                "exceptions": [
                    "{}",
                    "[]"
                ]
            }
        ],
        "space-infix-ops": "error",
        "space-unary-ops": [
            "error",
            {
                "words": true,
                "nonwords": false
            }
        ],
        "unicode-bom": [
            "error",
            "never"
        ],
        "wrap-iife": [
            "error",
            "any",
            {
                "functionPrototypeMethods": true
            }
        ],
        "yield-star-spacing": [
            "error",
            "both"
        ],
        "yoda": [
            "error",
            "never"
        ]
    }
}
tengattack commented 5 years ago

Did you mean we should provide an option to remove the entire lines which contain php tag only?

It will remove the CRLF just after PHP tag by default, unless we set keep-eol to true in settings.

And your code will be (keep-eol):

function valueAddedToCart( final_result ) {

        fbq( 'track', 'AddToCart', {
            'content_ids': '',
        });

}

I think it will show errors by rule no-trailing-spaces? So that we have to make an option remove entire empty line after remove the php tag. I will check it later and try to find a way to fix it.

tengattack commented 5 years ago

48ce52d9e76de06265df1b142f801b3d10700b1a 4d3830bcd3f08fcf0f5a89e85a035020ecd2f59b may help you with settings php/remove-whitespace will remove empty line now.

kkmuffme commented 5 years ago

Problem is when using like this with the php "", and = "0" option, that we will get false positive errors. e.g. productListCheckout = <?php echo json_encode($product_detail); ?>;

will trigger: Parsing error: Unexpected token ;

Thats why I suggested the solution with the newline+whitespace. if <?php ...?> is preceeded by ^ (beginning of file) OR newline (\n or \r\n) + whitespace [\t ]{1,} in regex: (^|\n|\r\n)[\t ]{1,}

=> then replace it with empty, else replace it with 0.

tengattack commented 5 years ago

I mean we can change the <?php echo to <?= which is enabled by default since 5.4, so that you can change it to 0 instead of empty string.

kkmuffme commented 5 years ago

I can't, Wordpress doesn't allow the use of <?= unfortunately, thus I cannot use it if I want to get anything published (no matter if its a free plugin I provide via wordpress.org or a paid one)

But I want my code to be the best possible, that's why I use eslint with your plugin.

tengattack commented 5 years ago

I see. I understand your solution, it will be useful. I am considering adding it as an option remove-empty-line so that you can set 'php': '0', '=': '0' to achieve the solution.

tengattack commented 5 years ago

@kkmuffme v0.2.4 support it now!

kkmuffme commented 5 years ago

Perfect works now. Thanks a lot!