Skip to content

Commit

Permalink
Move functions outside create()
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioh8010 committed Nov 28, 2024
1 parent b9aec0a commit f784aff
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions eslint-plugin-expensify/no-default-id-values.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
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: {
Expand All @@ -15,31 +40,6 @@ module.exports = {
},
},
create(context) {
function createPatternRegex(pattern) {
return new RegExp(pattern.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1'), 'g');
}

function searchForPatternsAndReport(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);
}
}

const sourceCode = context.getSourceCode();
const soureCodeStr = sourceCode.text; // This gets all the text in the file

Expand Down Expand Up @@ -74,11 +74,11 @@ module.exports = {
];

disallowedNumberDefaults.forEach((pattern) => {
searchForPatternsAndReport(sourceCode, soureCodeStr, pattern, 'disallowedNumberDefault');
searchForPatternsAndReport(context, sourceCode, soureCodeStr, pattern, 'disallowedNumberDefault');
});

disallowedStringDefaults.forEach((pattern) => {
searchForPatternsAndReport(sourceCode, soureCodeStr, pattern, 'disallowedStringDefault');
searchForPatternsAndReport(context, sourceCode, soureCodeStr, pattern, 'disallowedStringDefault');
});

return {};
Expand Down

0 comments on commit f784aff

Please sign in to comment.