Skip to content

Commit

Permalink
fix(params): fix bug in no-unused-params
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Heavner committed Mar 10, 2020
1 parent 45b14a1 commit c2244d8
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 27 deletions.
8 changes: 5 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
"<node_internals>/**"
],
"args": [
"-i"
"-i",
"--coverage",
"false"
],
"internalConsoleOptions": "openOnSessionStart",
"internalConsoleOptions": "neverOpen",
"program": "${workspaceFolder}/node_modules/jest/bin/jest.js",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": [
"${workspaceRoot}/dist/**/*"
],
}
]
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"eslint": "5.6.0"
},
"dependencies": {
"@roku-road/bright": "1.2.11"
"@roku-road/bright": "^1.2.13"
},
"devDependencies": {
"@types/eslint": "4.16.3",
Expand Down
43 changes: 30 additions & 13 deletions src/rules/no-unused-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Rule, Scope } from 'eslint'
const meta: Rule.RuleMetaData = {
docs: {
category: 'Possible Errors',
description: 'Check that all function paremeters are referenced',
description: 'Check that all function parameters are referenced',
recommended: true,
},
fixable: 'code',
Expand All @@ -23,24 +23,41 @@ function findAllRefs(scope: Scope.Scope): Scope.Reference[] {
let refs = []
refs = refs.concat(scope.references)
scope.childScopes.forEach(s => {
refs = findAllRefs(s)
refs = refs.concat(findAllRefs(s))
})
return refs
}

const create = (context: Rule.RuleContext) => {
return {
Parameter(node) {
const scope = context.getScope()
const ref = findAllRefs(scope).find(r => r.identifier.name === node.name.name)
if (!ref) {
context.report({
data: {
functionName: (context.getScope().block as any).id.name,
name: node.name.name,
},
messageId: 'UNUSED',
node,
Identifier(node) {
if (node.parent && !['Property', 'Parameter', 'DotMemberExpression'].find(n => n === node.parent.type)) {
if (!context.getScope().references
.find(s => s.identifier.name === node.name && s.identifier.range[0] === node.range[0] && s.identifier.range[1] === node.range[1])) {
context.getScope().references.push({
from: context.getScope(),
identifier: node,
} as any)
}
}
},
'FunctionDeclaration:exit'(fn) {
if (fn.params && fn.params.args.length > 0) {
fn.params.args.forEach(node => {
const scope = context.getScope()
const ref = findAllRefs(scope).find(r => r.identifier.name === node.name.name)
const block = (context.getScope().block as { id?: { name: string } })
const functionName = block.id ? block.id.name : 'anonymous'
if (!ref) {
context.report({
data: {
functionName,
name: node.name.name,
},
messageId: 'UNUSED',
node,
})
}
})
}
},
Expand Down
28 changes: 25 additions & 3 deletions tests/rules/no-unused-params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ export const test = runTest(RULE_NAME, {
a = { value: "value" }
end function`,

[
{
message:
'Parameter value in function a is not used. Consider removing it if it is not needed.',
},
],
],
[
`function a(value as string) as Dynamic
a.value()
end function`,

[
{
message:
Expand All @@ -47,12 +59,22 @@ export const test = runTest(RULE_NAME, {
return a
end function`,

`function voidFunction(a) as Void
`function voidFunction(a, b, n) as Void
if b = 1 and true then
end if
obj = {
f: function()
return a
f: function(section, fuck, d) as string
if a = 123 and d = m then
return m.get(section, fuck).cafd(n)
end if
end function
}
end function`,
`
function onHideScores(v, x)
a().b().c(v)
a()[x]
end function`
].map(valid),
})
16 changes: 9 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@
node-fetch "^2.1.1"
url-template "^2.0.8"

"@roku-road/bright@1.2.11":
version "1.2.11"
resolved "https://registry.yarnpkg.com/@roku-road/bright/-/bright-1.2.11.tgz#e34596b5f0d82ef766a79f6282f842050c392248"
"@roku-road/bright@^1.2.13":
version "1.2.13"
resolved "https://registry.yarnpkg.com/@roku-road/bright/-/bright-1.2.13.tgz#3eb38a1823dde38b38ceaaaaabb989ddee312373"
integrity sha512-uBcBlqNhq3jRpk+iG801EfrWopL4r1sqy2bxJIGG2OyAYEAULDP21QeEHyv6ePmXA/Iny8V297TC+nt98HCkMQ==
dependencies:
chevrotain "3.7.2"
lodash "4.17.11"
lodash "4.17.12"

"@semantic-release/commit-analyzer@^6.0.0":
version "6.0.0"
Expand Down Expand Up @@ -3408,9 +3409,10 @@ lodash.without@~4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.without/-/lodash.without-4.4.0.tgz#3cd4574a00b67bae373a94b748772640507b7aac"

lodash@4.17.11:
version "4.17.11"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
lodash@4.17.12:
version "4.17.12"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.12.tgz#a712c74fdc31f7ecb20fe44f157d802d208097ef"
integrity sha512-+CiwtLnsJhX03p20mwXuvhoebatoh5B3tt+VvYlrPgZC1g36y+RRbkufX95Xa+X4I59aWEacDFYwnJZiyBh9gA==

lodash@^4.13.1, lodash@^4.17.10, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.2.1:
version "4.17.10"
Expand Down

0 comments on commit c2244d8

Please sign in to comment.