Skip to content

Ensure non-identifier hash keys are mapped correctly #64

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

Merged
merged 1 commit into from
Mar 13, 2021
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
6 changes: 3 additions & 3 deletions packages/core/__tests__/language-server/completions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Language Server: Completions', () => {
\`;
}

class Inner extends Component<{ Args: { foo?: string; bar?: number } }> {}
class Inner extends Component<{ Args: { foo?: string; 'bar-baz'?: number } }> {}
`;

project.write('index.ts', code);
Expand All @@ -42,13 +42,13 @@ describe('Language Server: Completions', () => {
},
{
kind: CompletionItemKind.Field,
label: 'bar',
label: 'bar-baz',
},
]);

let details = server.getCompletionDetails(completions![1]);

expect(details.detail).toEqual('(property) bar?: number | undefined');
expect(details.detail).toEqual("(property) 'bar-baz'?: number | undefined");
});

test('referencing class properties', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/transform/__tests__/offset-mapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ describe('Source-to-source offset mapping', () => {
contents: '{{foo bar-baz=hello}}',
});
expectTokenMapping(module, 'foo');
expectTokenMapping(module, 'bar-baz', { transformedToken: '"bar-baz"' });
expectTokenMapping(module, 'bar-baz');
expectTokenMapping(module, 'hello');
});

Expand All @@ -230,7 +230,7 @@ describe('Source-to-source offset mapping', () => {
contents: '<Foo @bar-baz={{hello}} />',
});
expectTokenMapping(module, 'Foo');
expectTokenMapping(module, 'bar-baz', { transformedToken: '"bar-baz"' });
expectTokenMapping(module, 'bar-baz');
expectTokenMapping(module, 'hello');
});

Expand All @@ -249,7 +249,7 @@ describe('Source-to-source offset mapping', () => {
contents: '<Foo><:block-name>hi</:block-name></Foo>',
});
expectTokenMapping(module, 'Foo');
expectTokenMapping(module, 'block-name', { transformedToken: '"block-name"' });
expectTokenMapping(module, 'block-name');
});
});

Expand Down
11 changes: 7 additions & 4 deletions packages/transform/src/map-template-contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export function mapTemplateContents(
let captureMapping = (
hbsRange: Range,
source: AST.Node | Identifier,
allowEmpty: boolean,
callback: () => void
): void => {
let start = offset;
Expand All @@ -147,7 +148,7 @@ export function mapTemplateContents(
// If the offset didn't change (either because nothing was emitted
// or because an exception was thrown), don't add a new node to the
// mapping tree or flush any new content.
if (start !== offset) {
if (start !== offset || allowEmpty) {
let end = offset;
let tsRange = { start, end };

Expand Down Expand Up @@ -179,7 +180,9 @@ export function mapTemplateContents(
segmentsStack[0].push(value);
},
synthetic(value: string) {
emit.identifier(value, 0, 0);
if (value.length) {
emit.identifier(value, 0, 0);
}
},
identifier(value: string, hbsOffset: number, hbsLength = value.length) {
// If there's a pending indent, flush that so it's not included in
Expand All @@ -190,10 +193,10 @@ export function mapTemplateContents(

let hbsRange = { start: hbsOffset, end: hbsOffset + hbsLength };
let source = new Identifier(value);
captureMapping(hbsRange, source, () => emit.text(value));
captureMapping(hbsRange, source, true, () => emit.text(value));
},
forNode(node: AST.Node, callback: () => void) {
captureMapping(rangeForNode(node), node, callback);
captureMapping(rangeForNode(node), node, false, callback);
},
};

Expand Down
14 changes: 10 additions & 4 deletions packages/transform/src/template-to-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -792,16 +792,22 @@ export function templateToTypescript(
if (isSafeKey(part)) {
emit.identifier(part, start);
} else {
emit.text('[');
emit.identifier(JSON.stringify(part), start, part.length);
emit.text(']');
emit.text('["');
emit.identifier(JSON.stringify(part).slice(1, -1), start, part.length);
emit.text('"]');
}
start += part.length;
}
}

function emitHashKey(name: string, start: number): void {
emit.identifier(isSafeKey(name) ? name : JSON.stringify(name), start, name.length);
if (isSafeKey(name)) {
emit.identifier(name, start);
} else {
emit.text('"');
emit.identifier(JSON.stringify(name).slice(1, -1), start, name.length);
emit.text('"');
}
}

function emitLiteral(node: AST.Literal): void {
Expand Down