10
10
//------------------------------------------------------------------------------
11
11
12
12
const path = require ( "path" ) ,
13
+ { unIndent } = require ( "../../_utils" ) ,
13
14
rule = require ( "../../../lib/rules/comma-dangle" ) ,
14
15
{ RuleTester } = require ( "../../../lib/rule-tester" ) ;
15
16
@@ -35,6 +36,29 @@ function parser(name) {
35
36
36
37
const ruleTester = new RuleTester ( ) ;
37
38
39
+ ruleTester . defineRule ( "add-named-import" , {
40
+ meta : {
41
+ fixable : "code"
42
+ } ,
43
+ create ( context ) {
44
+ return {
45
+ ImportDeclaration ( node ) {
46
+ const sourceCode = context . getSourceCode ( ) ;
47
+ const closingBrace = sourceCode . getLastToken ( node , token => token . value === "}" ) ;
48
+ const addComma = sourceCode . getTokenBefore ( closingBrace ) . value !== "," ;
49
+
50
+ context . report ( {
51
+ message : "Add I18nManager." ,
52
+ node,
53
+ fix ( fixer ) {
54
+ return fixer . insertTextBefore ( closingBrace , `${ addComma ? "," : "" } I18nManager` ) ;
55
+ }
56
+ } ) ;
57
+ }
58
+ } ;
59
+ }
60
+ } ) ;
61
+
38
62
ruleTester . run ( "comma-dangle" , rule , {
39
63
valid : [
40
64
"var foo = { bar: 'baz' }" ,
@@ -1766,6 +1790,66 @@ let d = 0;export {d,};
1766
1790
output : "foo(a)" ,
1767
1791
parserOptions : { ecmaVersion : 8 } ,
1768
1792
errors : [ { messageId : "unexpected" } ]
1793
+ } ,
1794
+
1795
+ // https://github.com/eslint/eslint/issues/15660
1796
+ {
1797
+ code : unIndent `
1798
+ /*eslint add-named-import:1*/
1799
+ import {
1800
+ StyleSheet,
1801
+ View,
1802
+ TextInput,
1803
+ ImageBackground,
1804
+ Image,
1805
+ TouchableOpacity,
1806
+ SafeAreaView
1807
+ } from 'react-native';
1808
+ ` ,
1809
+ output : unIndent `
1810
+ /*eslint add-named-import:1*/
1811
+ import {
1812
+ StyleSheet,
1813
+ View,
1814
+ TextInput,
1815
+ ImageBackground,
1816
+ Image,
1817
+ TouchableOpacity,
1818
+ SafeAreaView,
1819
+ } from 'react-native';
1820
+ ` ,
1821
+ options : [ { imports : "always-multiline" } ] ,
1822
+ parserOptions : { ecmaVersion : 6 , sourceType : "module" } ,
1823
+ errors : 2
1824
+ } ,
1825
+ {
1826
+ code : unIndent `
1827
+ /*eslint add-named-import:1*/
1828
+ import {
1829
+ StyleSheet,
1830
+ View,
1831
+ TextInput,
1832
+ ImageBackground,
1833
+ Image,
1834
+ TouchableOpacity,
1835
+ SafeAreaView,
1836
+ } from 'react-native';
1837
+ ` ,
1838
+ output : unIndent `
1839
+ /*eslint add-named-import:1*/
1840
+ import {
1841
+ StyleSheet,
1842
+ View,
1843
+ TextInput,
1844
+ ImageBackground,
1845
+ Image,
1846
+ TouchableOpacity,
1847
+ SafeAreaView
1848
+ } from 'react-native';
1849
+ ` ,
1850
+ options : [ { imports : "never" } ] ,
1851
+ parserOptions : { ecmaVersion : 6 , sourceType : "module" } ,
1852
+ errors : 2
1769
1853
}
1770
1854
]
1771
1855
} ) ;
0 commit comments