-
Notifications
You must be signed in to change notification settings - Fork 27
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
no-default-id-values rule #137
Merged
+346
−0
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
function createPatternRegex(pattern) { | ||
return new RegExp(pattern.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1'), 'g'); | ||
} | ||
|
||
function searchForPatternsAndReport(context, sourceCode, soureCodeStr, pattern, messageId) { | ||
const regex = createPatternRegex(pattern); | ||
let match = regex.exec(soureCodeStr); | ||
while (match !== null) { | ||
const index = match.index; | ||
|
||
const defaultStr = match[0]; | ||
const defaultStrPosition = sourceCode.getLocFromIndex(index); | ||
|
||
context.report({ | ||
messageId, | ||
loc: { | ||
start: {line: defaultStrPosition.line, column: defaultStrPosition.column + defaultStr.indexOf(' ')}, | ||
end: {line: defaultStrPosition.line, column: defaultStrPosition.column + defaultStr.length}, | ||
}, | ||
}); | ||
|
||
match = regex.exec(soureCodeStr); | ||
} | ||
} | ||
|
||
module.exports = { | ||
name: 'no-default-id-values', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Restricts use of default number/string IDs in the project.', | ||
recommended: 'error', | ||
}, | ||
schema: [], | ||
messages: { | ||
// eslint-disable-next-line max-len | ||
disallowedNumberDefault: 'Default the number ID to `CONST.DEFAULT_NUMBER_ID` instead. See: https://github.com/Expensify/App/blob/main/contributingGuides/STYLE.md#default-value-for-inexistent-IDs', | ||
// eslint-disable-next-line max-len | ||
disallowedStringDefault: 'Do not default string IDs to any value. See: https://github.com/Expensify/App/blob/main/contributingGuides/STYLE.md#default-value-for-inexistent-IDs', | ||
}, | ||
}, | ||
create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
const soureCodeStr = sourceCode.text; // This gets all the text in the file | ||
|
||
const disallowedNumberDefaults = [ | ||
'ID ?? -1', | ||
'id ?? -1', | ||
'ID ?? 0', | ||
'id ?? 0', | ||
'ID || -1', | ||
'id || -1', | ||
'ID || 0', | ||
'id || 0', | ||
'ID : -1', | ||
'id : -1', | ||
'ID : 0', | ||
'id : 0', | ||
]; | ||
|
||
const disallowedStringDefaults = [ | ||
" ?? '-1'", | ||
"ID ?? ''", | ||
"id ?? ''", | ||
"ID ?? '0'", | ||
"id ?? '0'", | ||
" || '-1'", | ||
"ID || ''", | ||
"id || ''", | ||
"ID || '0'", | ||
"id || '0'", | ||
" : '-1'", | ||
" : '0'", | ||
]; | ||
|
||
disallowedNumberDefaults.forEach((pattern) => { | ||
searchForPatternsAndReport(context, sourceCode, soureCodeStr, pattern, 'disallowedNumberDefault'); | ||
}); | ||
|
||
disallowedStringDefaults.forEach((pattern) => { | ||
searchForPatternsAndReport(context, sourceCode, soureCodeStr, pattern, 'disallowedStringDefault'); | ||
}); | ||
|
||
return {}; | ||
}, | ||
}; |
260 changes: 260 additions & 0 deletions
260
eslint-plugin-expensify/tests/no-default-id-values.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,260 @@ | ||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../no-default-id-values'); | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: 'module', | ||
}, | ||
}); | ||
|
||
ruleTester.run('no-default-id-values', rule, { | ||
valid: [ | ||
// Number IDs | ||
{ | ||
code: 'const accountID = report?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID;', | ||
}, | ||
{ | ||
code: 'const accountID = account?.id ?? CONST.DEFAULT_NUMBER_ID;', | ||
}, | ||
{ | ||
code: 'currentState = currentState?.routes[currentState.index ?? -1].state;', | ||
}, | ||
{ | ||
code: 'currentState = currentState?.routes[currentState.index ?? 0].state;', | ||
}, | ||
{ | ||
code: 'const accountID = report?.ownerAccountID || CONST.DEFAULT_NUMBER_ID;', | ||
}, | ||
{ | ||
code: 'const accountID = account?.id || CONST.DEFAULT_NUMBER_ID;', | ||
}, | ||
{ | ||
code: 'currentState = currentState?.routes[currentState.index || -1].state;', | ||
}, | ||
{ | ||
code: 'currentState = currentState?.routes[currentState.index || 0].state;', | ||
}, | ||
{ | ||
code: 'const managerID = report ? report.managerID : CONST.DEFAULT_NUMBER_ID;', | ||
}, | ||
{ | ||
code: 'const accountID = account ? account.id : CONST.DEFAULT_NUMBER_ID;', | ||
}, | ||
{ | ||
code: 'options.sort((method) => (method.value === exportMethod ? -1 : 0));', | ||
}, | ||
|
||
// String IDs | ||
{ | ||
code: 'const reportID = report?.reportID;', | ||
}, | ||
{ | ||
code: 'const iconName = icon.name ?? \'\'', | ||
}, | ||
{ | ||
code: 'const index = tempIndex ?? \'0\'', | ||
}, | ||
{ | ||
code: 'const iconName = icon.name || \'\'', | ||
}, | ||
{ | ||
code: 'const index = tempIndex || \'0\'', | ||
}, | ||
], | ||
invalid: [ | ||
// Number IDs | ||
{ | ||
code: 'const accountID = report?.ownerAccountID ?? -1;', | ||
errors: [{ | ||
messageId: 'disallowedNumberDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const reportID = report?.id ?? -1;', | ||
errors: [{ | ||
messageId: 'disallowedNumberDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const accountID = report?.ownerAccountID ?? 0;', | ||
errors: [{ | ||
messageId: 'disallowedNumberDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const reportID = report?.id ?? 0;', | ||
errors: [{ | ||
messageId: 'disallowedNumberDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const accountID = report?.ownerAccountID || -1;', | ||
errors: [{ | ||
messageId: 'disallowedNumberDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const reportID = report?.id || -1;', | ||
errors: [{ | ||
messageId: 'disallowedNumberDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const accountID = report?.ownerAccountID || 0;', | ||
errors: [{ | ||
messageId: 'disallowedNumberDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const reportID = report?.id || 0;', | ||
errors: [{ | ||
messageId: 'disallowedNumberDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const managerID = report ? report.managerID : -1;', | ||
errors: [{ | ||
messageId: 'disallowedNumberDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const accountID = account ? account.id : -1;', | ||
errors: [{ | ||
messageId: 'disallowedNumberDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const managerID = report ? report.managerID : 0;', | ||
errors: [{ | ||
messageId: 'disallowedNumberDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const accountID = account ? account.id : 0;', | ||
errors: [{ | ||
messageId: 'disallowedNumberDefault', | ||
}], | ||
}, | ||
|
||
// String IDs | ||
{ | ||
code: 'const reportID = report?.reportID ?? \'-1\';', | ||
errors: [{ | ||
messageId: 'disallowedStringDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const currentReportID = Navigation.getTopmostReportId() ?? \'-1\';', | ||
errors: [{ | ||
messageId: 'disallowedStringDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const currentReportID = navigationRef?.isReady?.() ? Navigation.getTopmostReportId() ?? \'-1\' : \'-1\';', | ||
errors: [ | ||
{ | ||
messageId: 'disallowedStringDefault', | ||
}, | ||
{ | ||
messageId: 'disallowedStringDefault', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'const reportID = report?.reportID ?? \'-1\';', | ||
errors: [{ | ||
messageId: 'disallowedStringDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const policyID = policy?.id ?? \'-1\';', | ||
errors: [{ | ||
messageId: 'disallowedStringDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const reportID = report?.reportID ?? \'\';', | ||
errors: [{ | ||
messageId: 'disallowedStringDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const policyID = policy?.id ?? \'\';', | ||
errors: [{ | ||
messageId: 'disallowedStringDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const reportID = report?.reportID ?? \'0\';', | ||
errors: [{ | ||
messageId: 'disallowedStringDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const policyID = policy?.id ?? \'0\';', | ||
errors: [{ | ||
messageId: 'disallowedStringDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const reportID = report?.reportID || \'-1\';', | ||
errors: [{ | ||
messageId: 'disallowedStringDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const currentReportID = Navigation.getTopmostReportId() || \'-1\';', | ||
errors: [{ | ||
messageId: 'disallowedStringDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const currentReportID = navigationRef?.isReady?.() ? Navigation.getTopmostReportId() || \'-1\' : \'-1\';', | ||
errors: [ | ||
{ | ||
messageId: 'disallowedStringDefault', | ||
}, | ||
{ | ||
messageId: 'disallowedStringDefault', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'const reportID = report?.reportID || \'\';', | ||
errors: [{ | ||
messageId: 'disallowedStringDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const policyID = policy?.id || \'\';', | ||
errors: [{ | ||
messageId: 'disallowedStringDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const reportID = report?.reportID || \'0\';', | ||
errors: [{ | ||
messageId: 'disallowedStringDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const policyID = policy?.id || \'0\';', | ||
errors: [{ | ||
messageId: 'disallowedStringDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const reportID = report ? report.reportID : \'-1\';', | ||
errors: [{ | ||
messageId: 'disallowedStringDefault', | ||
}], | ||
}, | ||
{ | ||
code: 'const reportID = report ? report.reportID : \'0\';', | ||
errors: [{ | ||
messageId: 'disallowedStringDefault', | ||
}], | ||
}, | ||
], | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than having all case-sensitive options, can we just make the check be cause-insensitive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fabioh8010 bump on this question
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to keep them all separated for all, just in case we discover that some string match is being problematic, but it happens to be case sensitive. Is that ok for you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, yeah. That's fine. Thanks for explaining and documenting the analysis you did!