Skip to content

Commit

Permalink
Fix using iterations for each without used props inside (#69)
Browse files Browse the repository at this point in the history
* Turn on all tests for prop-types rule

* Make sure we attach scope variables only for prop-related variables
  • Loading branch information
ezhlobo committed Aug 26, 2019
1 parent 3a3b68c commit 83291a1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
22 changes: 11 additions & 11 deletions lib/util/getUsedVariablesInPug.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ const getUsedVariablesInPug = (template = '') => {
},
})

let variablesInScope = []
const lastAddedVariable = usedVariables[usedVariables.length - 1]

if (token.type === 'each') {
// If we define a scope with a variable connected to props in that definition,
// we also mark all vars in that scope as used
if (token.type === 'each' && lastAddedVariable) {
const getLastTokenInScope = (all, start) => {
const startIndex = all.findIndex(item => item === start)
const lastToken = all.slice(startIndex).find(item => item.type === 'outdent' && item.loc.end.column <= start.loc.start.column)
Expand All @@ -151,7 +153,7 @@ const getUsedVariablesInPug = (template = '') => {
const bodyLines = [''].concat(lines.slice(startToken.loc.start.line, endToken.loc.end.line - 1))
const body = bodyLines.join('\n')

variablesInScope = getUsedVariablesInPug(body)
const variablesInScope = getUsedVariablesInPug(body)
.filter(item => item.allNames.length > 1 || item.extra.isSpreadElement === true)
.map(item => ({
...item,
Expand All @@ -169,16 +171,14 @@ const getUsedVariablesInPug = (template = '') => {
? ['__COMPUTED_PROP__', ...item.allNames.slice(1)]
: item.allNames,
}))
}

const lastAddedVariable = usedVariables[usedVariables.length - 1]

variablesInScope.forEach((item) => {
usedVariables.push({
...item,
allNames: [...lastAddedVariable.allNames, ...item.allNames],
variablesInScope.forEach((item) => {
usedVariables.push({
...item,
allNames: [...lastAddedVariable.allNames, ...item.allNames],
})
})
})
}
})

return usedVariables
Expand Down
14 changes: 13 additions & 1 deletion tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,19 @@ const cases = [
}
`,
},
{
code: `
function Component() {
const list = []
return pug\`
each item in list
div(key=item.id)
= item.test
\`
}
`,
},
],
invalid: [
{
Expand Down Expand Up @@ -642,7 +655,6 @@ const cases = [
],
},
{
only: true,
code: `
function Component(props) {
const id = true
Expand Down

0 comments on commit 83291a1

Please sign in to comment.