From 4f60a56459933f70b0d3ab56182afdb42b713dbb Mon Sep 17 00:00:00 2001 From: Joan Cejudo Date: Tue, 20 Dec 2022 08:30:38 -0800 Subject: [PATCH 1/4] Add optional chaining to node value access. Add test for non-initialized property --- lib/rules/no-tracked-properties-from-args.js | 6 +++--- tests/lib/rules/no-tracked-properties-from-args.js | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/rules/no-tracked-properties-from-args.js b/lib/rules/no-tracked-properties-from-args.js index 86354f4078..3b3bd1f76b 100644 --- a/lib/rules/no-tracked-properties-from-args.js +++ b/lib/rules/no-tracked-properties-from-args.js @@ -12,9 +12,9 @@ function visitClassPropertyOrPropertyDefinition(node, context, trackedImportName isClassPropertyOrPropertyDefinitionWithDecorator(node, trackedImportName); const hasThisArgsValue = - node.value.object?.type === 'MemberExpression' && - node.value.object?.object.type === 'ThisExpression' && - node.value.object?.property.name === 'args'; + node.value?.object?.type === 'MemberExpression' && + node.value?.object?.object.type === 'ThisExpression' && + node.value?.object?.property.name === 'args'; if (hasTrackedDecorator && hasThisArgsValue) { context.report({ node, messageId: 'main' }); diff --git a/tests/lib/rules/no-tracked-properties-from-args.js b/tests/lib/rules/no-tracked-properties-from-args.js index 9d79379358..f97c49a0b1 100644 --- a/tests/lib/rules/no-tracked-properties-from-args.js +++ b/tests/lib/rules/no-tracked-properties-from-args.js @@ -62,6 +62,11 @@ ruleTester.run('no-tracked-properties-from-args', rule, { @fooTracked test = this.notArgs.args.test } `, + ` + class Test{ + notInitializedProperty; + } + `, ], invalid: [ { From c0ded44dc4ed409bd860baf5b6258ceac9fff7b5 Mon Sep 17 00:00:00 2001 From: Joan Cejudo Date: Tue, 20 Dec 2022 08:38:07 -0800 Subject: [PATCH 2/4] add null check for nested object --- lib/rules/no-tracked-properties-from-args.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/no-tracked-properties-from-args.js b/lib/rules/no-tracked-properties-from-args.js index 3b3bd1f76b..5e09f9f8ba 100644 --- a/lib/rules/no-tracked-properties-from-args.js +++ b/lib/rules/no-tracked-properties-from-args.js @@ -13,7 +13,7 @@ function visitClassPropertyOrPropertyDefinition(node, context, trackedImportName const hasThisArgsValue = node.value?.object?.type === 'MemberExpression' && - node.value?.object?.object.type === 'ThisExpression' && + node.value?.object?.object?.type === 'ThisExpression' && node.value?.object?.property.name === 'args'; if (hasTrackedDecorator && hasThisArgsValue) { From 19b3962f6aee30c6f03d2a40e3df4b3796970580 Mon Sep 17 00:00:00 2001 From: Joan Cejudo Date: Tue, 20 Dec 2022 09:48:06 -0800 Subject: [PATCH 3/4] update tests and rule checks --- lib/rules/no-tracked-properties-from-args.js | 9 +++++--- .../rules/no-tracked-properties-from-args.js | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/rules/no-tracked-properties-from-args.js b/lib/rules/no-tracked-properties-from-args.js index 5e09f9f8ba..149401ba68 100644 --- a/lib/rules/no-tracked-properties-from-args.js +++ b/lib/rules/no-tracked-properties-from-args.js @@ -1,5 +1,7 @@ 'use strict'; +const { startsWithThisExpression } = require('../utils/utils'); +const { nodeToDependentKey } = require('../utils/property-getter'); const { getImportIdentifier } = require('../utils/import'); const { isClassPropertyOrPropertyDefinitionWithDecorator } = require('../utils/decorators'); @@ -12,9 +14,10 @@ function visitClassPropertyOrPropertyDefinition(node, context, trackedImportName isClassPropertyOrPropertyDefinitionWithDecorator(node, trackedImportName); const hasThisArgsValue = - node.value?.object?.type === 'MemberExpression' && - node.value?.object?.object?.type === 'ThisExpression' && - node.value?.object?.property.name === 'args'; + node.value && + startsWithThisExpression(node.value) && + nodeToDependentKey(node.value, context) && + nodeToDependentKey(node.value, context).split('.')[0] === 'args'; if (hasTrackedDecorator && hasThisArgsValue) { context.report({ node, messageId: 'main' }); diff --git a/tests/lib/rules/no-tracked-properties-from-args.js b/tests/lib/rules/no-tracked-properties-from-args.js index f97c49a0b1..0309875468 100644 --- a/tests/lib/rules/no-tracked-properties-from-args.js +++ b/tests/lib/rules/no-tracked-properties-from-args.js @@ -62,6 +62,13 @@ ruleTester.run('no-tracked-properties-from-args', rule, { @fooTracked test = this.notArgs.args.test } `, + ` + import { tracked } from '@glimmer/tracking'; + + class Test { + @tracked test = this.args2.test + } + `, ` class Test{ notInitializedProperty; @@ -69,6 +76,21 @@ ruleTester.run('no-tracked-properties-from-args', rule, { `, ], invalid: [ + { + code: ` + import { tracked } from '@glimmer/tracking' + + class Test { + @tracked test = this.args; + }`, + output: null, + errors: [ + { + messageId: 'main', + // type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8) + }, + ], + }, { code: ` import { tracked } from '@glimmer/tracking' From fdb79a54a36475d958f6e708d92941a53cc6ae9e Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Tue, 20 Dec 2022 21:47:25 -0500 Subject: [PATCH 4/4] Update lib/rules/no-tracked-properties-from-args.js --- lib/rules/no-tracked-properties-from-args.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/rules/no-tracked-properties-from-args.js b/lib/rules/no-tracked-properties-from-args.js index 149401ba68..45b9f22471 100644 --- a/lib/rules/no-tracked-properties-from-args.js +++ b/lib/rules/no-tracked-properties-from-args.js @@ -16,7 +16,6 @@ function visitClassPropertyOrPropertyDefinition(node, context, trackedImportName const hasThisArgsValue = node.value && startsWithThisExpression(node.value) && - nodeToDependentKey(node.value, context) && nodeToDependentKey(node.value, context).split('.')[0] === 'args'; if (hasTrackedDecorator && hasThisArgsValue) {