Added jsx to singleLineComment

This commit is contained in:
Armando Aguirre committed 2020-05-22 16:46:33 -07:00
1 parent 97de811d48
commit fc9753473e
13 files changed
+198 -77

No files matched your search

+30 -6
View File
@@ -1997,43 +1997,67 @@ namespace ts {
let leftMostPosition = Number.MAX_VALUE;
let lineTextStarts = new Map<number>();
const whiteSpaceRegex = new RegExp(/\S/);
const isJsx = isInsideJsxElement(sourceFile, lineStarts[firstLine])
const openComment = isJsx ? "{/*" : "//";
const closeComment = "*/}";
// First check the lines before any text changes.
for (let i = firstLine; i <= lastLine; i++) {
const lineText = sourceFile.text.substring(lineStarts[i], lineStarts[i + 1]); // TODO: Validate the end of line it might go outside of range.
const lineText = sourceFile.text.substring(lineStarts[i], sourceFile.getLineEndOfPosition(lineStarts[i]));
// Find the start of text and the left-most character. No-op on empty lines.
const regExec = whiteSpaceRegex.exec(lineText);
if (regExec) {
leftMostPosition = Math.min(leftMostPosition, regExec.index);
lineTextStarts.set(i.toString(), regExec.index);
// let sourceFilePosition = lineStarts[i] + leftMostPosition;
if (lineText.substr(regExec.index, 3) !== "// ") { // TODO: Validate when it is inside a comment. It can only uncomment if it's inside a comment. // TODO: Check when not finishing on empty space.
if (lineText.substr(regExec.index, openComment.length) !== openComment) { // TODO: Validate when it is inside a comment. It can only uncomment if it's inside a comment. // TODO: Check when not finishing on empty space.
isCommenting = true;
}
}
}
// Push all text changes.
for (let i = firstLine; i <= lastLine; i++) {
const lineTextStart = lineTextStarts.get(i.toString());
// If the line is not an empty line; otherwise no-op;
if (lineTextStart !== undefined) {
if (isCommenting) {
textChanges.push({
newText: "// ",
newText: openComment,
span: {
length: 0,
start: lineStarts[i] + leftMostPosition
}
});
if (isJsx) {
textChanges.push({
newText: closeComment,
span: {
length: 0,
start: sourceFile.getLineEndOfPosition(lineStarts[i])
}
});
}
} else {
textChanges.push({
newText: "",
span: {
length: 3,
length: openComment.length,
start: lineStarts[i] + lineTextStart
}
});
if (isJsx) {
textChanges.push({
newText: "",
span: {
length: closeComment.length,
start: sourceFile.getLineEndOfPosition(lineStarts[i]) - closeComment.length
}
});
}
}
}
}
@@ -2052,7 +2076,7 @@ namespace ts {
const positions = [] as number[] as SortedArray<number>;
let pos = textRange.pos;
const isJsx = isInsideJsxTags(sourceFile, pos);
const isJsx = isInsideJsxElement(sourceFile, pos);
const openMultiline = isJsx ? "{/*" : "/*";
const closeMultiline = isJsx ? "*/}" : "*/";
+20 -14
View File
@@ -1320,23 +1320,29 @@ namespace ts {
return false;
}
export function isInsideJsxTags(sourceFile: SourceFile, position: number) {
const token = getTokenAtPosition(sourceFile, position);
export function isInsideJsxElement(sourceFile: SourceFile, position: number): boolean {
function isInsideJsxElementRecursion(node: Node): boolean {
while (node) {
if (node.kind >= SyntaxKind.JsxSelfClosingElement && node.kind <= SyntaxKind.JsxExpression
|| node.kind === SyntaxKind.JsxText
|| node.kind === SyntaxKind.LessThanToken
|| node.kind === SyntaxKind.GreaterThanToken
|| node.kind === SyntaxKind.Identifier
|| node.kind === SyntaxKind.CloseBraceToken
|| node.kind === SyntaxKind.OpenBraceToken
|| node.kind === SyntaxKind.SlashToken) {
node = node.parent;
} else if (node.kind === SyntaxKind.JsxElement) {
return position > node.getStart(sourceFile) || isInsideJsxElementRecursion(node.parent);
} else {
return false;
}
}
switch (token.kind) {
case SyntaxKind.JsxText:
return true;
case SyntaxKind.LessThanToken:
case SyntaxKind.Identifier:
return token.parent.kind === SyntaxKind.JsxText // <div>Hello |</div>
|| token.parent.kind === SyntaxKind.JsxClosingElement // <div>|</div>
|| isJsxOpeningLikeElement(token.parent) && isJsxElement(token.parent.parent) // <div>|<component /></div> or <div><comp|onent /></div>
case SyntaxKind.CloseBraceToken:
case SyntaxKind.OpenBraceToken:
return isJsxExpression(token.parent) && isJsxElement(token.parent.parent); // <div>{|}</div> or <div>|{}</div>
return false;
}
return false;
return isInsideJsxElementRecursion(getTokenAtPosition(sourceFile, position));
}
export function findPrecedingMatchingToken(token: Node, matchingTokenKind: SyntaxKind, sourceFile: SourceFile) {