Skip to content

Commit

Permalink
prevent iterating over the list of parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandomg committed Oct 29, 2020
1 parent 445bf32 commit e2d594e
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions lib/rules/order/visibility-modifier-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,28 @@ class VisibilityModifierOrderChecker extends BaseChecker {
this.tokens = tokens
}

getTokensWithoutFunctionParams(node) {
const parametersCount = node.parameters.length
const nodeStart = parametersCount
? node.parameters[parametersCount - 1].loc.end
: node.loc.start
const lastParamIndex = this.tokens.findIndex(
token =>
token.loc.start.line === nodeStart.line && token.loc.start.column === nodeStart.column
)

// discard parameters
return this.tokens.slice(lastParamIndex + 1)
}

FunctionDefinition(node) {
if (node.visibility !== 'default' && (node.stateMutability || node.modifiers.length)) {
const functionTokens = []
const nodeStart = node.loc.start.line
const nodeEnd = node.loc.end.line
const nodeEnd = node.loc.end
const tokens = this.getTokensWithoutFunctionParams(node)

for (let i = 0, n = this.tokens.length; i < n; ++i) {
const token = this.tokens[i]
for (let i = 0, n = tokens.length; i < n; ++i) {
const token = tokens[i]

if (functionTokens.length && token.value === '{') break

Expand All @@ -52,14 +66,11 @@ class VisibilityModifierOrderChecker extends BaseChecker {
loc: { start }
} = token

if (
nodeStart <= start.line &&
start.line <= nodeEnd &&
['Keyword', 'Identifier'].includes(type)
) {
if (start.line <= nodeEnd.line && ['Keyword', 'Identifier'].includes(type)) {
functionTokens.push(token)
}
}

const visibilityIndex = functionTokens.findIndex(t => t.value === node.visibility)
const stateMutabilityIndex = functionTokens.findIndex(t => t.value === node.stateMutability)
const modifierIndex = functionTokens.findIndex(t => t.value === node.modifiers[0].name)
Expand Down

0 comments on commit e2d594e

Please sign in to comment.