angular-estree-parser
Version:
A parser that converts Angular source code into an ESTree-compatible form
59 lines (58 loc) • 1.99 kB
JavaScript
import { fitSpans, getCharacterIndex, getCharacterLastIndex, sourceSpanToLocationInformation, } from './utils.js';
export class Source {
text;
constructor(text) {
this.text = text;
}
getCharacterIndex(pattern, index) {
return getCharacterIndex(this.text, pattern, index);
}
getCharacterLastIndex(pattern, index) {
return getCharacterLastIndex(this.text, pattern, index);
}
transformSpan(span, { stripSpaces = false, hasParentParens = false } = {}) {
if (!stripSpaces) {
return sourceSpanToLocationInformation(span);
}
const { outerSpan, innerSpan, hasParens } = fitSpans(span, this.text, hasParentParens);
const locationInformation = sourceSpanToLocationInformation(innerSpan);
if (hasParens) {
locationInformation.extra = {
parenthesized: true,
parenStart: outerSpan.start,
parenEnd: outerSpan.end,
};
}
return locationInformation;
}
createNode(properties,
// istanbul ignore next
{ stripSpaces = true, hasParentParens = false } = {}) {
const { type, start, end } = properties;
const node = {
...properties,
...this.transformSpan({ start, end }, {
stripSpaces,
hasParentParens,
}),
};
switch (type) {
case 'NumericLiteral':
case 'StringLiteral': {
const raw = this.text.slice(node.start, node.end);
const { value } = node;
node.extra = { ...node.extra, raw, rawValue: value };
break;
}
case 'ObjectProperty': {
const { shorthand } = node;
if (shorthand) {
node.extra = { ...node.extra, shorthand };
}
break;
}
}
return node;
}
}
export default Source;