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: update metadata mapping to handle no getter #583

Merged
merged 2 commits into from
Oct 30, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { LightningElement } from 'lwc';
/** NoGetter doc */
export default class NoGetter extends LightningElement {
_property = '';
set property(value) {
this._property = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
18 changes: 13 additions & 5 deletions packages/lwc-language-server/src/javascript/type-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From local testing it appears there will be no property object without either a getter, setter, both, or a dataProperty. TheexternalToInternalLoc method handle the case where this value is empty.

} 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),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Locally this project fails to compile for me. Not sure what's up with that.

image

});
}

Expand Down
Loading