Comparing version 1.4.0 to 1.4.1
{ | ||
"name": "esrap", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"description": "Parse in reverse", | ||
@@ -5,0 +5,0 @@ "repository": { |
/** @import { TSESTree } from '@typescript-eslint/types' */ | ||
/** @import { Chunk, Command, Dedent, Handlers, Indent, Newline, NodeWithComments, Sequence, State, TypeAnnotationNodes } from './types' */ | ||
/** @import { Command, Dedent, Handlers, Location, Indent, Newline, NodeWithComments, State, TypeAnnotationNodes } from './types' */ | ||
@@ -14,7 +14,6 @@ /** @type {Newline} */ | ||
/** | ||
* @param {Command[]} children | ||
* @returns {Sequence} | ||
* @returns {Command[]} | ||
*/ | ||
function create_sequence(...children) { | ||
return { type: 'Sequence', children }; | ||
function create_sequence() { | ||
return []; | ||
} | ||
@@ -34,7 +33,7 @@ | ||
total += command.length; | ||
} else if (command.type === 'Chunk') { | ||
total += command.content.length; | ||
} else if (command.type === 'Sequence') { | ||
// assume this is ', ' | ||
total += 2; | ||
} else if (Array.isArray(command)) { | ||
total += | ||
command.length === 0 | ||
? 2 // assume this is ', ' | ||
: measure(command, 0); | ||
} | ||
@@ -72,12 +71,27 @@ } | ||
/** | ||
* @param {number} line | ||
* @param {number} column | ||
* @returns {Location} | ||
*/ | ||
function l(line, column) { | ||
return { | ||
type: 'Location', | ||
line, | ||
column | ||
}; | ||
} | ||
/** | ||
* @param {string} content | ||
* @param {TSESTree.Node} node | ||
* @returns {Chunk} | ||
* @returns {string | Command[]} | ||
*/ | ||
function c(content, node) { | ||
return { | ||
type: 'Chunk', | ||
content, | ||
loc: node?.loc ?? null | ||
}; | ||
return node.loc | ||
? [ | ||
l(node.loc.start.line, node.loc.start.column), | ||
content, | ||
l(node.loc.end.line, node.loc.end.column) | ||
] | ||
: content; | ||
} | ||
@@ -107,3 +121,22 @@ | ||
function quote(string, char) { | ||
return char + string.replaceAll(char, '\\' + char) + char; | ||
let out = char; | ||
let escaped = false; | ||
for (const c of string) { | ||
if (escaped) { | ||
out += c; | ||
escaped = false; | ||
} else if (c === '\\') { | ||
out += '\\\\'; | ||
escaped = true; | ||
} else if (c === char) { | ||
out += '\\' + c; | ||
} else if (c === '\n') { | ||
out += '\\n'; | ||
} else { | ||
out += c; | ||
} | ||
} | ||
return out + char; | ||
} | ||
@@ -295,3 +328,3 @@ | ||
) { | ||
margin.children.push('\n'); | ||
margin.push('\n'); | ||
} | ||
@@ -340,7 +373,7 @@ | ||
state.multiline = true; | ||
if (node.declarations.length > 1) open.children.push(indent); | ||
join.children.push(',', newline); | ||
if (node.declarations.length > 1) open.push(indent); | ||
join.push(',', newline); | ||
if (node.declarations.length > 1) state.commands.push(dedent); | ||
} else { | ||
join.children.push(', '); | ||
join.push(', '); | ||
} | ||
@@ -417,9 +450,9 @@ }; | ||
open.children.push(indent, newline); | ||
join.children.push(newline); | ||
close.children.push(dedent, newline); | ||
open.push(indent, newline); | ||
join.push(newline); | ||
close.push(dedent, newline); | ||
} else { | ||
if (spaces) open.children.push(' '); | ||
join.children.push(' '); | ||
if (spaces) close.children.push(' '); | ||
if (spaces) open.push(' '); | ||
join.push(' '); | ||
if (spaces) close.push(' '); | ||
} | ||
@@ -720,7 +753,7 @@ } | ||
if (multiline) { | ||
open.children.push(indent, newline); | ||
join.children.push(',', newline); | ||
close.children.push(dedent, newline); | ||
open.push(indent, newline); | ||
join.push(',', newline); | ||
close.push(dedent, newline); | ||
} else { | ||
join.children.push(', '); | ||
join.push(', '); | ||
} | ||
@@ -917,8 +950,8 @@ }, | ||
if (multiline) { | ||
if_true.children.push(indent, newline, '? '); | ||
if_false.children.push(newline, ': '); | ||
if_true.push(indent, newline, '? '); | ||
if_false.push(newline, ': '); | ||
state.commands.push(dedent); | ||
} else { | ||
if_true.children.push(' ? '); | ||
if_false.children.push(' : '); | ||
if_true.push(' ? '); | ||
if_false.push(' : '); | ||
} | ||
@@ -925,0 +958,0 @@ }, |
@@ -82,26 +82,17 @@ /** @import { TSESTree } from '@typescript-eslint/types' */ | ||
if (Array.isArray(command)) { | ||
for (let i = 0; i < command.length; i += 1) { | ||
run(command[i]); | ||
} | ||
return; | ||
} | ||
switch (command.type) { | ||
case 'Chunk': | ||
const loc = command.loc; | ||
if (loc) { | ||
current_line.push([ | ||
current_column, | ||
0, // source index is always zero | ||
loc.start.line - 1, | ||
loc.start.column | ||
]); | ||
} | ||
append(command.content); | ||
if (loc) { | ||
current_line.push([ | ||
current_column, | ||
0, // source index is always zero | ||
loc.end.line - 1, | ||
loc.end.column | ||
]); | ||
} | ||
case 'Location': | ||
current_line.push([ | ||
current_column, | ||
0, // source index is always zero | ||
command.line - 1, | ||
command.column | ||
]); | ||
break; | ||
@@ -121,9 +112,2 @@ | ||
case 'Sequence': | ||
for (let i = 0; i < command.children.length; i += 1) { | ||
run(command.children[i]); | ||
} | ||
break; | ||
case 'Comment': | ||
@@ -130,0 +114,0 @@ if (command.comment.type === 'Line') { |
@@ -41,9 +41,6 @@ import { TSESTree } from '@typescript-eslint/types'; | ||
export interface Chunk { | ||
type: 'Chunk'; | ||
content: string; | ||
loc: null | { | ||
start: { line: number; column: number }; | ||
end: { line: number; column: number }; | ||
}; | ||
export interface Location { | ||
type: 'Location'; | ||
line: number; | ||
column: number; | ||
} | ||
@@ -68,7 +65,2 @@ | ||
export interface Sequence { | ||
type: 'Sequence'; | ||
children: Command[]; | ||
} | ||
export interface CommentChunk { | ||
@@ -79,3 +71,3 @@ type: 'Comment'; | ||
export type Command = string | Chunk | Newline | Indent | Dedent | Sequence | CommentChunk; | ||
export type Command = string | Location | Newline | Indent | Dedent | CommentChunk | Command[]; | ||
@@ -82,0 +74,0 @@ export interface PrintOptions { |
Sorry, the diff of this file is not supported yet
1558
50043