New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@vuedx/template-ast-types

Package Overview
Dependencies
Maintainers
1
Versions
141
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vuedx/template-ast-types - npm Package Compare versions

Comparing version 0.3.1-insiders-1607576746.0 to 0.3.1-insiders-1607577772.0

285

dist/index.cjs.js

@@ -5,2 +5,7 @@ 'use strict';

const compilerCore = require('@vue/compiler-core');
function isSimpleIdentifier(content) {
return compilerCore.isSimpleIdentifier(content.trim());
}
function isNode(node) {

@@ -153,3 +158,15 @@ return typeof node === 'object' && node != null && 'type' in node;

return;
enter(node, state);
let isStopped = false;
const stop = () => {
isStopped = true;
};
enter(node, state, stop);
if (isStopped)
return;
const forwardEnter = (node, state, prevStop) => {
enter(node, state, () => {
stop();
prevStop();
});
};
for (const key of keys) {

@@ -159,7 +176,11 @@ const subNode = node[key];

for (const node of subNode) {
traverseFast(node, enter, state);
traverseFast(node, forwardEnter, state);
if (isStopped)
return;
}
}
else if (isNode(subNode)) {
traverseFast(subNode, enter, state);
traverseFast(subNode, forwardEnter, state);
if (isStopped)
return;
}

@@ -169,2 +190,258 @@ }

const defaults = {
indent: 2,
initialIndent: 0,
directive: 'shorthand',
replaceNodes: new Map(),
};
function stringify(node, options) {
const finalOptions = { ...defaults, ...options };
return genNode(Array.isArray(node) ? { type: 0, children: node } : node, finalOptions.initialIndent * finalOptions.initialIndent, finalOptions);
}
const shorthands = {
bind: ':',
on: '@',
slot: '#',
};
function genNode(node, indent, options) {
if (options.replaceNodes.has(node)) {
const replaced = options.replaceNodes.get(node);
return replaced == null ? '' : genNode(replaced, indent, options);
}
else if (isRootNode(node)) {
return genRootNode(node, indent, options);
}
else if (isElementNode(node)) {
return genElementNode(node, indent, options);
}
else if (isAttributeNode(node)) {
return genAttributeNode(node, indent, options);
}
else if (isDirectiveNode(node)) {
return genDirectiveNode(node, indent, options);
}
else if (isInterpolationNode(node)) {
return `{{ ${genNode(node.content, indent, options)} }}`;
}
else if (isSimpleExpressionNode(node)) {
return genExpressionNode(node, indent, options);
}
else if (isTextNode(node)) {
return genTextNode(node, indent, options);
}
else {
throw new Error(`Unsupported node type: ${node.type}`);
}
}
function genExpressionNode(node, indent, options) {
return genMultilineText(node.content, indent, options);
}
function genTextNode(node, indent, options) {
return genMultilineText(node.content, indent, options);
}
function genMultilineText(content, indent, options) {
if (content.startsWith('\n')) {
content = content.trimStart();
}
if (content.includes('\n')) {
content = content
.split('\n')
.map((line) => line.trim())
.join('\n' + ' '.repeat(indent + options.indent));
}
return content;
}
function genRootNode(node, indent, options) {
return genChildren(node, indent, options);
}
function genElementNode(node, indent, options) {
const code = [];
code.push(' '.repeat(indent), '<', node.tag);
let shouldIndentClosing = false;
const props = applyReplaceNodes(node.props, options);
if (props.length > 0) {
if (props.length > 2) {
code.push('\n');
node.props.forEach((prop) => {
code.push(' '.repeat(indent + options.indent));
code.push(genNode(prop, indent + options.indent, options));
code.push('\n');
});
shouldIndentClosing = true;
}
else {
props.forEach((prop) => {
code.push(' ');
code.push(genNode(prop, indent, options));
});
}
}
if (shouldIndentClosing)
code.push(' '.repeat(indent));
if (node.isSelfClosing) {
if (!shouldIndentClosing)
code.push(' ');
code.push('/>');
}
else {
code.push('>', genChildren(node, indent, options), '</', node.tag, '>');
}
return code.join('');
}
function genChildren(node, indent, options) {
const code = [];
const children = applyReplaceNodes(node.children, options);
if (children.length > 0) {
const hasOnlyInlineChildren = children.every((child) => !isElementNode(child));
if (hasOnlyInlineChildren) {
children.forEach((child) => {
code.push(genNode(child, indent + options.indent, options));
});
}
else {
let wasLastChildInline = true;
children.forEach((child) => {
if (isTextNode(child) && child.content.trim() === '')
return; // Ignore empty text nodes.
const isThisChildInline = !isElementNode(child);
if (wasLastChildInline && isThisChildInline) ;
else if (wasLastChildInline) {
code.push('\n');
}
else if (isThisChildInline) {
code.push('\n', ' '.repeat(indent + options.indent));
}
else {
code.push('\n');
}
code.push(genNode(child, indent + options.indent, options));
wasLastChildInline = isThisChildInline;
});
code.push('\n', ' '.repeat(indent));
}
}
return code.join('');
}
function applyReplaceNodes(nodes, options) {
return nodes
.map((node) => {
if (options.replaceNodes.has(node))
return options.replaceNodes.get(node);
return node;
})
.filter(Boolean);
}
function genDirectiveNode(node, indent, options) {
const code = [];
if (options.directive === 'shorthand' && node.name in shorthands) {
code.push(shorthands[node.name]);
}
else {
code.push(`v-${node.name}`);
if (node.arg != null)
code.push(':');
}
if (isSimpleExpressionNode(node.arg)) {
if (node.arg.isStatic)
code.push(genNode(node.arg, indent, options));
else
code.push('[', genNode(node.arg, indent, options), ']');
}
node.modifiers.forEach((modifier) => code.push('.', modifier));
if (isSimpleExpressionNode(node.exp)) {
code.push('="', genNode(node.exp, indent, options), '"');
}
return code.join('');
}
function genAttributeNode(node, indent, options) {
return node.value != null
? `${node.name}="${genNode(node.value, indent, options)}"`
: node.name;
}
function findTemplateNodeAt(ast, position) {
return findTemplateNodeInRange(ast, position, position);
}
function findTemplateChildNodeAt(ast, position, mode) {
var _a, _b;
const result = findTemplateNodeInRange(ast, position, position, mode);
while (result.ancestors.length > 0) {
if (isRootNode(result.node) ||
isElementNode(result.node) ||
isTextNode(result.node) ||
isInterpolationNode(result.node) ||
isCommentNode(result.node)) {
break;
}
result.node = (_b = (_a = result.ancestors.pop()) === null || _a === void 0 ? void 0 : _a.node) !== null && _b !== void 0 ? _b : null;
}
return result;
}
function findTemplateNodeInRange(ast, start, end, mode) {
const found = {
node: null,
ancestors: [],
};
traverseEvery(ast, (node, ancestors) => {
if (mode === 'start'
? node.loc.start.offset <= start && end < node.loc.end.offset
: mode === 'end'
? node.loc.start.offset < start && end <= node.loc.end.offset
: node.loc.start.offset <= start && end <= node.loc.end.offset) {
found.node = node;
found.ancestors = ancestors.slice();
return true;
}
else {
return false;
}
});
return found;
}
function findTemplateNodesInRange(ast, start, end) {
const found = [];
traverseFast(ast, (node) => {
if (node.loc.start.offset <= start && end <= node.loc.end.offset) {
found.push(node);
}
});
return found;
}
function findTemplateChildrenInRange(ast, start, end) {
var _a, _b;
if (start === end) {
const a = findTemplateChildNodeAt(ast, start);
return a.node != null ? [a.node] : [];
}
const a = findTemplateChildNodeAt(ast, start, 'start');
const b = findTemplateChildNodeAt(ast, end, 'end');
if (a.node == null || b.node == null)
return [];
if (a.node === b.node)
return [a.node];
const pa = (_a = a.ancestors.pop()) === null || _a === void 0 ? void 0 : _a.node;
const pb = (_b = b.ancestors.pop()) === null || _b === void 0 ? void 0 : _b.node;
if (pa == null || pb == null)
return [];
if (pa === b.node)
return [pa];
if (pb === a.node)
return [pb];
if (pa === pb && isElementNode(pa)) {
return pa.children.slice(pa.children.indexOf(a.node), 1 + pa.children.indexOf(b.node));
}
return [];
}
Object.defineProperty(exports, 'createSimpleExpression', {
enumerable: true,
get: function () {
return compilerCore.createSimpleExpression;
}
});
exports.findTemplateChildNodeAt = findTemplateChildNodeAt;
exports.findTemplateChildrenInRange = findTemplateChildrenInRange;
exports.findTemplateNodeAt = findTemplateNodeAt;
exports.findTemplateNodeInRange = findTemplateNodeInRange;
exports.findTemplateNodesInRange = findTemplateNodesInRange;
exports.isAttributeNode = isAttributeNode;

@@ -183,5 +460,7 @@ exports.isCommentNode = isCommentNode;

exports.isSimpleExpressionNode = isSimpleExpressionNode;
exports.isSimpleIdentifier = isSimpleIdentifier;
exports.isSlotNode = isSlotNode;
exports.isTemplateNode = isTemplateNode;
exports.isTextNode = isTextNode;
exports.stringify = stringify;
exports.traverse = traverse;

@@ -188,0 +467,0 @@ exports.traverseEvery = traverseEvery;

@@ -1,5 +0,7 @@

import { Node, RootNode, ElementNode, PlainElementNode, ComponentNode, SlotOutletNode, TemplateNode, TextNode, IfNode, ForNode, CommentNode, SimpleExpressionNode, InterpolationNode, AttributeNode, DirectiveNode, CompoundExpressionNode } from '@vue/compiler-core';
import * as compilerCore from '@vue/compiler-core';
export { compilerCore as t };
import t__default, { Node, RootNode, ElementNode, PlainElementNode, ComponentNode, SlotOutletNode, TemplateNode, TextNode, IfNode, ForNode, CommentNode, SimpleExpressionNode, InterpolationNode, AttributeNode, DirectiveNode, CompoundExpressionNode } from '@vue/compiler-core';
import * as t from '@vue/compiler-core';
export { t };
export { createSimpleExpression } from '@vue/compiler-core';
declare function isSimpleIdentifier(content: string): boolean;
declare function isNode(node: unknown): node is Node;

@@ -39,4 +41,22 @@ declare function isRootNode(node: unknown): node is RootNode;

declare function traverseEvery<T>(node: Node, enter: (node: Node, ancestors: TraversalAncestors, state: T) => boolean, state?: any, ancestors?: TraversalAncestors): void;
declare function traverseFast<T = any>(node: object, enter: (node: Node, state: T) => void, state?: T): void;
declare function traverseFast<T = any>(node: object, enter: (node: Node, state: T, stop: () => void) => void, state?: T): void;
export { TraversalAncestors, TraversalHandler, TraversalHandlers, isAttributeNode, isCommentNode, isComponentNode, isCompoundExpressionNode, isDirectiveNode, isElementNode, isForNode, isIfNode, isInterpolationNode, isNode, isPlainElementNode, isRootNode, isSimpleExpressionNode, isSlotNode, isTemplateNode, isTextNode, traverse, traverseEvery, traverseFast };
interface StringifyOptions {
indent: number;
initialIndent: number;
directive: 'shorthand' | 'longhand';
replaceNodes: Map<Node, Node | null>;
}
declare function stringify(node: Node | Node[], options?: Partial<StringifyOptions>): string;
interface SearchResult {
node: t__default.Node | null;
ancestors: TraversalAncestors;
}
declare function findTemplateNodeAt(ast: t__default.RootNode, position: number): SearchResult;
declare function findTemplateChildNodeAt(ast: t__default.RootNode, position: number, mode?: 'start' | 'end'): SearchResult;
declare function findTemplateNodeInRange(ast: t__default.RootNode, start: number, end: number, mode?: 'start' | 'end'): SearchResult;
declare function findTemplateNodesInRange(ast: t__default.RootNode, start: number, end: number): t__default.Node[];
declare function findTemplateChildrenInRange(ast: t__default.RootNode, start: number, end: number): t__default.Node[];
export { SearchResult, StringifyOptions, TraversalAncestors, TraversalHandler, TraversalHandlers, findTemplateChildNodeAt, findTemplateChildrenInRange, findTemplateNodeAt, findTemplateNodeInRange, findTemplateNodesInRange, isAttributeNode, isCommentNode, isComponentNode, isCompoundExpressionNode, isDirectiveNode, isElementNode, isForNode, isIfNode, isInterpolationNode, isNode, isPlainElementNode, isRootNode, isSimpleExpressionNode, isSimpleIdentifier, isSlotNode, isTemplateNode, isTextNode, stringify, traverse, traverseEvery, traverseFast };

@@ -0,1 +1,7 @@

import { isSimpleIdentifier as isSimpleIdentifier$1 } from '@vue/compiler-core';
export { createSimpleExpression } from '@vue/compiler-core';
function isSimpleIdentifier(content) {
return isSimpleIdentifier$1(content.trim());
}
function isNode(node) {

@@ -148,3 +154,15 @@ return typeof node === 'object' && node != null && 'type' in node;

return;
enter(node, state);
let isStopped = false;
const stop = () => {
isStopped = true;
};
enter(node, state, stop);
if (isStopped)
return;
const forwardEnter = (node, state, prevStop) => {
enter(node, state, () => {
stop();
prevStop();
});
};
for (const key of keys) {

@@ -154,7 +172,11 @@ const subNode = node[key];

for (const node of subNode) {
traverseFast(node, enter, state);
traverseFast(node, forwardEnter, state);
if (isStopped)
return;
}
}
else if (isNode(subNode)) {
traverseFast(subNode, enter, state);
traverseFast(subNode, forwardEnter, state);
if (isStopped)
return;
}

@@ -164,3 +186,248 @@ }

export { isAttributeNode, isCommentNode, isComponentNode, isCompoundExpressionNode, isDirectiveNode, isElementNode, isForNode, isIfNode, isInterpolationNode, isNode, isPlainElementNode, isRootNode, isSimpleExpressionNode, isSlotNode, isTemplateNode, isTextNode, traverse, traverseEvery, traverseFast };
const defaults = {
indent: 2,
initialIndent: 0,
directive: 'shorthand',
replaceNodes: new Map(),
};
function stringify(node, options) {
const finalOptions = { ...defaults, ...options };
return genNode(Array.isArray(node) ? { type: 0, children: node } : node, finalOptions.initialIndent * finalOptions.initialIndent, finalOptions);
}
const shorthands = {
bind: ':',
on: '@',
slot: '#',
};
function genNode(node, indent, options) {
if (options.replaceNodes.has(node)) {
const replaced = options.replaceNodes.get(node);
return replaced == null ? '' : genNode(replaced, indent, options);
}
else if (isRootNode(node)) {
return genRootNode(node, indent, options);
}
else if (isElementNode(node)) {
return genElementNode(node, indent, options);
}
else if (isAttributeNode(node)) {
return genAttributeNode(node, indent, options);
}
else if (isDirectiveNode(node)) {
return genDirectiveNode(node, indent, options);
}
else if (isInterpolationNode(node)) {
return `{{ ${genNode(node.content, indent, options)} }}`;
}
else if (isSimpleExpressionNode(node)) {
return genExpressionNode(node, indent, options);
}
else if (isTextNode(node)) {
return genTextNode(node, indent, options);
}
else {
throw new Error(`Unsupported node type: ${node.type}`);
}
}
function genExpressionNode(node, indent, options) {
return genMultilineText(node.content, indent, options);
}
function genTextNode(node, indent, options) {
return genMultilineText(node.content, indent, options);
}
function genMultilineText(content, indent, options) {
if (content.startsWith('\n')) {
content = content.trimStart();
}
if (content.includes('\n')) {
content = content
.split('\n')
.map((line) => line.trim())
.join('\n' + ' '.repeat(indent + options.indent));
}
return content;
}
function genRootNode(node, indent, options) {
return genChildren(node, indent, options);
}
function genElementNode(node, indent, options) {
const code = [];
code.push(' '.repeat(indent), '<', node.tag);
let shouldIndentClosing = false;
const props = applyReplaceNodes(node.props, options);
if (props.length > 0) {
if (props.length > 2) {
code.push('\n');
node.props.forEach((prop) => {
code.push(' '.repeat(indent + options.indent));
code.push(genNode(prop, indent + options.indent, options));
code.push('\n');
});
shouldIndentClosing = true;
}
else {
props.forEach((prop) => {
code.push(' ');
code.push(genNode(prop, indent, options));
});
}
}
if (shouldIndentClosing)
code.push(' '.repeat(indent));
if (node.isSelfClosing) {
if (!shouldIndentClosing)
code.push(' ');
code.push('/>');
}
else {
code.push('>', genChildren(node, indent, options), '</', node.tag, '>');
}
return code.join('');
}
function genChildren(node, indent, options) {
const code = [];
const children = applyReplaceNodes(node.children, options);
if (children.length > 0) {
const hasOnlyInlineChildren = children.every((child) => !isElementNode(child));
if (hasOnlyInlineChildren) {
children.forEach((child) => {
code.push(genNode(child, indent + options.indent, options));
});
}
else {
let wasLastChildInline = true;
children.forEach((child) => {
if (isTextNode(child) && child.content.trim() === '')
return; // Ignore empty text nodes.
const isThisChildInline = !isElementNode(child);
if (wasLastChildInline && isThisChildInline) ;
else if (wasLastChildInline) {
code.push('\n');
}
else if (isThisChildInline) {
code.push('\n', ' '.repeat(indent + options.indent));
}
else {
code.push('\n');
}
code.push(genNode(child, indent + options.indent, options));
wasLastChildInline = isThisChildInline;
});
code.push('\n', ' '.repeat(indent));
}
}
return code.join('');
}
function applyReplaceNodes(nodes, options) {
return nodes
.map((node) => {
if (options.replaceNodes.has(node))
return options.replaceNodes.get(node);
return node;
})
.filter(Boolean);
}
function genDirectiveNode(node, indent, options) {
const code = [];
if (options.directive === 'shorthand' && node.name in shorthands) {
code.push(shorthands[node.name]);
}
else {
code.push(`v-${node.name}`);
if (node.arg != null)
code.push(':');
}
if (isSimpleExpressionNode(node.arg)) {
if (node.arg.isStatic)
code.push(genNode(node.arg, indent, options));
else
code.push('[', genNode(node.arg, indent, options), ']');
}
node.modifiers.forEach((modifier) => code.push('.', modifier));
if (isSimpleExpressionNode(node.exp)) {
code.push('="', genNode(node.exp, indent, options), '"');
}
return code.join('');
}
function genAttributeNode(node, indent, options) {
return node.value != null
? `${node.name}="${genNode(node.value, indent, options)}"`
: node.name;
}
function findTemplateNodeAt(ast, position) {
return findTemplateNodeInRange(ast, position, position);
}
function findTemplateChildNodeAt(ast, position, mode) {
var _a, _b;
const result = findTemplateNodeInRange(ast, position, position, mode);
while (result.ancestors.length > 0) {
if (isRootNode(result.node) ||
isElementNode(result.node) ||
isTextNode(result.node) ||
isInterpolationNode(result.node) ||
isCommentNode(result.node)) {
break;
}
result.node = (_b = (_a = result.ancestors.pop()) === null || _a === void 0 ? void 0 : _a.node) !== null && _b !== void 0 ? _b : null;
}
return result;
}
function findTemplateNodeInRange(ast, start, end, mode) {
const found = {
node: null,
ancestors: [],
};
traverseEvery(ast, (node, ancestors) => {
if (mode === 'start'
? node.loc.start.offset <= start && end < node.loc.end.offset
: mode === 'end'
? node.loc.start.offset < start && end <= node.loc.end.offset
: node.loc.start.offset <= start && end <= node.loc.end.offset) {
found.node = node;
found.ancestors = ancestors.slice();
return true;
}
else {
return false;
}
});
return found;
}
function findTemplateNodesInRange(ast, start, end) {
const found = [];
traverseFast(ast, (node) => {
if (node.loc.start.offset <= start && end <= node.loc.end.offset) {
found.push(node);
}
});
return found;
}
function findTemplateChildrenInRange(ast, start, end) {
var _a, _b;
if (start === end) {
const a = findTemplateChildNodeAt(ast, start);
return a.node != null ? [a.node] : [];
}
const a = findTemplateChildNodeAt(ast, start, 'start');
const b = findTemplateChildNodeAt(ast, end, 'end');
if (a.node == null || b.node == null)
return [];
if (a.node === b.node)
return [a.node];
const pa = (_a = a.ancestors.pop()) === null || _a === void 0 ? void 0 : _a.node;
const pb = (_b = b.ancestors.pop()) === null || _b === void 0 ? void 0 : _b.node;
if (pa == null || pb == null)
return [];
if (pa === b.node)
return [pa];
if (pb === a.node)
return [pb];
if (pa === pb && isElementNode(pa)) {
return pa.children.slice(pa.children.indexOf(a.node), 1 + pa.children.indexOf(b.node));
}
return [];
}
export { findTemplateChildNodeAt, findTemplateChildrenInRange, findTemplateNodeAt, findTemplateNodeInRange, findTemplateNodesInRange, isAttributeNode, isCommentNode, isComponentNode, isCompoundExpressionNode, isDirectiveNode, isElementNode, isForNode, isIfNode, isInterpolationNode, isNode, isPlainElementNode, isRootNode, isSimpleExpressionNode, isSimpleIdentifier, isSlotNode, isTemplateNode, isTextNode, stringify, traverse, traverseEvery, traverseFast };
//# sourceMappingURL=index.esm.js.map

2

package.json
{
"name": "@vuedx/template-ast-types",
"version": "0.3.1-insiders-1607576746.0",
"version": "0.3.1-insiders-1607577772.0",
"description": "Helper functions for Vue template AST",

@@ -5,0 +5,0 @@ "main": "dist/index.cjs.js",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc