@microsoft/powerquery-parser
Advanced tools
Comparing version
@@ -25,3 +25,4 @@ import { Ast, NodeIdMap } from "."; | ||
export declare function endContext(state: State, contextNode: Node, astNode: Ast.TNode): Option<Node>; | ||
export declare function deleteAst(state: State, nodeId: number, parentWillBeDeleted: boolean): void; | ||
export declare function deleteContext(state: State, nodeId: number): Option<Node>; | ||
export declare function deepCopy(state: State): State; |
@@ -76,2 +76,45 @@ "use strict"; | ||
exports.endContext = endContext; | ||
function deleteAst(state, nodeId, parentWillBeDeleted) { | ||
const nodeIdMapCollection = state.nodeIdMapCollection; | ||
const astNodeById = nodeIdMapCollection.astNodeById; | ||
const parentIdById = nodeIdMapCollection.parentIdById; | ||
const childIdsById = nodeIdMapCollection.childIdsById; | ||
if (!astNodeById.has(nodeId)) { | ||
const details = { nodeId }; | ||
throw new common_1.CommonError.InvariantError(`Ast nodeId not in state.`, details); | ||
} | ||
// If Node was a leaf node, remove it from the list of leaf nodes. | ||
removeLeafOrNoop(state, nodeId); | ||
const maybeParentId = parentIdById.get(nodeId); | ||
const maybeChildIds = childIdsById.get(nodeId); | ||
// Not a leaf node. | ||
if (maybeChildIds !== undefined) { | ||
const childIds = maybeChildIds; | ||
const details = { | ||
childIds, | ||
nodeId, | ||
}; | ||
throw new common_1.CommonError.InvariantError(`Ast maybeChildIds !== undefined`, details); | ||
} | ||
// Is a leaf node, not root node. | ||
// Delete the node from the list of children under the node's parent. | ||
else if (maybeParentId) { | ||
const parentId = maybeParentId; | ||
if (astNodeById.has(parentId) && !parentWillBeDeleted) { | ||
const details = { | ||
parentId, | ||
nodeId, | ||
}; | ||
throw new common_1.CommonError.InvariantError(`parent is a Ast node not marked for deletion`, details); | ||
} | ||
removeOrReplaceChildId(nodeIdMapCollection, parentId, nodeId, undefined); | ||
} | ||
// Else is root node, is leaf node. | ||
// No children updates need to be taken. | ||
// Remove the node from existence. | ||
astNodeById.delete(nodeId); | ||
childIdsById.delete(nodeId); | ||
parentIdById.delete(nodeId); | ||
} | ||
exports.deleteAst = deleteAst; | ||
function deleteContext(state, nodeId) { | ||
@@ -85,12 +128,7 @@ const nodeIdMapCollection = state.nodeIdMapCollection; | ||
const details = { nodeId }; | ||
throw new common_1.CommonError.InvariantError(`nodeId not in state.`, details); | ||
throw new common_1.CommonError.InvariantError(`Context nodeId not in state.`, details); | ||
} | ||
const node = maybeNode; | ||
// If Node was a leaf node, remove it from the list of leaf nodes. | ||
const leafNodeIds = state.leafNodeIds; | ||
const maybeLeafIndex = leafNodeIds.indexOf(nodeId); | ||
if (maybeLeafIndex !== -1) { | ||
const leafIndex = maybeLeafIndex; | ||
state.leafNodeIds = [...leafNodeIds.slice(0, leafIndex), ...leafNodeIds.slice(leafIndex + 1)]; | ||
} | ||
removeLeafOrNoop(state, nodeId); | ||
const maybeParentId = parentIdById.get(nodeId); | ||
@@ -106,3 +144,3 @@ const maybeChildIds = childIdsById.get(nodeId); | ||
}; | ||
throw new common_1.CommonError.InvariantError(`childIds.length !== 0`, details); | ||
throw new common_1.CommonError.InvariantError(`Context childIds.length !== 0`, details); | ||
} | ||
@@ -162,2 +200,10 @@ const childId = childIds[0]; | ||
exports.deepCopy = deepCopy; | ||
function removeLeafOrNoop(state, nodeId) { | ||
const leafNodeIds = state.leafNodeIds; | ||
const maybeLeafIndex = leafNodeIds.indexOf(nodeId); | ||
if (maybeLeafIndex !== -1) { | ||
const leafIndex = maybeLeafIndex; | ||
state.leafNodeIds = [...leafNodeIds.slice(0, leafIndex), ...leafNodeIds.slice(leafIndex + 1)]; | ||
} | ||
} | ||
function removeOrReplaceChildId(nodeIdMapCollection, parentId, childId, maybeReplacementId) { | ||
@@ -176,11 +222,29 @@ const childIdsById = nodeIdMapCollection.childIdsById; | ||
const afterChildId = childIds.slice(replacementIndex + 1); | ||
let maybeNewChildIds; | ||
if (maybeReplacementId) { | ||
const replacementId = maybeReplacementId; | ||
childIdsById.set(parentId, [...beforeChildId, replacementId, ...afterChildId]); | ||
nodeIdMapCollection.parentIdById.set(replacementId, parentId); | ||
if (childIds.length === 1) { | ||
maybeNewChildIds = [replacementId]; | ||
} | ||
else { | ||
maybeNewChildIds = [...beforeChildId, replacementId, ...afterChildId]; | ||
} | ||
} | ||
else { | ||
childIdsById.set(parentId, [...beforeChildId, ...afterChildId]); | ||
if (childIds.length === 1) { | ||
maybeNewChildIds = undefined; | ||
} | ||
else { | ||
maybeNewChildIds = [...beforeChildId, ...afterChildId]; | ||
} | ||
} | ||
if (maybeNewChildIds) { | ||
const newChildIds = maybeNewChildIds; | ||
childIdsById.set(parentId, newChildIds); | ||
} | ||
else { | ||
childIdsById.delete(parentId); | ||
} | ||
} | ||
//# sourceMappingURL=context.js.map |
@@ -62,11 +62,23 @@ "use strict"; | ||
const contextState = state.contextState; | ||
const nodeIdMapCollection = state.contextState.nodeIdMapCollection; | ||
const backupIdCounter = backup.contextStateIdCounter; | ||
contextState.idCounter = backupIdCounter; | ||
const newNodeIds = []; | ||
for (const nodeId of contextState.nodeIdMapCollection.contextNodeById.keys()) { | ||
const newContextNodeIds = []; | ||
const newAstNodeIds = []; | ||
for (const nodeId of nodeIdMapCollection.astNodeById.keys()) { | ||
if (nodeId > backupIdCounter) { | ||
newNodeIds.push(nodeId); | ||
newAstNodeIds.push(nodeId); | ||
} | ||
} | ||
for (const nodeId of newNodeIds.sort().reverse()) { | ||
for (const nodeId of nodeIdMapCollection.contextNodeById.keys()) { | ||
if (nodeId > backupIdCounter) { | ||
newContextNodeIds.push(nodeId); | ||
} | ||
} | ||
for (const nodeId of newAstNodeIds.sort().reverse()) { | ||
const maybeParent = nodeIdMapCollection.parentIdById.get(nodeId); | ||
const parentWillBeDeleted = maybeParent !== undefined && maybeParent >= backupIdCounter; | ||
__1.ParserContext.deleteAst(state.contextState, nodeId, parentWillBeDeleted); | ||
} | ||
for (const nodeId of newContextNodeIds.sort().reverse()) { | ||
__1.ParserContext.deleteContext(state.contextState, nodeId); | ||
@@ -73,0 +85,0 @@ } |
{ | ||
"name": "@microsoft/powerquery-parser", | ||
"version": "0.1.23", | ||
"version": "0.1.24", | ||
"description": "A parser for the Power Query/M formula language.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
526465
-19.93%118
-7.09%7012
-19.3%