Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

code-block-writer

Package Overview
Dependencies
Maintainers
1
Versions
86
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

code-block-writer - npm Package Compare versions

Comparing version 6.7.1 to 6.7.2

10

CHANGELOG.md

@@ -5,2 +5,12 @@ # Change Log

<a name="6.7.2"></a>
## [6.7.2](https://github.com/dsherret/code-block-writer/compare/v6.7.1...v6.7.2) (2018-03-31)
### Bug Fixes
* Should not be in a string when using a string char in a regular expression literal. ([70e88a5](https://github.com/dsherret/code-block-writer/commit/70e88a5))
<a name="6.7.1"></a>

@@ -7,0 +17,0 @@ ## [6.7.1](https://github.com/dsherret/code-block-writer/compare/v6.7.0...v6.7.1) (2018-03-27)

3

dist/code-block-writer.d.ts

@@ -12,2 +12,3 @@ export default class CodeBlockWriter {

private _stringCharStack;
private _isInRegEx;
constructor(opts?: {

@@ -150,3 +151,3 @@ newLine?: string;

private _baseWriteNewline();
private _updateStringStack(str);
private _updateInternalState(str);
private _writeIndentation();

@@ -153,0 +154,0 @@ private _newLineIfNewLineOnNextWrite();

@@ -13,2 +13,3 @@ "use strict";

this._stringCharStack = [];
this._isInRegEx = false;
this._newLine = (opts && opts.newLine) || "\n";

@@ -248,3 +249,3 @@ this._useTabs = (opts && opts.useTabs) || false;

}
this._updateStringStack(s);
this._updateInternalState(s);
this._text += s;

@@ -258,7 +259,19 @@ }

};
CodeBlockWriter.prototype._updateStringStack = function (str) {
CodeBlockWriter.prototype._updateInternalState = function (str) {
for (var i = 0; i < str.length; i++) {
var currentChar = str[i];
var pastChar = i === 0 ? this.getLastChar() : str[i - 1];
var lastCharOnStack = this._stringCharStack.length === 0 ? undefined : this._stringCharStack[this._stringCharStack.length - 1];
var pastChar = i === 0 ? this._text[this._text.length - 1] : str[i - 1];
var pastPastChar = i < 1 ? this._text[this._text.length - 2 + i] : str[i - 2];
// handle regex
if (this._isInRegEx) {
if (pastChar === "/" && pastPastChar !== "\\" || pastChar === "\n")
this._isInRegEx = false;
else
continue;
}
else if (!this.isInString() && !this.isInComment() && isRegExStart(currentChar, pastChar, pastPastChar)) {
this._isInRegEx = true;
continue;
}
// handle comments
if (this._currentCommentChar == null && pastChar === "/" && currentChar === "/")

@@ -272,13 +285,22 @@ this._currentCommentChar = CommentChar_1.CommentChar.Line;

continue;
else if (currentChar === "\"" || currentChar === "'" || currentChar === "`") {
if (lastCharOnStack === currentChar)
// handle strings
var lastStringCharOnStack = this._stringCharStack.length === 0 ? undefined : this._stringCharStack[this._stringCharStack.length - 1];
if (currentChar === "\"" || currentChar === "'" || currentChar === "`") {
if (lastStringCharOnStack === currentChar)
this._stringCharStack.pop();
else if (lastCharOnStack === "{" || lastCharOnStack === undefined)
else if (lastStringCharOnStack === "{" || lastStringCharOnStack === undefined)
this._stringCharStack.push(currentChar);
}
else if (pastChar === "$" && currentChar === "{" && lastCharOnStack === "`")
else if (pastChar === "$" && currentChar === "{" && lastStringCharOnStack === "`")
this._stringCharStack.push(currentChar);
else if (currentChar === "}" && lastCharOnStack === "{")
else if (currentChar === "}" && lastStringCharOnStack === "{")
this._stringCharStack.pop();
}
function isRegExStart(currentChar, pastChar, pastPastChar) {
return pastChar === "/"
&& currentChar !== "/"
&& currentChar !== "*"
&& pastPastChar !== "*"
&& pastPastChar !== "/";
}
};

@@ -285,0 +307,0 @@ CodeBlockWriter.prototype._writeIndentation = function () {

{
"name": "code-block-writer",
"version": "6.7.1",
"version": "6.7.2",
"description": "A simple code writer that assists with formatting and visualizing blocks of code.",

@@ -5,0 +5,0 @@ "main": "dist/code-block-writer.js",

@@ -17,2 +17,3 @@ import {stringRepeat, escapeForWithinString} from "./utils/stringUtils";

private _stringCharStack: ("\"" | "'" | "`" | "{")[] = [];
private _isInRegEx = false;

@@ -327,3 +328,3 @@ constructor(opts?: { newLine?: string; indentNumberOfSpaces?: number; useTabs?: boolean; useSingleQuote?: boolean; }) {

this._updateStringStack(s);
this._updateInternalState(s);
this._text += s;

@@ -339,8 +340,21 @@ }

private _updateStringStack(str: string) {
private _updateInternalState(str: string) {
for (let i = 0; i < str.length; i++) {
const currentChar = str[i];
const pastChar = i === 0 ? this.getLastChar() : str[i - 1];
const lastCharOnStack = this._stringCharStack.length === 0 ? undefined : this._stringCharStack[this._stringCharStack.length - 1];
const pastChar = i === 0 ? this._text[this._text.length - 1] : str[i - 1];
const pastPastChar = i < 1 ? this._text[this._text.length - 2 + i] : str[i - 2];
// handle regex
if (this._isInRegEx) {
if (pastChar === "/" && pastPastChar !== "\\" || pastChar === "\n")
this._isInRegEx = false;
else
continue;
}
else if (!this.isInString() && !this.isInComment() && isRegExStart(currentChar, pastChar, pastPastChar)) {
this._isInRegEx = true;
continue;
}
// handle comments
if (this._currentCommentChar == null && pastChar === "/" && currentChar === "/")

@@ -355,13 +369,24 @@ this._currentCommentChar = CommentChar.Line;

continue;
else if (currentChar === "\"" || currentChar === "'" || currentChar === "`") {
if (lastCharOnStack === currentChar)
// handle strings
const lastStringCharOnStack = this._stringCharStack.length === 0 ? undefined : this._stringCharStack[this._stringCharStack.length - 1];
if (currentChar === "\"" || currentChar === "'" || currentChar === "`") {
if (lastStringCharOnStack === currentChar)
this._stringCharStack.pop();
else if (lastCharOnStack === "{" || lastCharOnStack === undefined)
else if (lastStringCharOnStack === "{" || lastStringCharOnStack === undefined)
this._stringCharStack.push(currentChar);
}
else if (pastChar === "$" && currentChar === "{" && lastCharOnStack === "`")
else if (pastChar === "$" && currentChar === "{" && lastStringCharOnStack === "`")
this._stringCharStack.push(currentChar);
else if (currentChar === "}" && lastCharOnStack === "{")
else if (currentChar === "}" && lastStringCharOnStack === "{")
this._stringCharStack.pop();
}
function isRegExStart(currentChar: string, pastChar: string, pastPastChar: string) {
return pastChar === "/"
&& currentChar !== "/"
&& currentChar !== "*"
&& pastPastChar !== "*"
&& pastPastChar !== "/";
}
}

@@ -368,0 +393,0 @@

@@ -815,3 +815,3 @@ import * as assert from "assert";

writer.write(str[i]);
assert.equal(writer.isInString(), expectedValues[i + 1]);
assert.equal(writer.isInString(), expectedValues[i + 1], `at expected position ${i + 1}`);
}

@@ -875,2 +875,18 @@ }

});
it("should not be in a string for regex using a single quote", () => {
doTest("/'test/", [false, false, false, false, false, false, false, false]);
});
it("should not be in a string for regex using a double quote", () => {
doTest("/\"test/", [false, false, false, false, false, false, false, false]);
});
it("should not be in a string for regex using a back tick", () => {
doTest("/`test/", [false, false, false, false, false, false, false, false]);
});
it("should be in a string for a string after a regex", () => {
doTest("/`/'t'", [false, false, false, false, true, true, false]);
});
});

@@ -877,0 +893,0 @@

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