coderaiser / putout

🐊 Pluggable and configurable JavaScript Linter, code transformer and formatter, drop-in ESLint superpower replacement 💪 with built-in support for js, jsx, typescript, flow, markdown, yaml and json. Write declarative codemods in a simplest possible way 😏
https://putout.cloudcmd.io/
MIT License
698 stars 40 forks source link

Double quotes to single quotes conversion creates syntax error #192

Closed MurkyMeow closed 10 months ago

MurkyMeow commented 10 months ago
// index.js
const putout = require("putout");

const out = putout(`
import React from "react";

export function MyComponent(props) {
    return (
        <button title={"Reach out to us, and we'll discuss your specific moving needs to provide tailored solutions."} />
    );
}
`);

console.log(out);
node index.js

Outputs this code:

import React from 'react';

export function MyComponent(props) {
    return (
        <button title={'Reach out to us, and we'll discuss your specific moving needs to provide tailored solutions.'}/>
    );
}

Which is obviously broken as it now has an unmatched single quote

MurkyMeow commented 10 months ago

I'm using the eslint plugin and i don't get how to configure the putout's printer in this case. Creating a putout.json doesn't help.

coderaiser commented 10 months ago

Just fixed in @putout/printer@6.3.0 🎉 . Please re-install 🐊Putout. Is it works for you?

Configuration file name starts from . is .putout.json, and you can override any options, for example to use double quotes set:

{
    "printer": ["putout", {
        "format": {
            "quote": "\""
        },
        "semantics": {
            "encodeSingleQuote": false
        }
    }]
}

Or you can set recast as a printer (it is slower and not maintained, but tries to preserve style of code):

{
    "printer": "recast"
}
gggscript commented 9 months ago

@coderaiser, I think this problem still exists.

POC for putout CLI tool / putout's astexplorer website: Tested with Putout version: 33.12.0 (CLI tool) and 33.10.0 (website)

alert("This is text \\' but this will be code")
// ->
alert('This is text \\' but this will be code');

https://putout.cloudcmd.io/#/gist/fa19342acc9f7fb0fea2d266e42c31e2/ca2fa2781c5dfac46ed9bf8ace4d63444e9db97b

POC for @putout/printer package: Tested with version 6.4.1

const printer = require('@putout/printer')
const { parse } = require('@babel/parser')

const input = `alert("'")`
alert({ actual: printer.print(parse(input)) })
// -> { actual: "alert(''');\n" }