Skip to content

Commit

Permalink
feat(watch): remove deprecated PropWillChange/PropDidChange
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Aug 12, 2020
1 parent 3b4211e commit fa2b400
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 43 deletions.
30 changes: 12 additions & 18 deletions src/compiler/transformers/decorators-to-static/watch-decorator.ts
Expand Up @@ -20,25 +20,19 @@ export const watchDecoratorsToStatic = (
}
};

const isWatchDecorator = isDecoratorNamed('Watch');
const isPropWillChangeDecorator = isDecoratorNamed('PropWillChange');
const isPropDidChangeDecorator = isDecoratorNamed('PropDidChange');

const parseWatchDecorator = (config: d.Config, diagnostics: d.Diagnostic[], watchable: Set<string>, method: ts.MethodDeclaration): d.ComponentCompilerWatch[] => {
const methodName = method.name.getText();
return method.decorators
.filter(decorator => isWatchDecorator(decorator) || isPropWillChangeDecorator(decorator) || isPropDidChangeDecorator(decorator))
.map(decorator => {
const [propName] = getDeclarationParameters<string>(decorator);
if (!watchable.has(propName)) {
const dianostic = config.devMode ? buildWarn(diagnostics) : buildError(diagnostics);
dianostic.messageText = `@Watch('${propName}') is trying to watch for changes in a property that does not exist.
return method.decorators.filter(isDecoratorNamed('Watch')).map(decorator => {
const [propName] = getDeclarationParameters<string>(decorator);
if (!watchable.has(propName)) {
const dianostic = config.devMode ? buildWarn(diagnostics) : buildError(diagnostics);
dianostic.messageText = `@Watch('${propName}') is trying to watch for changes in a property that does not exist.
Make sure only properties decorated with @State() or @Prop() are watched.`;
augmentDiagnosticWithNode(dianostic, decorator);
}
return {
propName,
methodName,
};
});
augmentDiagnosticWithNode(dianostic, decorator);
}
return {
propName,
methodName,
};
});
};
25 changes: 0 additions & 25 deletions src/compiler/transformers/test/parse-watch.spec.ts
Expand Up @@ -30,29 +30,4 @@ describe('parse watch', () => {
{ methodName: 'onStateUpdated', propName: 'state1' },
]);
});

it('legacy PropWillChange and PropDidChange', () => {
const t = transpileModule(`
@Component({tag: 'cmp-a'})
export class CmpA {
@Prop() prop1;
@Prop() prop2;
@PropWillChange('prop1')
onUpdate() {
console.log('update');
}
@PropDidChange('prop2')
onStateUpdated() {
console.log('state updated');
}
}
`);

expect(getStaticGetter(t.outputText, 'watchers')).toEqual([
{ methodName: 'onUpdate', propName: 'prop1' },
{ methodName: 'onStateUpdated', propName: 'prop2' },
]);
});
});

0 comments on commit fa2b400

Please sign in to comment.