Skip to content

Add option to show warnings when props could be accessed via this #2101

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

Merged
merged 14 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions lib/rules/no-unused-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ const GROUP_SETUP = 'setup'
const GROUP_WATCHER = 'watch'
const GROUP_EXPOSE = 'expose'

const STOPREPORTING_UNKNOWN = 'unknownPropertyAccess'
const STOPREPORTING_RETURN = 'returnInstance'

const PROPERTY_LABEL = {
props: 'property',
data: 'data',
Expand Down Expand Up @@ -206,7 +209,15 @@ module.exports = {
uniqueItems: true
},
deepData: { type: 'boolean' },
ignorePublicMembers: { type: 'boolean' }
ignorePublicMembers: { type: 'boolean' },
stopReporting: {
type: 'array',
items: {
enum: [STOPREPORTING_UNKNOWN, STOPREPORTING_RETURN]
},
additionalItems: false,
uniqueItems: true
}
},
additionalProperties: false
}
Expand All @@ -221,8 +232,21 @@ module.exports = {
const groups = new Set(options.groups || [GROUP_PROPERTY])
const deepData = Boolean(options.deepData)
const ignorePublicMembers = Boolean(options.ignorePublicMembers)
const stopReporting = new Set(
options.stopReporting || [STOPREPORTING_UNKNOWN, STOPREPORTING_RETURN]
)
const stopReportingUnknown = Boolean(
stopReporting.has(STOPREPORTING_UNKNOWN)
)
const stopReportingReturn = Boolean(stopReporting.has(STOPREPORTING_RETURN))

const propertyReferenceExtractor = definePropertyReferenceExtractor(context)
const propertyReferenceExtractor = definePropertyReferenceExtractor(
context,
{
stopReportingUnknown,
stopReportingReturn
}
)

/** @type {TemplatePropertiesContainer} */
const templatePropertiesContainer = {
Expand Down
25 changes: 20 additions & 5 deletions lib/utils/property-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ module.exports = {
/**
* @param {RuleContext} context The rule context.
*/
function definePropertyReferenceExtractor(context) {
function definePropertyReferenceExtractor(
context,
{ stopReportingUnknown = true, stopReportingReturn = true } = {}
) {
/** @type {Map<Expression, IPropertyReferences>} */
const cacheForExpression = new Map()
/** @type {Map<Pattern, IPropertyReferences>} */
Expand Down Expand Up @@ -314,9 +317,15 @@ function definePropertyReferenceExtractor(context) {
if (parent.object === node) {
// `arg.foo`
const name = utils.getStaticPropertyName(parent)
return name
? new PropertyReferencesForMember(parent, name, withInTemplate)
: ANY
if (name) {
return new PropertyReferencesForMember(
parent,
name,
withInTemplate
)
} else {
return stopReportingUnknown ? ANY : NEVER
}
}
return NEVER
}
Expand All @@ -331,12 +340,18 @@ function definePropertyReferenceExtractor(context) {
return extractFromExpression(parent, withInTemplate)
}
case 'ArrowFunctionExpression':
case 'ReturnStatement':
case 'VExpressionContainer':
case 'Property':
case 'ArrayExpression': {
return maybeExternalUsed(parent) ? ANY : NEVER
}
case 'ReturnStatement': {
if (stopReportingReturn) {
return maybeExternalUsed(parent) ? ANY : NEVER
} else {
return NEVER
}
}
}
return NEVER
}
Expand Down
208 changes: 207 additions & 1 deletion tests/lib/rules/no-unused-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ const allOptions = [
]
const deepDataOptions = [{ groups: ['data'], deepData: true }]

const stopReportingOptions = {
// Stop reporting errors when accessing via unknown property, e.g. this[varName]
unknownPropertyAccess: [
{
groups: ['computed'],
stopReporting: ['unknownPropertyAccess']
}
],
// Stop reporting errors when returning this
returnInstance: [
{
groups: ['computed'],
stopReporting: ['returnInstance']
}
],
// Don't stop reporting any errors
none: [
{
groups: ['computed'],
stopReporting: []
}
]
}

tester.run('no-unused-properties', rule, {
valid: [
// a property used in a script expression
Expand Down Expand Up @@ -1697,9 +1721,58 @@ tester.run('no-unused-properties', rule, {
},
};
</script>`
},

// stopReportingOptions: unknownPropertyAccess
{
filename: 'test.vue',
code: `
<script>
export default {
computed: {
one () {
return 1
},
two () {
return 2
},
},
methods: {
handler () {
const i = 'two'
const b = this[i]
},
}
}
</script>
`,
options: stopReportingOptions.unknownPropertyAccess
},
// stopReportingOptions: returnInstance
{
filename: 'test.vue',
code: `
<script>
export default {
computed: {
one () {
return 1
},
two () {
return 2
},
},
methods: {
handler () {
return this
},
}
}
</script>
`,
options: stopReportingOptions.returnInstance
}
],

invalid: [
// unused property
{
Expand Down Expand Up @@ -2803,6 +2876,139 @@ tester.run('no-unused-properties', rule, {
line: 10
}
]
},

// stopReportingOptions: unknownPropertyAccess
{
filename: 'test.vue',
code: `
<script>
export default {
computed: {
one () {
return 1
},
two () {
return 2
}
},
methods: {
handler () {
const a = this.one
return this
},
}
}
</script>
`,
options: stopReportingOptions.unknownPropertyAccess,
errors: [
{
message: "'two' of computed property found, but never used.",
line: 8
}
]
},
// stopReportingOptions: returnInstance
{
filename: 'test.vue',
code: `
<script>
export default {
computed: {
one () {
return 1
},
two () {
return 2
}
},
methods: {
handler () {
const a = this.one
const i = 'two'
return this[i]
},
}
}
</script>
`,
options: stopReportingOptions.returnInstance,
errors: [
{
message: "'two' of computed property found, but never used.",
line: 8
}
]
},
// stopReportingOptions: []
{
filename: 'test.vue',
code: `
<script>
export default {
computed: {
one () {
return 1
},
two () {
return 2
}
},
methods: {
handler () {
const a = this.one
const i = 'two'
const b = this[i]
return this
},
}
}
</script>
`,
options: stopReportingOptions.none,
errors: [
{
message: "'two' of computed property found, but never used.",
line: 8
}
]
},
{
filename: 'test.vue',
code: `
<script>
export default {
data () {
return {
foo: {
bar: 1
},
baz: 2
}
},
methods: {
handler () {
console.log(this.baz)
return this.foo
},
}
}
</script>
`,
options: [
{
groups: ['data'],
deepData: true,
stopReporting: []
}
],
errors: [
{
message: "'foo.bar' of data found, but never used.",
line: 7
}
]
}
]
})
Expand Down