BenoitZugmeyer / eslint-plugin-html

An ESLint plugin to extract and lint scripts from HTML files.
ISC License
430 stars 51 forks source link

Incorrect errors #108

Closed kkmuffme closed 5 years ago

kkmuffme commented 5 years ago

I have this code:

function valueAddedToCart( final_result ) {
        dataLayer.push({
            'event': 'addToCart',
            'eventCategory': 'Ecommerce',
            'eventAction': 'Add to Cart',
            'eventLabel': 'Click Add to Cart Product Page',
            'eventValue': undefined,
            'currencyCode': '<?php echo get_woocommerce_currency(); ?>',
            'ecommerce': {
                'currencyCode': '<?php echo get_woocommerce_currency(); ?>',
                'add': {
                    'products': final_result
                }
            }
        });
    <?php
    $fbpixelid = get_option('wc_settings_tag_manager_fbpixelid');
    //this script is use to facebook pixel id
    if (isset($fbpixelid) && !empty($fbpixelid)) { ?>
        //adding fbq value on add to cart button click
        fbq( 'track', 'AddToCart', {
            'content_ids': '<?php echo $product_id; ?>',
            'content_type': 'product',
            'value': '<?php echo $product_price; ?>',
            'currency': '<?php echo get_woocommerce_currency(); ?>'
        });
    <?php } ?>
    } // this is line 1040

Gives these errors which are completely wrong:

1028:2 error Expected indentation of 1 tab but found 0 indent
1028:2 error Expected an assignment or function call and instead saw an expression no-unused-expressions
1032:2 error Missing semicolon semi
1039:2 error Expected an assignment or function call and instead saw an expression no-unused-expressions
1039:2 error Expected indentation of 1 tab but found 0 indent
1040:2 error Requires a space before '}' block-spacing
1040:2 error Closing curly brace should be on the same line as opening curly brace or on the line after the previous block brace-style
1040:2 error Missing semicolon semi

Setup configuration

{
    "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"
        ]
    }
}
BenoitZugmeyer commented 5 years ago

This plugin don't support templating languages (as stated in the README). The <?php "tag" is considered as malformed JS code. What you could do is something like this:

var contentInfos;
// <?php echo "\ncontentInfos = " . json_encode(get_content_infos()); ?>
if (contentInfos) {
    //adding fbq value on add to cart button click
    fbq( 'track', 'AddToCart', contentInfos);
}
function get_content_infos() {
    $fbpixelid = get_option('wc_settings_tag_manager_fbpixelid');
    //this script is use to facebook pixel id
    if (isset($fbpixelid) && !empty($fbpixelid)) {
        return [
            'content_ids' => $product_id,
            'content_type' => 'product',
            'value' => $product_price,
            'currency' => get_woocommerce_currency(),
        ]
    } else {
        return null
    }
}
kkmuffme commented 5 years ago

But I am using plugin php-markup ( https://www.npmjs.com/package/eslint-plugin-php-markup ) which converts all <? ... ?> to 0, so there shouldn't be an issue with that? Or is this a bug with the php-markup plugin? (then I should probably open a issue here https://github.com/tengattack/eslint-plugin-php-markup/issues )

BenoitZugmeyer commented 5 years ago

Allright I didn't know about php-markup, thanks for pointing this out. So php-markup is replacing every php parts to '0', so basically:

function valueAddedToCart( final_result ) {
        dataLayer.push({
            'event': 'addToCart',
            'eventCategory': 'Ecommerce',
            'eventAction': 'Add to Cart',
            'eventLabel': 'Click Add to Cart Product Page',
            'eventValue': undefined,
            'currencyCode': '0',
            'ecommerce': {
                'currencyCode': '0',
                'add': {
                    'products': final_result
                }
            }
        });
    0
        //adding fbq value on add to cart button click
        fbq( 'track', 'AddToCart', {
            'content_ids': '0',
            'content_type': 'product',
            'value': '0',
            'currency': '0'
        });
    0
    } // this is line 1040

The line 1028 is indeed under-indented and is an unused expression, and lacks an ending semi colon. Same goes for the line 1039.

kkmuffme commented 5 years ago

Thanks, now its clear. Already opened an issue in the other repo for it with a suggestion on how to fix it.