azat-io / eslint-plugin-perfectionist

🦄 ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.
https://eslint-plugin-perfectionist.azat.io
MIT License
1.62k stars 28 forks source link

Bug: (sort-objects): How to sort vue options(data, computed, watch, methods), how to ignore child sorting in methods? #125

Closed KID-joker closed 1 month ago

KID-joker commented 1 month ago

Describe the bug

I just want to sort the order of vue options, not the methods object. Because nearby methods may handle related logic.

Code example

eslint.config.js:

'perfectionist/sort-objects': [
    'error',
    {
        groups: [
            'name',
            'data',
            'methods',
            'unknown'
        ],
        'custom-groups': {
            name: 'name',
            data: 'data',
            methods: 'methods'
        },
        'ignore-pattern': ['data', 'methods']
    }
]

input:

export default {
    methods: {
        getName() {
            return this.name;
        },
        getSoftware() {
            return this.software;
        },
        getPrice() {
            return this.price;
        }
    },
    data() {
        return {
            name: 'iPhone 14 Pro',
            software: 'iOS',
            price: 1199
        }
    }
}

output:

export default {
    data() {
        return {
            name: 'iPhone 14 Pro',
            price: 1199,
            software: 'iOS'
        }
    },
    methods: {
        getName() {
            return this.name;
        },
        getPrice() {
            return this.price;
        },
        getSoftware() {
            return this.software;
        }
    }
}

expect:

export default {
    data() {
        return {
            name: 'iPhone 14 Pro',
            software: 'iOS',
            price: 1199
        }
    },
    methods: {
        getName() {
            return this.name;
        },
        getSoftware() {
            return this.software;
        },
        getPrice() {
            return this.price;
        }
    }
}

ESLint version

v8.56.0

ESLint Plugin Perfectionist version

v2.8.0

Additional comments

No response

Validations

KID-joker commented 1 month ago

add node.parent.type === 'Property' && node.parent.key.type === 'Identifier' ? node.parent.key.name : null can solve the methods problem. And the data should find the grandparent whose type is VariableDeclarator or Property.

KID-joker commented 1 month ago

Maybe I can try to submit a pr?