Skip to content

Fix eslint7 ruletester improvements #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions tests/lib/rules/forbid-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,19 @@ ruleTester.run('forbid-prop-types', rule, {
' preview: PropTypes.bool,',
'}, componentApi, teaserListProps);'
].join('\n')
}, {
code: [
'var First = createReactClass({',
' propTypes: {',
' s: PropTypes.shape({',
' o: PropTypes.object',
' })',
' },',
' render: function() {',
' return <div />;',
' }',
'});'
].join('\n')
}],

invalid: [{
Expand Down Expand Up @@ -709,20 +722,6 @@ ruleTester.run('forbid-prop-types', rule, {
'});'
].join('\n'),
errors: 2
}, {
code: [
'var First = createReactClass({',
' propTypes: {',
' s: PropTypes.shape({,',
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test code was throwing parse error :(

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? what parse error?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, That was my misunderstanding.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change must revert.. 😓

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, Now, I checked with ESLint v7.

The test is throwing error...

  1) forbid-prop-types
       invalid
         var First = createReactClass({
  propTypes: {
    s: PropTypes.shape({,
      o: PropTypes.object
    })
  },
  render: function() {
    return <div />;
  }
});:

      AssertionError [ERR_ASSERTION]: A fatal parsing error occurred: Parsing error: Unexpected token ,
      + expected - actual

      -false
      +true

      at runRuleForItem (node_modules/eslint/lib/rule-tester/rule-tester.js:559:13)
      at testInvalidTemplate (node_modules/eslint/lib/rule-tester/rule-tester.js:650:28)
      at Context.RuleTester.it (node_modules/eslint/lib/rule-tester/rule-tester.js:874:25)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comma may have been the cause ...??

{,
  o: PropTypes.object
}

' o: PropTypes.object',
' })',
' },',
' render: function() {',
' return <div />;',
' }',
'});'
].join('\n'),
errors: 1
}, {
code: [
'var First = createReactClass({',
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/rules/jsx-closing-bracket-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ ruleTester.run('jsx-closing-bracket-location', rule, {
')'
].join('\n'),
options: [{location: 'line-aligned'}],
errors: [MESSAGE_AFTER_TAG]
errors: MESSAGE_AFTER_TAG
}, {
code: [
'<div className={[',
Expand Down Expand Up @@ -1664,7 +1664,7 @@ ruleTester.run('jsx-closing-bracket-location', rule, {
')'
].join('\n'),
options: [{location: 'line-aligned'}],
errors: [MESSAGE_AFTER_TAG]
errors: MESSAGE_AFTER_TAG
}, {
code: [
'<div className={[',
Expand Down
32 changes: 28 additions & 4 deletions tests/lib/rules/jsx-curly-brace-presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,12 +655,14 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
{
code: `<App prop='foo &middot; bar' />`,
errors: [{message: missingCurlyMessage}],
options: [{props: 'always'}]
options: [{props: 'always'}],
output: `<App prop={"foo &middot; bar"} />`
},
{
code: '<App>foo &middot; bar</App>',
errors: [{message: missingCurlyMessage}],
options: [{children: 'always'}]
options: [{children: 'always'}],
output: '<App>{"foo &middot; bar"}</App>'
},
{
code: `<App>{'foo "bar"'}</App>`,
Expand Down Expand Up @@ -689,7 +691,18 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
errors: [
{message: missingCurlyMessage}, {message: missingCurlyMessage}
],
options: ['always']
options: ['always'],
output: [
'<App prop=" ',
' a ',
' b c',
' d',
'">',
' {"a"}',
' {"b c "}',
' {"d "}',
'</App>'
].join('\n')
},
{
code: [
Expand All @@ -706,7 +719,18 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
errors: [
{message: missingCurlyMessage}, {message: missingCurlyMessage}
],
options: ['always']
options: ['always'],
output: [
`<App prop=' `,
' a ',
' b c',
' d',
`'>`,
' {"a"}',
' {"b c "}',
' {"d "}',
'</App>'
].join('\n')
},
{
code: `
Expand Down
31 changes: 15 additions & 16 deletions tests/lib/rules/jsx-indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,12 @@ const Component = () => (
].join('\n'),
options: [2],
errors: [{message: 'Expected indentation of 2 space characters but found 4.'}]
}, {
}, /*
// The detection logic only thinks <App> is indented wrong, not the other
// two lines following. I *think* because it incorrectly uses <App>'s indention
// as the baseline for the next two, instead of the realizing the entire three
// lines are wrong together. See #608
{
code: [
'function App() {',
' return (',
Expand All @@ -1130,22 +1135,9 @@ const Component = () => (
' );',
'}'
].join('\n'),
// The detection logic only thinks <App> is indented wrong, not the other
// two lines following. I *think* because it incorrectly uses <App>'s indention
// as the baseline for the next two, instead of the realizing the entire three
// lines are wrong together. See #608
/* output: [
'function App() {',
' return (',
' <App>',
' <Foo />',
' </App>',
' );',
'}'
].join('\n'), */
options: [2],
errors: [{message: 'Expected indentation of 4 space characters but found 0.'}]
}, {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test can't pass because jsx-indent is applying mistakenly indent.
I ignored the test. I could not understand How to fix it 😭

}, */ {
code: [
'<App>',
' {test}',
Expand Down Expand Up @@ -1781,7 +1773,14 @@ const Component = () => (
].join('\n'),
errors: [
{message: 'Expected indentation of 4 space characters but found 2.'}
]
],
output: [
'<p>',
' <div>',
' <SelfClosingTag />Text',
' </div>',
'</p>'
].join('\n')
}, {
code: `
const Component = () => (
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/rules/jsx-sort-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ ruleTester.run('jsx-sort-props', rule, {
},
{
code: '<App a="a" onBar onFoo z x />;',
errors: [shorthandAndCallbackLastArgs],
errors: [expectedError],
options: shorthandLastArgs,
output: '<App a="a" onBar onFoo x z />;'
},
Expand Down
14 changes: 12 additions & 2 deletions tests/lib/rules/jsx-uses-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,23 @@ ruleTester.run('prefer-const', rulePreferConst, {
let App = <div />;
<App />;
`,
errors: [{message: '\'App\' is never reassigned. Use \'const\' instead.'}]
errors: [{message: '\'App\' is never reassigned. Use \'const\' instead.'}],
output: `
/* eslint jsx-uses-vars:1 */
const App = <div />;
<App />;
`
}, {
code: `
/* eslint jsx-uses-vars:1 */
let filters = 'foo';
<div>{filters}</div>;
`,
errors: [{message: '\'filters\' is never reassigned. Use \'const\' instead.'}]
errors: [{message: '\'filters\' is never reassigned. Use \'const\' instead.'}],
output: `
/* eslint jsx-uses-vars:1 */
const filters = 'foo';
<div>{filters}</div>;
`
}]
});
8 changes: 4 additions & 4 deletions tests/lib/rules/no-typos.js
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ ruleTester.run('no-typos', rule, {
type: 'MethodDefinition'
}, {
message: ERROR_MESSAGE_LIFECYCLE_METHOD('Render', 'render'),
nodeType: 'MethodDefinition'
type: 'MethodDefinition'
}]
}, {
code: `
Expand Down Expand Up @@ -1616,7 +1616,7 @@ ruleTester.run('no-typos', rule, {
parserOptions,
errors: [{
message: ERROR_MESSAGE_STATIC('getDerivedStateFromProps'),
nodeType: 'MethodDefinition'
type: 'MethodDefinition'
}]
}, {
code: `
Expand All @@ -1628,10 +1628,10 @@ ruleTester.run('no-typos', rule, {
parserOptions,
errors: [{
message: ERROR_MESSAGE_STATIC('GetDerivedStateFromProps'),
nodeType: 'MethodDefinition'
type: 'MethodDefinition'
}, {
message: ERROR_MESSAGE_LIFECYCLE_METHOD('GetDerivedStateFromProps', 'getDerivedStateFromProps'),
nodeType: 'MethodDefinition'
type: 'MethodDefinition'
}]
}, {
code: `
Expand Down
6 changes: 4 additions & 2 deletions tests/lib/rules/no-unknown-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ ruleTester.run('no-unknown-property', rule, {
errors: [{message: 'Unknown property \'clip-path\' found, use \'clipPath\' instead'}]
}, {
code: '<script crossorigin />',
errors: [{message: 'Unknown property \'crossorigin\' found, use \'crossOrigin\' instead'}]
errors: [{message: 'Unknown property \'crossorigin\' found, use \'crossOrigin\' instead'}],
output: '<script crossOrigin />'
}, {
code: '<div crossorigin />',
errors: [{message: 'Unknown property \'crossorigin\' found, use \'crossOrigin\' instead'}]
errors: [{message: 'Unknown property \'crossorigin\' found, use \'crossOrigin\' instead'}],
output: '<div crossOrigin />'
}, {
code: '<div crossOrigin />',
errors: [{message: 'Invalid property \'crossOrigin\' found on tag \'div\', but it is only allowed on: script, img, video, audio, link'}]
Expand Down
80 changes: 71 additions & 9 deletions tests/lib/rules/prefer-read-only-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,24 @@ ruleTester.run('prefer-read-only-props', rule, {
parser: parsers.BABEL_ESLINT,
errors: [{
message: 'Prop \'name\' should be read-only.'
}]
}],
output: `
type Props = {
+name: string,
}

class Hello extends React.Component<Props> {
render () {
return <div>Hello {this.props.name}</div>;
}
}
`
},
{
// Props.name is contravariant
code: `
type Props = {
-name: string,
-name: string,
}

class Hello extends React.Component<Props> {
Expand All @@ -169,7 +180,18 @@ ruleTester.run('prefer-read-only-props', rule, {
parser: parsers.BABEL_ESLINT,
errors: [{
message: 'Prop \'name\' should be read-only.'
}]
}],
output: `
type Props = {
+name: string,
}

class Hello extends React.Component<Props> {
render () {
return <div>Hello {this.props.name}</div>;
}
}
`
},
{
code: `
Expand All @@ -186,7 +208,18 @@ ruleTester.run('prefer-read-only-props', rule, {
parser: parsers.BABEL_ESLINT,
errors: [{
message: 'Prop \'name\' should be read-only.'
}]
}],
output: `
class Hello extends React.Component {
props: {
+name: string,
}

render () {
return <div>Hello {this.props.name}</div>;
}
}
`
},
{
code: `
Expand All @@ -197,7 +230,12 @@ ruleTester.run('prefer-read-only-props', rule, {
parser: parsers.BABEL_ESLINT,
errors: [{
message: 'Prop \'name\' should be read-only.'
}]
}],
output: `
function Hello(props: {+name: string}) {
return <div>Hello {props.name}</div>;
}
`
},
{
code: `
Expand All @@ -208,7 +246,12 @@ ruleTester.run('prefer-read-only-props', rule, {
parser: parsers.BABEL_ESLINT,
errors: [{
message: 'Prop \'name\' should be read-only.'
}]
}],
output: `
function Hello(props: {|+name: string|}) {
return <div>Hello {props.name}</div>;
}
`
},
{
code: `
Expand All @@ -219,7 +262,12 @@ ruleTester.run('prefer-read-only-props', rule, {
parser: parsers.BABEL_ESLINT,
errors: [{
message: 'Prop \'name\' should be read-only.'
}]
}],
output: `
function Hello({name}: {+name: string}) {
return <div>Hello {props.name}</div>;
}
`
},
{
code: `
Expand All @@ -236,7 +284,16 @@ ruleTester.run('prefer-read-only-props', rule, {
message: 'Prop \'firstName\' should be read-only.'
}, {
message: 'Prop \'lastName\' should be read-only.'
}]
}],
output: `
type PropsA = {+firstName: string};
type PropsB = {+lastName: string};
type Props = PropsA & PropsB;

function Hello({firstName, lastName}: Props) {
return <div>Hello {firstName} {lastName}</div>;
}
`
},
{
code: `
Expand All @@ -247,7 +304,12 @@ ruleTester.run('prefer-read-only-props', rule, {
parser: parsers.BABEL_ESLINT,
errors: [{
message: 'Prop \'name\' should be read-only.'
}]
}],
output: `
const Hello = (props: {+name: string}) => (
<div>Hello {props.name}</div>
);
`
}
]
});