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

html-quotes single should allow double when single quote is within it #874

Closed
rightaway opened this issue Apr 14, 2019 · 5 comments · Fixed by #1031
Closed

html-quotes single should allow double when single quote is within it #874

rightaway opened this issue Apr 14, 2019 · 5 comments · Fixed by #1031

Comments

@rightaway
Copy link

What did you do?

<component attr="contains single quote ' in attr" />

What did you expect to happen?
It should be allowed even though single is set for html-quotes. This is how the quoting functionality works in core eslint for javascript. If you set single quotes you can still use double quotes if the string contains a single quote. This should be no different.

What actually happened?

It gave an eslint error.

@RiqueBR
Copy link

RiqueBR commented Jun 14, 2019

Same problem here. Had to stop using this rule o avoid any big issues when using lint on save

@ota-meshi
Copy link
Member

Thank you for this issue.

I think that, like quotes, we need to add an "avoidEscape" option to the html-quotes rule.

@zdm
Copy link

zdm commented Jan 5, 2020

attr='{"a":"b"}'

become

attr="{&quot;a&quot;:&quot;b&quot;}"

that is completely unreadable.
You really need to keep single quotes and avoid escaping;

@zdm
Copy link

zdm commented Jan 12, 2020

@ota-meshi
I have other idea, that can be useful and will make life easier.
If attribute content starts with the { rule can try to treat content as json, quote (parse/stringify) it correctly and wrap into single quotes.
Example:

attr="{a   :   'aaa', b   :   123}"

should be fixed to:

attr='{"a":"aaa","b":123}'

If json after stringification contains ' - it should be html-escaped.

@zdm
Copy link

zdm commented Jan 13, 2020

Sorry, I have no time to make pull request.
Here is the working rule.
New option "auto" added. It select best quotes automatically, to minimize number of escaped quotes.
Also, It tries to correctly process JSON in attributes,
It requires json5 as dependency.

"use strict";

const JSON5 = require( "json5" );
const utils = require( "eslint-plugin-vue/lib/utils" );

const quoteName = {
    '"': "double quotes",
    "'": "single quotes",
};

module.exports = {
    "meta": {
        "type": "layout",
        "docs": {
            "description": "enforce quotes style of HTML attributes",
            "category": "recommended",
            "url": "https://bitbucket.org/softvisio/softvisio-eslint-plugin",
        },
        "fixable": "code",
        "schema": [{ "enum": ["auto", "double", "single"] }],
    },

    create ( context ) {
        const sourceCode = context.getSourceCode();
        let hasInvalidEOF;

        return utils.defineTemplateBodyVisitor( context,
            {
                "VAttribute[value!=null]" ( node ) {
                    if ( hasInvalidEOF ) {
                        return;
                    }

                    const text = sourceCode.getText( node.value );
                    const firstChar = text[0];

                    var quote, dequoted, newQuote;

                    if ( firstChar === "'" || firstChar === '"' ) {
                        quote = firstChar;

                        dequoted = text.slice( 1, -1 );
                    }
                    else {
                        dequoted = text;
                    }

                    // unescape
                    dequoted = dequoted.replace( /&quot;/g, '"' );
                    dequoted = dequoted.replace( /&apos;/g, "'" );

                    if ( dequoted[0] === "{" ) {
                        try {
                            const json = JSON.stringify( JSON5.parse( dequoted ) );

                            if ( dequoted !== json ) {
                                newQuote = "'";

                                dequoted = json;
                            }
                        }
                        catch ( e ) {
                            //
                        }
                    }

                    if ( !newQuote ) {
                        const mode = context.options[0];

                        if ( mode === "double" ) {
                            newQuote = '"';
                        }
                        else if ( mode === "single" ) {
                            newQuote = "'";
                        }
                        else {
                            const countDouble = ( dequoted.match( /"/g ) || [] ).length;
                            const countSingle = ( dequoted.match( /'/g ) || [] ).length;

                            if ( countDouble <= countSingle ) {
                                newQuote = '"';
                            }
                            else {
                                newQuote = "'";
                            }
                        }
                    }

                    if ( quote !== newQuote ) {
                        context.report( {
                            "node": node.value,
                            "loc": node.value.loc,
                            "message": "Expected to be enclosed by {{kind}}.",
                            "data": { "kind": quoteName[newQuote] },
                            fix ( fixer ) {
                                let replacement;

                                if ( newQuote === '"' ) {
                                    replacement = '"' + dequoted.replace( /"/g, "&quot;" ) + '"';
                                }
                                else {
                                    replacement = "'" + dequoted.replace( /'/g, "&apos;" ) + "'";
                                }

                                return fixer.replaceText( node.value, replacement );
                            },
                        } );
                    }
                },
            },
            {
                Program ( node ) {
                    hasInvalidEOF = utils.hasInvalidEOF( node );
                },
            } );
    },
}; 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants