angular-estree-parser
Advanced tools
Comparing version 8.0.0 to 8.0.1
export declare class Context { | ||
#private; | ||
text: string; | ||
locator: Locator; | ||
constructor(text: string); | ||
} | ||
declare class Locator { | ||
private _linesAndColumns; | ||
constructor(text: string); | ||
locationForIndex(index: number): { | ||
@@ -14,3 +10,5 @@ line: number; | ||
}; | ||
getCharacterIndex(pattern: RegExp | string, index: number): number; | ||
getCharacterLastIndex(pattern: RegExp | string, index: number): number; | ||
} | ||
export {}; | ||
export default Context; |
@@ -1,20 +0,21 @@ | ||
// @ts-expect-error -- Can't work | ||
import { LinesAndColumns } from 'lines-and-columns'; | ||
import { getCharacterIndex, getCharacterLastIndex } from './utils.js'; | ||
export class Context { | ||
text; | ||
locator; | ||
#linesAndColumns; | ||
constructor(text) { | ||
this.text = text; | ||
this.locator = new Locator(this.text); | ||
} | ||
} | ||
class Locator { | ||
_linesAndColumns; | ||
constructor(text) { | ||
this._linesAndColumns = new LinesAndColumns(text); | ||
} | ||
locationForIndex(index) { | ||
const { line, column } = this._linesAndColumns.locationForIndex(index); | ||
this.#linesAndColumns ??= new LinesAndColumns(this.text); | ||
const { line, column } = this.#linesAndColumns.locationForIndex(index); | ||
return { line: line + 1, column, index }; | ||
} | ||
getCharacterIndex(pattern, index) { | ||
return getCharacterIndex(this.text, pattern, index); | ||
} | ||
getCharacterLastIndex(pattern, index) { | ||
return getCharacterLastIndex(this.text, pattern, index); | ||
} | ||
} | ||
export default Context; |
@@ -1,7 +0,7 @@ | ||
import type { NGMicrosyntax, NGNode } from './types.js'; | ||
export { NGMicrosyntax, NGNode }; | ||
export declare function parseBinding(input: string): NGNode; | ||
export declare function parseSimpleBinding(input: string): NGNode; | ||
export declare function parseInterpolationExpression(input: string): NGNode; | ||
export declare function parseAction(input: string): NGNode; | ||
export declare function parseTemplateBindings(input: string): NGMicrosyntax; | ||
import type { NGNode, NGMicrosyntax } from './types.js'; | ||
export declare const parseBinding: (input: string) => NGNode; | ||
export declare const parseSimpleBinding: (input: string) => NGNode; | ||
export declare const parseInterpolationExpression: (input: string) => NGNode; | ||
export declare const parseAction: (input: string) => NGNode; | ||
export declare const parseTemplateBindings: (input: string) => NGMicrosyntax; | ||
export type { NGMicrosyntax, NGNode }; |
@@ -5,24 +5,15 @@ import { Context } from './context.js'; | ||
import { parseNgAction, parseNgBinding, parseNgInterpolationExpression, parseNgSimpleBinding, parseNgTemplateBindings, } from './utils.js'; | ||
function parse(input, parseNg) { | ||
const { ast: rawNgAst, comments } = parseNg(input); | ||
const context = new Context(input); | ||
const _t = (n) => transform(n, context); | ||
const ast = _t(rawNgAst); | ||
ast.comments = comments.map((comment) => _t(comment)); | ||
return ast; | ||
function createParser(parse) { | ||
return (input) => { | ||
const { ast: rawNgAst, comments } = parse(input); | ||
const context = new Context(input); | ||
const ast = transform(rawNgAst, context); | ||
ast.comments = comments.map((comment) => transform(comment, context)); | ||
return ast; | ||
}; | ||
} | ||
export function parseBinding(input) { | ||
return parse(input, parseNgBinding); | ||
} | ||
export function parseSimpleBinding(input) { | ||
return parse(input, parseNgSimpleBinding); | ||
} | ||
export function parseInterpolationExpression(input) { | ||
return parse(input, parseNgInterpolationExpression); | ||
} | ||
export function parseAction(input) { | ||
return parse(input, parseNgAction); | ||
} | ||
export function parseTemplateBindings(input) { | ||
return transformTemplateBindings(parseNgTemplateBindings(input), new Context(input)); | ||
} | ||
export const parseBinding = createParser(parseNgBinding); | ||
export const parseSimpleBinding = createParser(parseNgSimpleBinding); | ||
export const parseInterpolationExpression = createParser(parseNgInterpolationExpression); | ||
export const parseAction = createParser(parseNgAction); | ||
export const parseTemplateBindings = (input) => transformTemplateBindings(parseNgTemplateBindings(input), new Context(input)); |
import { ExpressionBinding as NGExpressionBinding, VariableBinding as NGVariableBinding, } from '@angular/compiler'; | ||
import { transform, transformSpan, } from './transform.js'; | ||
import { findBackChar, NG_PARSE_TEMPLATE_BINDINGS_FAKE_PREFIX, toLowerCamelCase, } from './utils.js'; | ||
import { NG_PARSE_TEMPLATE_BINDINGS_FAKE_PREFIX, toLowerCamelCase, } from './utils.js'; | ||
export function transformTemplateBindings(rawTemplateBindings, context) { | ||
@@ -51,3 +51,3 @@ rawTemplateBindings.forEach(fixTemplateBindingSpan); | ||
? rawTemplateBindings[0].sourceSpan | ||
: { start: body[0].start, end: body[body.length - 1].end }); | ||
: { start: body[0].start, end: body.at(-1).end }); | ||
function transformTemplateBinding(templateBinding, index) { | ||
@@ -153,3 +153,3 @@ if (isExpressionBinding(templateBinding)) { | ||
} | ||
const index = findBackChar(/\S/, variableBinding.sourceSpan.start, context.text); | ||
const index = context.getCharacterIndex(/\S/g, variableBinding.sourceSpan.start); | ||
return { | ||
@@ -156,0 +156,0 @@ source: '$implicit', |
import type * as ng from '@angular/compiler'; | ||
import type * as b from '@babel/types'; | ||
import { type Context } from './context.js'; | ||
import type Context from './context.js'; | ||
import type { NGNode, RawNGComment, RawNGSpan } from './types.js'; | ||
@@ -5,0 +5,0 @@ export type InputNode = ng.AST | RawNGComment; |
@@ -1,2 +0,2 @@ | ||
import { findBackChar, findFrontChar, fitSpans, getLast, getNgType, } from './utils.js'; | ||
import { fitSpans, getNgType } from './utils.js'; | ||
export const transform = (node, context, isInParentParens = false) => { | ||
@@ -30,3 +30,3 @@ const type = getNgType(node); | ||
const tExp = _t(exp); | ||
const nameStart = _findBackChar(/\S/, _findBackChar(/\|/, _getOuterEnd(tExp)) + 1); | ||
const nameStart = context.getCharacterIndex(/\S/g, context.getCharacterIndex('|', _getOuterEnd(tExp)) + 1); | ||
const tName = _c('Identifier', { name }, { start: nameStart, end: nameStart + name.length }); | ||
@@ -40,3 +40,3 @@ const tArgs = args.map(_t); | ||
start: _getOuterStart(tExp), | ||
end: _getOuterEnd(tArgs.length === 0 ? tName : getLast(tArgs)), | ||
end: _getOuterEnd(tArgs.length === 0 ? tName : tArgs.at(-1)), | ||
}, { hasParentParens: isInParentParens }); | ||
@@ -102,8 +102,9 @@ } | ||
const valueEnd = _getOuterEnd(tValue); | ||
const keyStart = _findBackChar(/\S/, index === 0 | ||
const keyStart = context.getCharacterIndex(/\S/g, index === 0 | ||
? node.sourceSpan.start + 1 // { | ||
: _findBackChar(/,/, _getOuterEnd(tValues[index - 1])) + 1); | ||
: context.getCharacterIndex(',', _getOuterEnd(tValues[index - 1])) + | ||
1); | ||
const keyEnd = valueStart === keyStart | ||
? valueEnd | ||
: _findFrontChar(/\S/, _findFrontChar(/:/, valueStart - 1) - 1) + 1; | ||
: context.getCharacterLastIndex(/\S/g, context.getCharacterLastIndex(':', valueStart - 1) - 1) + 1; | ||
const keySpan = { start: keyStart, end: keyEnd }; | ||
@@ -194,3 +195,3 @@ const tKey = quoted | ||
const { receiver, name } = node; | ||
const nameEnd = _findFrontChar(/\S/, node.sourceSpan.end - 1) + 1; | ||
const nameEnd = context.getCharacterLastIndex(/\S/g, node.sourceSpan.end - 1) + 1; | ||
const tName = _c('Identifier', { name }, { start: nameEnd - name.length, end: nameEnd }, _isImplicitThis(receiver) ? { hasParentParens: isInParentParens } : {}); | ||
@@ -213,3 +214,3 @@ return _transformReceiverAndName(receiver, tName, { | ||
optional: false, | ||
}, { end: _findBackChar(/\]/, _getOuterEnd(tKey)) + 1 }); | ||
}, { end: context.getCharacterIndex(']', _getOuterEnd(tKey)) + 1 }); | ||
return _c('AssignmentExpression', { | ||
@@ -224,3 +225,3 @@ left: tReceiverAndName, | ||
const tValue = _t(value); | ||
const nameEnd = _findFrontChar(/\S/, _findFrontChar(/=/, _getOuterStart(tValue) - 1) - 1) + 1; | ||
const nameEnd = context.getCharacterLastIndex(/\S/g, context.getCharacterLastIndex('=', _getOuterStart(tValue) - 1) - 1) + 1; | ||
const tName = _c('Identifier', { name }, { start: nameEnd - name.length, end: nameEnd }); | ||
@@ -304,8 +305,2 @@ const tReceiverAndName = _transformReceiverAndName(receiver, tName, { | ||
} | ||
function _findFrontChar(regex, index) { | ||
return findFrontChar(regex, index, context.text); | ||
} | ||
function _findBackChar(regex, index) { | ||
return findBackChar(regex, index, context.text); | ||
} | ||
function _isImplicitThis(n) { | ||
@@ -339,4 +334,4 @@ return (n.sourceSpan.start >= n.sourceSpan.end || | ||
identifierName: '', | ||
start: context.locator.locationForIndex(start), | ||
end: context.locator.locationForIndex(end), | ||
start: context.locationForIndex(start), | ||
end: context.locationForIndex(end), | ||
}, | ||
@@ -352,4 +347,4 @@ }; | ||
identifierName: '', | ||
start: context.locator.locationForIndex(innerSpan.start), | ||
end: context.locator.locationForIndex(innerSpan.end), | ||
start: context.locationForIndex(innerSpan.start), | ||
end: context.locationForIndex(innerSpan.end), | ||
}, | ||
@@ -356,0 +351,0 @@ ...(hasParens && { |
@@ -29,5 +29,4 @@ import * as ng from '@angular/compiler'; | ||
}; | ||
export declare function findFrontChar(regex: RegExp, index: number, text: string): number; | ||
export declare function findBackChar(regex: RegExp, index: number, text: string): number; | ||
export declare function getCharacterLastIndex(text: string, pattern: RegExp | string, fromIndex: number): number; | ||
export declare function getCharacterIndex(text: string, pattern: RegExp | string, fromIndex: number): number; | ||
export declare function toLowerCamelCase(str: string): string; | ||
export declare function getLast<T>(array: T[]): T | undefined; |
@@ -177,21 +177,30 @@ import * as ng from '@angular/compiler'; | ||
} | ||
export function findFrontChar(regex, index, text) { | ||
let i = index; | ||
while (!regex.test(text[i])) { | ||
// istanbul ignore next | ||
if (--i < 0) { | ||
throw new Error(`Cannot find front char ${regex} from index ${index} in ${JSON.stringify(text)}`); | ||
function getCharacterSearchTestFunction(pattern) { | ||
if (typeof pattern === 'string') { | ||
return (character) => character === pattern; | ||
} | ||
if (!pattern.global) { | ||
throw new TypeError('a non-global RegExp argument is not supported'); | ||
} | ||
return (character) => pattern.test(character); | ||
} | ||
export function getCharacterLastIndex(text, pattern, fromIndex) { | ||
const test = getCharacterSearchTestFunction(pattern); | ||
for (let index = fromIndex; index >= 0; index--) { | ||
const character = text[index]; | ||
if (test(character)) { | ||
return index; | ||
} | ||
} | ||
return i; | ||
throw new Error(`Cannot find front char ${pattern} from index ${fromIndex} in ${JSON.stringify(text)}`); | ||
} | ||
export function findBackChar(regex, index, text) { | ||
let i = index; | ||
while (!regex.test(text[i])) { | ||
// istanbul ignore next | ||
if (++i >= text.length) { | ||
throw new Error(`Cannot find back char ${regex} from index ${index} in ${JSON.stringify(text)}`); | ||
export function getCharacterIndex(text, pattern, fromIndex) { | ||
const test = getCharacterSearchTestFunction(pattern); | ||
for (let index = fromIndex; index < text.length; index++) { | ||
const character = text[index]; | ||
if (test(character)) { | ||
return index; | ||
} | ||
} | ||
return i; | ||
throw new Error(`Cannot find character ${pattern} from index ${fromIndex} in ${JSON.stringify(text)}`); | ||
} | ||
@@ -201,7 +210,1 @@ export function toLowerCamelCase(str) { | ||
} | ||
export function getLast(array) { | ||
return array.length === 0 | ||
? // istanbul ignore next | ||
undefined | ||
: array[array.length - 1]; | ||
} |
{ | ||
"name": "angular-estree-parser", | ||
"version": "8.0.0", | ||
"version": "8.0.1", | ||
"description": "A parser that converts Angular source code into an ESTree-compatible form", | ||
@@ -22,3 +22,3 @@ "keywords": [], | ||
"lint:eslint": "eslint --ext=.ts,.js,.cjs .", | ||
"lint:prettier": "prettier --check src", | ||
"lint:prettier": "prettier --check .", | ||
"fix": "run-s \"fix:*\"", | ||
@@ -31,20 +31,20 @@ "fix:eslint": "yarn lint:eslint --fix", | ||
"dependencies": { | ||
"lines-and-columns": "^2.0.3", | ||
"lines-and-columns": "^2.0.4", | ||
"tslib": "^2.6.2" | ||
}, | ||
"devDependencies": { | ||
"@angular/compiler": "17.0.0-rc.2", | ||
"@angular/compiler": "17.0.0-rc.3", | ||
"@babel/code-frame": "7.22.13", | ||
"@babel/parser": "7.23.0", | ||
"@babel/types": "7.23.0", | ||
"@types/babel-types": "7.0.13", | ||
"@types/babel__code-frame": "7.0.5", | ||
"@types/babel__code-frame": "7.0.6", | ||
"@types/jest": "29.5.7", | ||
"@types/prettier": "3.0.0", | ||
"@typescript-eslint/eslint-plugin": "6.9.1", | ||
"@typescript-eslint/parser": "6.9.1", | ||
"@typescript-eslint/eslint-plugin": "6.10.0", | ||
"@typescript-eslint/parser": "6.10.0", | ||
"del-cli": "5.1.0", | ||
"eslint": "8.53.0", | ||
"eslint-config-prettier": "9.0.0", | ||
"eslint-plugin-deprecation": "2.0.0", | ||
"eslint-plugin-import": "2.29.0", | ||
"eslint-plugin-unicorn": "49.0.0", | ||
"jest": "29.7.0", | ||
@@ -59,3 +59,3 @@ "jest-snapshot-serializer-raw": "1.2.0", | ||
"peerDependencies": { | ||
"@angular/compiler": "^17.0.0-rc.2" | ||
"@angular/compiler": "^17.0.0-rc.3" | ||
}, | ||
@@ -62,0 +62,0 @@ "engines": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41104
913
Updatedlines-and-columns@^2.0.4