diff --git a/src/source.ts b/src/source.ts index b38521b3..be775fba 100644 --- a/src/source.ts +++ b/src/source.ts @@ -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; @@ -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); } diff --git a/src/transform-node.ts b/src/transform-node.ts index aaf156a5..adf70315 100644 --- a/src/transform-node.ts +++ b/src/transform-node.ts @@ -179,60 +179,32 @@ class Transformer extends Source { properties: Partial & { 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(values[index]); - - let tKey: (babel.StringLiteral | babel.Identifier) & - LocationInformation; - - if (isShorthandInitialized) { - tKey = transformChild(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( - { type: 'StringLiteral', value: key }, - [keyStart, keyEnd], - ) - : createChild({ type: 'Identifier', name: key }, [ - keyStart, - keyEnd, - ]); - } - return createChild( - { - type: 'ObjectProperty', - key: tKey, - value, - shorthand: isShorthandInitialized, - computed: false, - // @ts-expect-error -- Missed in types - method: false, - }, - [tKey.range[0], valueEnd], - ); - }); return createNode({ type: 'ObjectExpression', - properties: tProperties, + properties: keys.map((keyNode, index) => { + const valueNode = values[index]; + const shorthand = Boolean(keyNode.isShorthandInitialized); + const key = createChild( + keyNode.quoted + ? { type: 'StringLiteral', value: keyNode.key } + : { type: 'Identifier', name: keyNode.key }, + keyNode.sourceSpan, + ); + + return createChild( + { + type: 'ObjectProperty', + key, + value: transformChild(valueNode), + shorthand, + computed: false, + // @ts-expect-error -- Missed in types + method: false, + }, + [keyNode.sourceSpan.start, valueNode.sourceSpan.end], + ); + }), }); } diff --git a/src/utils.test.ts b/src/utils.test.ts index abf4ce0f..44308b64 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -1,4 +1,4 @@ -import { getCharacterIndex, getCharacterLastIndex } from './utils.ts'; +import { getCharacterIndex } from './utils.ts'; test('getCharacterIndex', () => { expect(getCharacterIndex('foobar', /o/, 0)).toBe(1); @@ -6,10 +6,3 @@ test('getCharacterIndex', () => { 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(); -}); diff --git a/src/utils.ts b/src/utils.ts index 2d4f37dd..8a3c8f8b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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,