|
| 1 | +import { test } from '../utils' |
| 2 | + |
| 3 | +import { RuleTester } from 'eslint' |
| 4 | + |
| 5 | +const ruleTester = new RuleTester(), |
| 6 | + rule = require('rules/rename-default-import') |
| 7 | + |
| 8 | +ruleTester.run('rename-default-import', rule, { |
| 9 | + valid: [ |
| 10 | + test({ |
| 11 | + code: ` |
| 12 | + import PropTypes from 'prop-types'; |
| 13 | + `, |
| 14 | + options: [], |
| 15 | + }), |
| 16 | + test({ |
| 17 | + code: ` |
| 18 | + import PropTypes from 'prop-types'; |
| 19 | + `, |
| 20 | + options: [{'foo': 'Foo'}], |
| 21 | + }), |
| 22 | + test({ |
| 23 | + code: ` |
| 24 | + import PropTypes from 'prop-types'; |
| 25 | + `, |
| 26 | + options: [{'prop-types': 'PropTypes'}], |
| 27 | + }), |
| 28 | + test({ |
| 29 | + code: ` |
| 30 | + import PropTypes, {Foo} from 'prop-types'; |
| 31 | + `, |
| 32 | + options: [{'prop-types': 'PropTypes'}], |
| 33 | + }), |
| 34 | + test({ |
| 35 | + code: ` |
| 36 | + import {default as PropTypes} from 'prop-types'; |
| 37 | + `, |
| 38 | + options: [{'prop-types': 'PropTypes'}], |
| 39 | + }), |
| 40 | + test({ |
| 41 | + code: ` |
| 42 | + import {Foo} from 'prop-types'; |
| 43 | + `, |
| 44 | + options: [{'prop-types': 'PropTypes'}], |
| 45 | + }), |
| 46 | + ], |
| 47 | + invalid: [ |
| 48 | + test({ |
| 49 | + code: `import propTypes from 'prop-types';`, |
| 50 | + options: [{'prop-types': 'PropTypes'}], |
| 51 | + output: `import PropTypes from 'prop-types';`, |
| 52 | + errors: [{ |
| 53 | + ruleId: 'rename-default-import', |
| 54 | + message: `Default import from 'prop-types' should be aliased to PropTypes, not propTypes`, |
| 55 | + }], |
| 56 | + }), |
| 57 | + test({ |
| 58 | + code: `import propTypes, {B} from 'prop-types';`, |
| 59 | + options: [{'prop-types': 'PropTypes'}], |
| 60 | + output: `import PropTypes, {B} from 'prop-types';`, |
| 61 | + errors: [{ |
| 62 | + ruleId: 'rename-default-import', |
| 63 | + message: `Default import from 'prop-types' should be aliased to PropTypes, not propTypes`, |
| 64 | + }], |
| 65 | + }), |
| 66 | + test({ |
| 67 | + code: `import {default as propTypes} from 'prop-types';`, |
| 68 | + options: [{'prop-types': 'PropTypes'}], |
| 69 | + output: `import {default as PropTypes} from 'prop-types';`, |
| 70 | + errors: [{ |
| 71 | + ruleId: 'rename-default-import', |
| 72 | + message: `Default import from 'prop-types' should be aliased to PropTypes, not propTypes`, |
| 73 | + }], |
| 74 | + }), |
| 75 | + test({ |
| 76 | + code: `import propTypes from 'prop-types';import foo from 'foo';`, |
| 77 | + options: [{'prop-types': 'PropTypes', 'foo': 'Foo'}], |
| 78 | + output: `import PropTypes from 'prop-types';import Foo from 'foo';`, |
| 79 | + errors: [{ |
| 80 | + ruleId: 'rename-default-import', |
| 81 | + message: `Default import from 'prop-types' should be aliased to PropTypes, not propTypes`, |
| 82 | + }, { |
| 83 | + ruleId: 'rename-default-import', |
| 84 | + message: `Default import from 'foo' should be aliased to Foo, not foo`, |
| 85 | + }], |
| 86 | + }), |
| 87 | + test({ |
| 88 | + code: ` |
| 89 | + import propTypes from 'prop-types'; |
| 90 | +
|
| 91 | + const obj = { |
| 92 | + foo: propTypes.string |
| 93 | + } |
| 94 | + `, |
| 95 | + options: [{'prop-types': 'PropTypes'}], |
| 96 | + output: ` |
| 97 | + import PropTypes from 'prop-types'; |
| 98 | +
|
| 99 | + const obj = { |
| 100 | + foo: PropTypes.string |
| 101 | + } |
| 102 | + `, |
| 103 | + errors: [{ |
| 104 | + ruleId: 'rename-default-import', |
| 105 | + message: `Default import from 'prop-types' should be aliased to PropTypes, not propTypes`, |
| 106 | + }, { |
| 107 | + ruleId: 'rename-default-import', |
| 108 | + message: `Using incorrect binding name 'propTypes' instead of PropTypes for default import from package prop-types` |
| 109 | + }], |
| 110 | + }), |
| 111 | + test({ |
| 112 | + code: ` |
| 113 | + import foo from 'bar'; |
| 114 | + const a = foo.foo(); |
| 115 | + const b = bar(foo); |
| 116 | + const c = (foo) => { |
| 117 | + foo(); |
| 118 | + }; |
| 119 | + c(foo) |
| 120 | + const d = (bar) => { |
| 121 | + bar(); |
| 122 | + }; |
| 123 | + d(foo); |
| 124 | + const e = () => { |
| 125 | + foo(); |
| 126 | + }; |
| 127 | + `, |
| 128 | + options: [{'bar': 'Foo'}], |
| 129 | + output: ` |
| 130 | + import Foo from 'bar'; |
| 131 | + const a = Foo.foo(); |
| 132 | + const b = bar(Foo); |
| 133 | + const c = (foo) => { |
| 134 | + foo(); |
| 135 | + }; |
| 136 | + c(Foo) |
| 137 | + const d = (bar) => { |
| 138 | + bar(); |
| 139 | + }; |
| 140 | + d(Foo); |
| 141 | + const e = () => { |
| 142 | + Foo(); |
| 143 | + }; |
| 144 | + `, |
| 145 | + errors: [{ |
| 146 | + ruleId: 'rename-default-import', |
| 147 | + message: `Default import from 'bar' should be aliased to Foo, not foo`, |
| 148 | + }, { |
| 149 | + ruleId: 'rename-default-import', |
| 150 | + message: `Using incorrect binding name 'foo' instead of Foo for default import from package bar`, |
| 151 | + }, { |
| 152 | + ruleId: 'rename-default-import', |
| 153 | + message: `Using incorrect binding name 'foo' instead of Foo for default import from package bar`, |
| 154 | + }, { |
| 155 | + ruleId: 'rename-default-import', |
| 156 | + message: `Using incorrect binding name 'foo' instead of Foo for default import from package bar`, |
| 157 | + }, { |
| 158 | + ruleId: 'rename-default-import', |
| 159 | + message: `Using incorrect binding name 'foo' instead of Foo for default import from package bar`, |
| 160 | + }, { |
| 161 | + ruleId: 'rename-default-import', |
| 162 | + message: `Using incorrect binding name 'foo' instead of Foo for default import from package bar`, |
| 163 | + }] |
| 164 | + }) |
| 165 | + ] |
| 166 | +}) |
0 commit comments