Skip to content
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

Fix crash with no-tracked-properties-from-args rule #1712

Merged
merged 4 commits into from Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions 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');

Expand All @@ -12,9 +14,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 &&
startsWithThisExpression(node.value) &&
nodeToDependentKey(node.value, context).split('.')[0] === 'args';

if (hasTrackedDecorator && hasThisArgsValue) {
context.report({ node, messageId: 'main' });
Expand Down
27 changes: 27 additions & 0 deletions tests/lib/rules/no-tracked-properties-from-args.js
Expand Up @@ -62,8 +62,35 @@ 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;
}
`,
],
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'
Expand Down