|
| 1 | +import type { RuleContext } from '@typescript-eslint/utils/ts-eslint' |
| 2 | + |
1 | 3 | import { RuleTester } from '@typescript-eslint/rule-tester'
|
2 |
| -import { afterAll, describe, it } from 'vitest' |
| 4 | +import { afterAll, describe, expect, it } from 'vitest' |
3 | 5 | import { dedent } from 'ts-dedent'
|
4 | 6 |
|
| 7 | +import type { MESSAGE_ID, Options } from '../rules/sort-imports' |
| 8 | + |
5 | 9 | import rule from '../rules/sort-imports'
|
6 | 10 |
|
7 | 11 | let ruleName = 'sort-imports'
|
@@ -155,9 +159,9 @@ describe(ruleName, () => {
|
155 | 159 |
|
156 | 160 | import a from '.'
|
157 | 161 | import h from '../../h'
|
| 162 | + import './style.css' |
158 | 163 | import { j } from '../j'
|
159 | 164 | import { K, L, M } from '../k'
|
160 |
| - import './style.css' |
161 | 165 | `,
|
162 | 166 | options: [
|
163 | 167 | {
|
@@ -248,13 +252,6 @@ describe(ruleName, () => {
|
248 | 252 | right: './style.css',
|
249 | 253 | },
|
250 | 254 | },
|
251 |
| - { |
252 |
| - messageId: 'unexpectedImportsOrder', |
253 |
| - data: { |
254 |
| - left: './style.css', |
255 |
| - right: '../j', |
256 |
| - }, |
257 |
| - }, |
258 | 255 | ],
|
259 | 256 | },
|
260 | 257 | ],
|
@@ -1554,6 +1551,331 @@ describe(ruleName, () => {
|
1554 | 1551 | },
|
1555 | 1552 | )
|
1556 | 1553 |
|
| 1554 | + describe(`${ruleName}(${type}): disabling side-effect sorting`, () => { |
| 1555 | + ruleTester.run( |
| 1556 | + `${ruleName}(${type}): allows 'side-effect' and 'side-effect-style' groups to stay in place`, |
| 1557 | + rule, |
| 1558 | + { |
| 1559 | + valid: [], |
| 1560 | + invalid: [ |
| 1561 | + { |
| 1562 | + code: dedent` |
| 1563 | + import "./z-side-effect.scss"; |
| 1564 | + import b from "./b"; |
| 1565 | + import './b-side-effect' |
| 1566 | + import "./g-side-effect.css"; |
| 1567 | + import './a-side-effect' |
| 1568 | + import a from "./a"; |
| 1569 | + `, |
| 1570 | + output: dedent` |
| 1571 | + import "./z-side-effect.scss"; |
| 1572 | + import a from "./a"; |
| 1573 | + import './b-side-effect' |
| 1574 | + import "./g-side-effect.css"; |
| 1575 | + import './a-side-effect' |
| 1576 | + import b from "./b"; |
| 1577 | + `, |
| 1578 | + options: [ |
| 1579 | + { |
| 1580 | + ...options, |
| 1581 | + newlinesBetween: 'always', |
| 1582 | + groups: ['unknown'], |
| 1583 | + }, |
| 1584 | + ], |
| 1585 | + errors: [ |
| 1586 | + { |
| 1587 | + messageId: 'unexpectedImportsOrder', |
| 1588 | + data: { |
| 1589 | + left: './b', |
| 1590 | + right: './b-side-effect', |
| 1591 | + }, |
| 1592 | + }, |
| 1593 | + { |
| 1594 | + messageId: 'unexpectedImportsOrder', |
| 1595 | + data: { |
| 1596 | + left: './a-side-effect', |
| 1597 | + right: './a', |
| 1598 | + }, |
| 1599 | + }, |
| 1600 | + ], |
| 1601 | + }, |
| 1602 | + ], |
| 1603 | + }, |
| 1604 | + ) |
| 1605 | + |
| 1606 | + ruleTester.run( |
| 1607 | + `${ruleName}(${type}): allows 'side-effect' to be grouped together but not sorted`, |
| 1608 | + rule, |
| 1609 | + { |
| 1610 | + valid: [], |
| 1611 | + invalid: [ |
| 1612 | + { |
| 1613 | + code: dedent` |
| 1614 | + import "./z-side-effect.scss"; |
| 1615 | + import b from "./b"; |
| 1616 | + import './b-side-effect' |
| 1617 | + import "./g-side-effect.css"; |
| 1618 | + import './a-side-effect' |
| 1619 | + import a from "./a"; |
| 1620 | + `, |
| 1621 | + output: dedent` |
| 1622 | + import "./z-side-effect.scss"; |
| 1623 | + import './b-side-effect' |
| 1624 | + import "./g-side-effect.css"; |
| 1625 | + import './a-side-effect' |
| 1626 | +
|
| 1627 | + import a from "./a"; |
| 1628 | + import b from "./b"; |
| 1629 | + `, |
| 1630 | + options: [ |
| 1631 | + { |
| 1632 | + ...options, |
| 1633 | + newlinesBetween: 'always', |
| 1634 | + groups: ['side-effect', 'unknown'], |
| 1635 | + }, |
| 1636 | + ], |
| 1637 | + errors: [ |
| 1638 | + { |
| 1639 | + messageId: 'missedSpacingBetweenImports', |
| 1640 | + data: { |
| 1641 | + left: './z-side-effect.scss', |
| 1642 | + right: './b', |
| 1643 | + }, |
| 1644 | + }, |
| 1645 | + { |
| 1646 | + messageId: 'unexpectedImportsGroupOrder', |
| 1647 | + data: { |
| 1648 | + left: './b', |
| 1649 | + leftGroup: 'unknown', |
| 1650 | + right: './b-side-effect', |
| 1651 | + rightGroup: 'side-effect', |
| 1652 | + }, |
| 1653 | + }, |
| 1654 | + { |
| 1655 | + messageId: 'missedSpacingBetweenImports', |
| 1656 | + data: { |
| 1657 | + left: './a-side-effect', |
| 1658 | + right: './a', |
| 1659 | + }, |
| 1660 | + }, |
| 1661 | + ], |
| 1662 | + }, |
| 1663 | + ], |
| 1664 | + }, |
| 1665 | + ) |
| 1666 | + |
| 1667 | + ruleTester.run( |
| 1668 | + `${ruleName}(${type}): allows 'side-effect' and 'side-effect-style' to be grouped together |
| 1669 | + in the same group but not sorted`, |
| 1670 | + rule, |
| 1671 | + { |
| 1672 | + valid: [], |
| 1673 | + invalid: [ |
| 1674 | + { |
| 1675 | + code: dedent` |
| 1676 | + import "./z-side-effect.scss"; |
| 1677 | + import b from "./b"; |
| 1678 | + import './b-side-effect' |
| 1679 | + import "./g-side-effect.css"; |
| 1680 | + import './a-side-effect' |
| 1681 | + import a from "./a"; |
| 1682 | + `, |
| 1683 | + output: dedent` |
| 1684 | + import "./z-side-effect.scss"; |
| 1685 | + import './b-side-effect' |
| 1686 | + import "./g-side-effect.css"; |
| 1687 | + import './a-side-effect' |
| 1688 | +
|
| 1689 | + import a from "./a"; |
| 1690 | + import b from "./b"; |
| 1691 | + `, |
| 1692 | + options: [ |
| 1693 | + { |
| 1694 | + ...options, |
| 1695 | + newlinesBetween: 'always', |
| 1696 | + groups: [['side-effect', 'side-effect-style'], 'unknown'], |
| 1697 | + }, |
| 1698 | + ], |
| 1699 | + errors: [ |
| 1700 | + { |
| 1701 | + messageId: 'missedSpacingBetweenImports', |
| 1702 | + data: { |
| 1703 | + left: './z-side-effect.scss', |
| 1704 | + right: './b', |
| 1705 | + }, |
| 1706 | + }, |
| 1707 | + { |
| 1708 | + messageId: 'unexpectedImportsGroupOrder', |
| 1709 | + data: { |
| 1710 | + left: './b', |
| 1711 | + leftGroup: 'unknown', |
| 1712 | + right: './b-side-effect', |
| 1713 | + rightGroup: 'side-effect', |
| 1714 | + }, |
| 1715 | + }, |
| 1716 | + { |
| 1717 | + messageId: 'missedSpacingBetweenImports', |
| 1718 | + data: { |
| 1719 | + left: './a-side-effect', |
| 1720 | + right: './a', |
| 1721 | + }, |
| 1722 | + }, |
| 1723 | + ], |
| 1724 | + }, |
| 1725 | + ], |
| 1726 | + }, |
| 1727 | + ) |
| 1728 | + |
| 1729 | + ruleTester.run( |
| 1730 | + `${ruleName}(${type}): allows 'side-effect' and 'side-effect-style' to be grouped together but not sorted`, |
| 1731 | + rule, |
| 1732 | + { |
| 1733 | + valid: [], |
| 1734 | + invalid: [ |
| 1735 | + { |
| 1736 | + code: dedent` |
| 1737 | + import "./z-side-effect.scss"; |
| 1738 | + import b from "./b"; |
| 1739 | + import './b-side-effect' |
| 1740 | + import "./g-side-effect.css"; |
| 1741 | + import './a-side-effect' |
| 1742 | + import a from "./a"; |
| 1743 | + `, |
| 1744 | + output: dedent` |
| 1745 | + import './b-side-effect' |
| 1746 | + import './a-side-effect' |
| 1747 | +
|
| 1748 | + import "./z-side-effect.scss"; |
| 1749 | + import "./g-side-effect.css"; |
| 1750 | +
|
| 1751 | + import a from "./a"; |
| 1752 | + import b from "./b"; |
| 1753 | + `, |
| 1754 | + options: [ |
| 1755 | + { |
| 1756 | + ...options, |
| 1757 | + newlinesBetween: 'always', |
| 1758 | + groups: ['side-effect', 'side-effect-style', 'unknown'], |
| 1759 | + }, |
| 1760 | + ], |
| 1761 | + errors: [ |
| 1762 | + { |
| 1763 | + messageId: 'missedSpacingBetweenImports', |
| 1764 | + data: { |
| 1765 | + left: './z-side-effect.scss', |
| 1766 | + right: './b', |
| 1767 | + }, |
| 1768 | + }, |
| 1769 | + { |
| 1770 | + messageId: 'unexpectedImportsGroupOrder', |
| 1771 | + data: { |
| 1772 | + left: './b', |
| 1773 | + leftGroup: 'unknown', |
| 1774 | + right: './b-side-effect', |
| 1775 | + rightGroup: 'side-effect', |
| 1776 | + }, |
| 1777 | + }, |
| 1778 | + { |
| 1779 | + messageId: 'missedSpacingBetweenImports', |
| 1780 | + data: { |
| 1781 | + left: './b-side-effect', |
| 1782 | + right: './g-side-effect.css', |
| 1783 | + }, |
| 1784 | + }, |
| 1785 | + { |
| 1786 | + messageId: 'unexpectedImportsGroupOrder', |
| 1787 | + data: { |
| 1788 | + left: './g-side-effect.css', |
| 1789 | + leftGroup: 'side-effect-style', |
| 1790 | + right: './a-side-effect', |
| 1791 | + rightGroup: 'side-effect', |
| 1792 | + }, |
| 1793 | + }, |
| 1794 | + { |
| 1795 | + messageId: 'missedSpacingBetweenImports', |
| 1796 | + data: { |
| 1797 | + left: './a-side-effect', |
| 1798 | + right: './a', |
| 1799 | + }, |
| 1800 | + }, |
| 1801 | + ], |
| 1802 | + }, |
| 1803 | + ], |
| 1804 | + }, |
| 1805 | + ) |
| 1806 | + |
| 1807 | + ruleTester.run( |
| 1808 | + `${ruleName}(${type}): allows 'side-effect-style' to be grouped together but not sorted`, |
| 1809 | + rule, |
| 1810 | + { |
| 1811 | + valid: [], |
| 1812 | + invalid: [ |
| 1813 | + { |
| 1814 | + code: dedent` |
| 1815 | + import "./z-side-effect"; |
| 1816 | + import b from "./b"; |
| 1817 | + import './b-side-effect.scss' |
| 1818 | + import "./g-side-effect"; |
| 1819 | + import './a-side-effect.css' |
| 1820 | + import a from "./a"; |
| 1821 | + `, |
| 1822 | + output: dedent` |
| 1823 | + import "./z-side-effect"; |
| 1824 | +
|
| 1825 | + import './b-side-effect.scss' |
| 1826 | + import './a-side-effect.css' |
| 1827 | +
|
| 1828 | + import "./g-side-effect"; |
| 1829 | + import a from "./a"; |
| 1830 | + import b from "./b"; |
| 1831 | + `, |
| 1832 | + options: [ |
| 1833 | + { |
| 1834 | + ...options, |
| 1835 | + newlinesBetween: 'always', |
| 1836 | + groups: ['side-effect-style', 'unknown'], |
| 1837 | + }, |
| 1838 | + ], |
| 1839 | + errors: [ |
| 1840 | + { |
| 1841 | + messageId: 'unexpectedImportsGroupOrder', |
| 1842 | + data: { |
| 1843 | + left: './b', |
| 1844 | + leftGroup: 'unknown', |
| 1845 | + right: './b-side-effect.scss', |
| 1846 | + rightGroup: 'side-effect-style', |
| 1847 | + }, |
| 1848 | + }, |
| 1849 | + { |
| 1850 | + messageId: 'missedSpacingBetweenImports', |
| 1851 | + data: { |
| 1852 | + left: './b-side-effect.scss', |
| 1853 | + right: './g-side-effect', |
| 1854 | + }, |
| 1855 | + }, |
| 1856 | + { |
| 1857 | + messageId: 'unexpectedImportsGroupOrder', |
| 1858 | + data: { |
| 1859 | + left: './g-side-effect', |
| 1860 | + leftGroup: 'unknown', |
| 1861 | + right: './a-side-effect.css', |
| 1862 | + rightGroup: 'side-effect-style', |
| 1863 | + }, |
| 1864 | + }, |
| 1865 | + { |
| 1866 | + messageId: 'missedSpacingBetweenImports', |
| 1867 | + data: { |
| 1868 | + left: './a-side-effect.css', |
| 1869 | + right: './a', |
| 1870 | + }, |
| 1871 | + }, |
| 1872 | + ], |
| 1873 | + }, |
| 1874 | + ], |
| 1875 | + }, |
| 1876 | + ) |
| 1877 | + }) |
| 1878 | + |
1557 | 1879 | ruleTester.run(
|
1558 | 1880 | `${ruleName}(${type}): allows to trim special characters`,
|
1559 | 1881 | rule,
|
@@ -1739,9 +2061,9 @@ describe(ruleName, () => {
|
1739 | 2061 |
|
1740 | 2062 | import a from '.'
|
1741 | 2063 | import h from '../../h'
|
| 2064 | + import './style.css' |
1742 | 2065 | import { j } from '../j'
|
1743 | 2066 | import { K, L, M } from '../k'
|
1744 |
| - import './style.css' |
1745 | 2067 | `,
|
1746 | 2068 | options: [
|
1747 | 2069 | {
|
@@ -1832,13 +2154,6 @@ describe(ruleName, () => {
|
1832 | 2154 | right: './style.css',
|
1833 | 2155 | },
|
1834 | 2156 | },
|
1835 |
| - { |
1836 |
| - messageId: 'unexpectedImportsOrder', |
1837 |
| - data: { |
1838 |
| - left: './style.css', |
1839 |
| - right: '../j', |
1840 |
| - }, |
1841 |
| - }, |
1842 | 2157 | ],
|
1843 | 2158 | },
|
1844 | 2159 | ],
|
@@ -3277,8 +3592,8 @@ describe(ruleName, () => {
|
3277 | 3592 |
|
3278 | 3593 | import { K, L, M } from '../k'
|
3279 | 3594 | import { j } from '../j'
|
3280 |
| - import h from '../../h' |
3281 | 3595 | import './style.css'
|
| 3596 | + import h from '../../h' |
3282 | 3597 | import a from '.'
|
3283 | 3598 | `,
|
3284 | 3599 | options: [
|
@@ -4953,6 +5268,15 @@ describe(ruleName, () => {
|
4953 | 5268 | right: '~/data',
|
4954 | 5269 | },
|
4955 | 5270 | },
|
| 5271 | + { |
| 5272 | + messageId: 'unexpectedImportsGroupOrder', |
| 5273 | + data: { |
| 5274 | + left: '~/data', |
| 5275 | + leftGroup: 'side-effect', |
| 5276 | + right: '~/css/globals.css', |
| 5277 | + rightGroup: 'side-effect-style', |
| 5278 | + }, |
| 5279 | + }, |
4956 | 5280 | ],
|
4957 | 5281 | },
|
4958 | 5282 | ],
|
@@ -5218,6 +5542,59 @@ describe(ruleName, () => {
|
5218 | 5542 | invalid: [],
|
5219 | 5543 | })
|
5220 | 5544 |
|
| 5545 | + describe(`${ruleName}: checks compatibility between 'sortSideEffects' and 'groups'`, () => { |
| 5546 | + let createRule = ( |
| 5547 | + groups: Options<string[]>[0]['groups'], |
| 5548 | + sortSideEffects: boolean = false, |
| 5549 | + ) => |
| 5550 | + rule.create({ |
| 5551 | + options: [ |
| 5552 | + { |
| 5553 | + groups, |
| 5554 | + sortSideEffects, |
| 5555 | + }, |
| 5556 | + ], |
| 5557 | + } as Readonly<RuleContext<MESSAGE_ID, Options<string[]>>>) |
| 5558 | + let expectedThrownError = |
| 5559 | + "Side effect groups cannot be nested with non side effect groups when 'sortSideEffects' is 'false'." |
| 5560 | + |
| 5561 | + it(`${ruleName}: throws if 'sideEffects' is in a non side effects only nested group`, () => { |
| 5562 | + expect(() => |
| 5563 | + createRule(['external', ['side-effect', 'internal']]), |
| 5564 | + ).toThrow(expectedThrownError) |
| 5565 | + }) |
| 5566 | + |
| 5567 | + it(`${ruleName}: throws if 'sideEffectsStyle' is in a non side effects only nested group`, () => { |
| 5568 | + expect(() => |
| 5569 | + createRule(['external', ['side-effect-style', 'internal']]), |
| 5570 | + ).toThrow(expectedThrownError) |
| 5571 | + }) |
| 5572 | + |
| 5573 | + it(`${ruleName}: throws if 'sideEffectsStyle' and 'sideEffectsStyle' are in a non side effects only nested group`, () => { |
| 5574 | + expect(() => |
| 5575 | + createRule([ |
| 5576 | + 'external', |
| 5577 | + ['side-effect-style', 'internal', 'side-effect'], |
| 5578 | + ]), |
| 5579 | + ).toThrow(expectedThrownError) |
| 5580 | + }) |
| 5581 | + |
| 5582 | + it(`${ruleName}: allows 'sideEffects' and 'sideEffectsStyle' in the same group`, () => { |
| 5583 | + expect(() => |
| 5584 | + createRule(['external', ['side-effect-style', 'side-effect']]), |
| 5585 | + ).not.toThrow(expectedThrownError) |
| 5586 | + }) |
| 5587 | + |
| 5588 | + it(`${ruleName}: allows 'sideEffects' and 'sideEffectsStyle' anywhere 'sortSideEffects' is true`, () => { |
| 5589 | + expect(() => |
| 5590 | + createRule( |
| 5591 | + ['external', ['side-effect-style', 'internal', 'side-effect']], |
| 5592 | + true, |
| 5593 | + ), |
| 5594 | + ).not.toThrow(expectedThrownError) |
| 5595 | + }) |
| 5596 | + }) |
| 5597 | + |
5221 | 5598 | describe(`${ruleName}: allows to use regex matcher`, () => {
|
5222 | 5599 | let options = {
|
5223 | 5600 | type: 'alphabetical',
|
|
0 commit comments