Skip to content
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

Add experimental TypeScript option to require-readonly-react-props #526

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions src/rules/requireReadonlyReactProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const schema = [

const reComponentName = /^(Pure)?Component$/u;
const reReadOnly = /^\$(ReadOnly|FlowFixMe)$/u;
const reReadOnlyTypeScript = /^(Readonly|$FlowFixMe)$/u;

const isReactComponent = (node) => {
if (!node.superClass) {
Expand Down Expand Up @@ -69,15 +70,20 @@ const isReadOnlyObjectUnionType = (node, options) => {
});
};

const isReadOnlyName = (name, {useExperimentalTypeScriptSyntax}) => {
return useExperimentalTypeScriptSyntax ? reReadOnlyTypeScript.test(name) : reReadOnly.test(name);
};

const isReadOnlyType = (node, options) => {
return node.right.id && reReadOnly.test(node.right.id.name) ||
return node.right.id && isReadOnlyName(node.right.id.name, options) ||
isReadOnlyObjectType(node.right, options) ||
isReadOnlyObjectUnionType(node.right, options);
};

const create = (context) => {
const useImplicitExactTypes = _.get(context, ['options', 0, 'useImplicitExactTypes'], false);
const options = {useImplicitExactTypes};
const useExperimentalTypeScriptSyntax = _.get(context, ['options', 0, 'useExperimentalTypeScriptSyntax'], false);
const options = {useImplicitExactTypes, useExperimentalTypeScriptSyntax};

const readOnlyTypes = [];
const foundTypes = [];
Expand All @@ -86,7 +92,7 @@ const create = (context) => {
const isReadOnlyClassProp = (node) => {
const id = node.superTypeParameters && node.superTypeParameters.params[0].id;

return id && !reReadOnly.test(id.name) && !readOnlyTypes.includes(id.name) && foundTypes.includes(id.name);
return id && !isReadOnlyName(id.name, options) && !readOnlyTypes.includes(id.name) && foundTypes.includes(id.name);
};

for (const node of context.getSourceCode().ast.body) {
Expand Down Expand Up @@ -153,7 +159,7 @@ const create = (context) => {
if ((identifier = typeAnnotation.typeAnnotation.id) &&
foundTypes.includes(identifier.name) &&
!readOnlyTypes.includes(identifier.name) &&
!reReadOnly.test(identifier.name)) {
!isReadOnlyName(identifier.name, options)) {
if (reportedFunctionalComponents.includes(identifier)) {
return;
}
Expand Down
47 changes: 47 additions & 0 deletions tests/rules/assertions/requireReadonlyReactProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ export default {
},
],
},
{
code: 'type Props = { }; class Foo extends React.Component<Props> { }',
errors: [
{
message: 'Props must be Readonly',
},
],
options: [
{
useExperimentalTypeScriptSyntax: true,
},
],
},
{
code: 'type OtherProps = { foo: string }; class Foo extends React.Component<OtherProps> { }',
errors: [
Expand Down Expand Up @@ -118,6 +131,24 @@ export default {
},
],
},
{
// vvvvv
code: 'type Props = { }; function Foo(props: Props) { return <p /> }',
errors: [
{
column: 39,
endColumn: 44,
endLine: 1,
line: 1,
message: 'Props must be Readonly',
},
],
options: [
{
useExperimentalTypeScriptSyntax: true,
},
],
},
{
code: 'type Props = { }; function Foo(props: Props) { return foo ? <p /> : <span /> }',
errors: [
Expand Down Expand Up @@ -149,6 +180,14 @@ export default {
{
code: 'class Foo extends React.Component<$ReadOnly<{}>> { }',
},
{
code: 'class Foo extends React.Component<Readonly<{}>> { }',
options: [
{
useExperimentalTypeScriptSyntax: true,
},
],
},
{
code: 'type Props = $ReadOnly<{}>; class Foo extends React.Component<Props> { }',
},
Expand Down Expand Up @@ -251,6 +290,14 @@ export default {
{
code: 'type Props = $ReadOnly<{}>; function Foo(props: Props) { }',
},
{
code: 'type Props = Readonly<{}>; function Foo(props: Props) { }',
options: [
{
useExperimentalTypeScriptSyntax: true,
},
],
},
{
code: 'type Props = {}; function Foo(props: OtherProps) { }',
},
Expand Down