From 5f305491e4d0cfacccdccba26dac465f836cc35b Mon Sep 17 00:00:00 2001 From: Gordon Bockus Date: Mon, 30 Oct 2023 14:26:58 -0500 Subject: [PATCH] fix: update metadata mapping to handle no getter --- .../javascript/__tests__/fixtures/nogetter.js | 8 +++++ .../javascript/__tests__/type-mapping.test.ts | 31 +++++++++++++++++++ .../src/javascript/type-mapping.ts | 18 ++++++++--- 3 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 packages/lwc-language-server/src/javascript/__tests__/fixtures/nogetter.js diff --git a/packages/lwc-language-server/src/javascript/__tests__/fixtures/nogetter.js b/packages/lwc-language-server/src/javascript/__tests__/fixtures/nogetter.js new file mode 100644 index 00000000..99940b21 --- /dev/null +++ b/packages/lwc-language-server/src/javascript/__tests__/fixtures/nogetter.js @@ -0,0 +1,8 @@ +import { LightningElement } from 'lwc'; +/** NoGetter doc */ +export default class NoGetter extends LightningElement { + _property = ''; + set property(value) { + this._property = value; + } +} diff --git a/packages/lwc-language-server/src/javascript/__tests__/type-mapping.test.ts b/packages/lwc-language-server/src/javascript/__tests__/type-mapping.test.ts index 36ab1452..7e675fd8 100644 --- a/packages/lwc-language-server/src/javascript/__tests__/type-mapping.test.ts +++ b/packages/lwc-language-server/src/javascript/__tests__/type-mapping.test.ts @@ -36,3 +36,34 @@ it('can map new metadata to old metadata', async () => { expect(derivedMetadata).toEqual(oldMetadata); }); + +it('Should handle mapping when there is a property with only a setter', async () => { + const filepath = path.join('src', 'javascript', '__tests__', 'fixtures', 'nogetter.js'); + const content = fs.readFileSync(filepath, 'utf8'); + + const newMetadataOpts: BundleConfig = { + type: 'internal', + name: 'nogetter', + namespace: 'x', + namespaceMapping: {}, + files: [ + { + fileName: 'nogetter.js', + source: content, + }, + ], + }; + + const modernMetadata = collectBundleMetadata(newMetadataOpts); + const derivedMetadata = mapLwcMetadataToInternal(modernMetadata.files[0] as ScriptFile); + + const oldTransformOpts: OldCompilerOptions = { + name: 'metadata', + namespace: 'x', + files: {}, + }; + const transformerResult = await transform(content, 'nogetter.js', oldTransformOpts); + const oldMetadata: Metadata = transformerResult.metadata as Metadata; + + expect(derivedMetadata).toEqual(oldMetadata); +}); diff --git a/packages/lwc-language-server/src/javascript/type-mapping.ts b/packages/lwc-language-server/src/javascript/type-mapping.ts index d037f0cb..58a8c54b 100644 --- a/packages/lwc-language-server/src/javascript/type-mapping.ts +++ b/packages/lwc-language-server/src/javascript/type-mapping.ts @@ -217,17 +217,25 @@ function getMemberProperty(propertyObj: ClassProperty): InternalClassMember | nu propertyObj.dataProperty, ); + // Note there can only be a getter or only a setter, both are not required. + // Use the getter if available, or the setter if there is no getter. + let loc: SourceLocation; + if (propertyObj.propertyType === 'accessor') { + if (propertyObj.getter) { + loc = propertyObj.getter.location + } else if (propertyObj.setter) { + loc = propertyObj.setter.location + } + } else { + loc = propertyObj?.dataProperty.location + } return stripKeysWithUndefinedVals({ name: propertyObj.name, type: 'property', value, decorator: decoratorType, doc: propertyObj.__internal__doc, - loc: externalToInternalLoc( - propertyObj.propertyType === 'accessor' - ? propertyObj.getter.location - : propertyObj.dataProperty.location - ), + loc: externalToInternalLoc(loc), }); }