From e2a85a1bdb690123fe6f183b43d6509611ab0a97 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Wed, 27 May 2020 20:17:39 +0200 Subject: [PATCH] Retain pure annotations in class fields (#3599) * Retain pure annotations in class fields * Improve coverage * Only check key if it is computed --- src/utils/pureComments.ts | 10 ++++++++++ test/form/samples/pure-class-field/_config.js | 3 +++ test/form/samples/pure-class-field/_expected.js | 6 ++++++ test/form/samples/pure-class-field/main.js | 4 ++++ 4 files changed, 23 insertions(+) create mode 100644 test/form/samples/pure-class-field/_config.js create mode 100644 test/form/samples/pure-class-field/_expected.js create mode 100644 test/form/samples/pure-class-field/main.js diff --git a/src/utils/pureComments.ts b/src/utils/pureComments.ts index 96822746845..c45336a4520 100644 --- a/src/utils/pureComments.ts +++ b/src/utils/pureComments.ts @@ -3,6 +3,16 @@ import * as acorn from 'acorn'; import { base as basicWalker } from 'acorn-walk'; import { CommentDescription } from '../Module'; +// patch up acorn-walk until class-fields are officially supported +basicWalker.FieldDefinition = function (node: any, st: any, c: any) { + if (node.computed) { + c(node.key, st, 'Expression'); + } + if (node.value) { + c(node.value, st, 'Expression'); + } +}; + function handlePureAnnotationsOfNode( node: acorn.Node, state: { commentIndex: number; commentNodes: CommentDescription[] }, diff --git a/test/form/samples/pure-class-field/_config.js b/test/form/samples/pure-class-field/_config.js new file mode 100644 index 00000000000..d2fd9bd5e0a --- /dev/null +++ b/test/form/samples/pure-class-field/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'retains pure annotations in class fields' +}; diff --git a/test/form/samples/pure-class-field/_expected.js b/test/form/samples/pure-class-field/_expected.js new file mode 100644 index 00000000000..b03c81c0cd0 --- /dev/null +++ b/test/form/samples/pure-class-field/_expected.js @@ -0,0 +1,6 @@ +class main { + static k = /*#__PURE__*/ V(); + static [/*#__PURE__*/ W()]; +} + +export default main; diff --git a/test/form/samples/pure-class-field/main.js b/test/form/samples/pure-class-field/main.js new file mode 100644 index 00000000000..bc05c5307b5 --- /dev/null +++ b/test/form/samples/pure-class-field/main.js @@ -0,0 +1,4 @@ +export default class { + static k = /*#__PURE__*/ V(); + static [/*#__PURE__*/ W()]; +}