-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Expensify:main' into add-eslint-rule-for-validating-lef…
…t-side-condition
- Loading branch information
Showing
6 changed files
with
183 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
eslint-plugin-expensify/prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
const {AST_NODE_TYPES} = require("@typescript-eslint/utils"); | ||
const _ = require("underscore"); | ||
const CONST = require("./CONST"); | ||
|
||
module.exports = { | ||
meta: { | ||
type: "problem", | ||
Check failure on line 7 in eslint-plugin-expensify/prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth.js
|
||
docs: { | ||
description: | ||
"Warn against using isSmallScreenWidth from useResponsiveLayout and suggest using shouldUseNarrowLayout instead.", | ||
}, | ||
schema: [], | ||
}, | ||
create(context) { | ||
|
||
const sourceCode = context.sourceCode ?? context.getSourceCode(); | ||
|
||
return { | ||
VariableDeclarator(node) { | ||
if ( | ||
!node.init || | ||
!node.init.callee || | ||
node.init.callee.name !== "useResponsiveLayout" | ||
) { | ||
return; | ||
} | ||
|
||
// Check for 'const {isSmallScreenWidth, ...} = useResponsiveLayout();' pattern | ||
if (node.id.type === AST_NODE_TYPES.ObjectPattern) { | ||
node.id.properties.forEach((property) => { | ||
if (!property.key || property.key.name !== "isSmallScreenWidth") { | ||
return; | ||
} | ||
context.report({ | ||
node: property, | ||
message: | ||
CONST.MESSAGE | ||
.PREFER_SHOULD_USE_NARROW_LAYOUT_INSTEAD_OF_IS_SMALL_SCREEN_WIDTH, | ||
}); | ||
}); | ||
} | ||
|
||
const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope(); | ||
|
||
// Check for 'const var = useResponsiveLayout();' and use of this var | ||
const variableName = node.id.name; | ||
const variableUsages = _.filter( | ||
scope.references, | ||
(reference) => reference.identifier.name === variableName | ||
); | ||
variableUsages.forEach((usage) => { | ||
const parent = usage.identifier.parent; | ||
|
||
// Check for 'const isSmallScreenWidth = var.isSmallScreenWidth;' pattern | ||
if ( | ||
parent.type === AST_NODE_TYPES.MemberExpression && | ||
parent.property.name === "isSmallScreenWidth" | ||
) { | ||
context.report({ | ||
node: parent.property, | ||
message: | ||
CONST.MESSAGE | ||
.PREFER_SHOULD_USE_NARROW_LAYOUT_INSTEAD_OF_IS_SMALL_SCREEN_WIDTH, | ||
}); | ||
} | ||
|
||
// Check for 'const {isSmallScreenWidth} = var;' pattern | ||
if ( | ||
parent.type === AST_NODE_TYPES.VariableDeclarator && | ||
parent.id.type === AST_NODE_TYPES.ObjectPattern | ||
) { | ||
parent.id.properties.forEach((property) => { | ||
if (!property.key || property.key.name !== "isSmallScreenWidth") { | ||
return; | ||
} | ||
context.report({ | ||
node: property, | ||
message: | ||
CONST.MESSAGE | ||
.PREFER_SHOULD_USE_NARROW_LAYOUT_INSTEAD_OF_IS_SMALL_SCREEN_WIDTH, | ||
}); | ||
}); | ||
} | ||
}); | ||
}, | ||
MemberExpression(node) { | ||
// Check for 'const isSmallScreenWidth = useResponsiveLayout().isSmallScreenWidth;' pattern | ||
if ( | ||
node.object.type !== "CallExpression" || | ||
node.object.callee.name !== "useResponsiveLayout" || | ||
node.property.name !== "isSmallScreenWidth" | ||
) { | ||
return; | ||
} | ||
context.report({ | ||
node, | ||
message: | ||
CONST.MESSAGE | ||
.PREFER_SHOULD_USE_NARROW_LAYOUT_INSTEAD_OF_IS_SMALL_SCREEN_WIDTH, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
74 changes: 74 additions & 0 deletions
74
...plugin-expensify/tests/prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const RuleTester = require("eslint").RuleTester; | ||
const rule = require("../prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth"); | ||
const message = | ||
require("../CONST").MESSAGE | ||
.PREFER_SHOULD_USE_NARROW_LAYOUT_INSTEAD_OF_IS_SMALL_SCREEN_WIDTH; | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
sourceType: "module", | ||
}, | ||
}); | ||
|
||
ruleTester.run( | ||
"prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth", | ||
rule, | ||
{ | ||
valid: [ | ||
{ | ||
code: "const {shouldUseNarrowLayout} = useResponsiveLayout();", | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: "const {isSmallScreenWidth} = useResponsiveLayout();", | ||
errors: [ | ||
{ | ||
message, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: "const {isSmallScreenWidth, shouldUseNarrowLayout} = useResponsiveLayout();", | ||
errors: [ | ||
{ | ||
message, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
const isSmallScreenWidth = useResponsiveLayout().isSmallScreenWidth; | ||
`, | ||
errors: [ | ||
{ | ||
message, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
const dimensions = useResponsiveLayout(); | ||
const isSmallScreenWidth = dimensions.isSmallScreenWidth; | ||
`, | ||
errors: [ | ||
{ | ||
message, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
const dimensions = useResponsiveLayout(); | ||
const {isSmallScreenWidth} = dimensions; | ||
`, | ||
errors: [ | ||
{ | ||
message, | ||
}, | ||
], | ||
}, | ||
], | ||
} | ||
); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters