Skip to content
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
10 changes: 1 addition & 9 deletions src/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import * as angular from '@angular/compiler';
import type * as babel from '@babel/types';

import type { LocationInformation, NGNode, RawNGSpan } from './types.ts';
import {
getCharacterIndex,
getCharacterLastIndex,
sourceSpanToLocationInformation,
} from './utils.ts';
import { getCharacterIndex, sourceSpanToLocationInformation } from './utils.ts';

export class Source {
text;
Expand All @@ -19,10 +15,6 @@ export class Source {
return getCharacterIndex(this.text, pattern, index);
}

getCharacterLastIndex(pattern: RegExp | string, index: number) {
return getCharacterLastIndex(this.text, pattern, index);
}

transformSpan(span: RawNGSpan): LocationInformation {
return sourceSpanToLocationInformation(span);
}
Expand Down
74 changes: 23 additions & 51 deletions src/transform-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,60 +179,32 @@ class Transformer extends Source {
properties: Partial<T> & { type: T['type'] },
location: angular.AST | RawNGSpan | [number, number] = node,
) => this.#create(properties, location, [node, ...ancestors]);
const tProperties = keys.map((property, index) => {
const { key, quoted, isShorthandInitialized = false } = property;
const { start: valueStart, end: valueEnd } = values[index].sourceSpan;
const value = transformChild<babel.Expression>(values[index]);

let tKey: (babel.StringLiteral | babel.Identifier) &
LocationInformation;

if (isShorthandInitialized) {
tKey = transformChild<babel.Identifier>(values[index]);
} else {
// No location information
// https://github.com/angular/angular/issues/66175
const keyStart = super.getCharacterIndex(
/\S/,
index === 0
? node.sourceSpan.start + 1 // {
: super.getCharacterIndex(',', values[index - 1].sourceSpan.end) +
1,
);
const keyEnd =
valueStart === keyStart
? valueEnd
: super.getCharacterLastIndex(
/\S/,
super.getCharacterLastIndex(':', valueStart - 1) - 1,
) + 1;
tKey = quoted
? createChild<babel.StringLiteral>(
{ type: 'StringLiteral', value: key },
[keyStart, keyEnd],
)
: createChild<babel.Identifier>({ type: 'Identifier', name: key }, [
keyStart,
keyEnd,
]);
}

return createChild<babel.ObjectPropertyNonComputed>(
{
type: 'ObjectProperty',
key: tKey,
value,
shorthand: isShorthandInitialized,
computed: false,
// @ts-expect-error -- Missed in types
method: false,
},
[tKey.range[0], valueEnd],
);
});
return createNode<babel.ObjectExpression>({
type: 'ObjectExpression',
properties: tProperties,
properties: keys.map((keyNode, index) => {
const valueNode = values[index];
const shorthand = Boolean(keyNode.isShorthandInitialized);
const key = createChild<babel.Identifier | babel.StringLiteral>(
keyNode.quoted
? { type: 'StringLiteral', value: keyNode.key }
: { type: 'Identifier', name: keyNode.key },
keyNode.sourceSpan,
);

return createChild<babel.ObjectPropertyNonComputed>(
{
type: 'ObjectProperty',
key,
value: transformChild<babel.Expression>(valueNode),
shorthand,
computed: false,
// @ts-expect-error -- Missed in types
method: false,
},
[keyNode.sourceSpan.start, valueNode.sourceSpan.end],
);
}),
});
}

Expand Down
9 changes: 1 addition & 8 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { getCharacterIndex, getCharacterLastIndex } from './utils.ts';
import { getCharacterIndex } from './utils.ts';

test('getCharacterIndex', () => {
expect(getCharacterIndex('foobar', /o/, 0)).toBe(1);
expect(getCharacterIndex('foobar', /o/, 1)).toBe(1);
expect(getCharacterIndex('foobar', 'o', 0)).toBe(1);
expect(() => getCharacterIndex('foobar', '_', 0)).toThrow();
});

test('getCharacterLastIndex', () => {
expect(getCharacterLastIndex('foobar', /o/, 6)).toBe(2);
expect(getCharacterLastIndex('foobar', /o/, 2)).toBe(2);
expect(getCharacterLastIndex('foobar', 'o', 6)).toBe(2);
expect(() => getCharacterLastIndex('foobar', '_', 6)).toThrow();
});
23 changes: 0 additions & 23 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,6 @@ function getCharacterSearchTestFunction(pattern: RegExp | string) {
return (character: string) => pattern.test(character);
}

export function getCharacterLastIndex(
text: string,
pattern: RegExp | string,
fromIndex: number,
) {
const test = getCharacterSearchTestFunction(pattern);

for (let index = fromIndex; index >= 0; index--) {
const character = text[index];

if (test(character)) {
return index;
}
}

/* c8 ignore next 4 @preserve */
throw new Error(
`Cannot find front char ${pattern} from index ${fromIndex} in ${JSON.stringify(
text,
)}`,
);
}

export function getCharacterIndex(
text: string,
pattern: RegExp | string,
Expand Down