Skip to content

Commit

Permalink
更新JS
Browse files Browse the repository at this point in the history
  • Loading branch information
akof1314 committed May 20, 2018
1 parent 9a884e7 commit 1781dd1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
14 changes: 9 additions & 5 deletions FormatterLib/JsFormatterLib/jsparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ void JSParser::GetTokenRaw()
bool bFirst = true;
bool bNum = false; // 是不是数字
bool bLineBegin = false;
char chQuote; // 记录引号类型 ' 或 "
char chComment; // 注释类型 / 或 *
char chQuote = 0; // 记录引号类型 ' 或 "
char chComment = 0; // 注释类型 / 或 *

while(1)
{
Expand Down Expand Up @@ -174,9 +174,13 @@ void JSParser::GetTokenRaw()
if(!bRegularFlags &&
(IsNormalChar(m_charB) || m_iRegBracket > 0))
{
// 正则的 flags 部分
// /g /i /ig...
bRegularFlags = true;
if(m_iRegBracket == 0)
{
// 正则的 flags 部分
// /g /i /ig...
// 否则 [] 中 / 不需要转移
bRegularFlags = true;
}
continue;
}
else
Expand Down
25 changes: 15 additions & 10 deletions FormatterLib/JsFormatterLib/realjsformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ RealJSFormatter::RealJSFormatter(const FormatterOption& option):

string RealJSFormatter::Trim(const string& str)
{
std::string ret(str);
string ret(str);
ret = ret.erase(ret.find_last_not_of(" \r\n\t") + 1);
return ret.erase(0, ret.find_first_not_of(" \r\n\t"));
}

string RealJSFormatter::TrimSpace(const string& str)
{
std::string ret(str);
string ret(str);
ret = ret.erase(ret.find_last_not_of(" \t") + 1);
return ret.erase(0, ret.find_first_not_of(" \t"));
}

string RealJSFormatter::TrimRightSpace(const string& str)
{
std::string ret(str);
string ret(str);
return ret.erase(ret.find_last_not_of(" \t") + 1);
}

Expand Down Expand Up @@ -127,9 +127,9 @@ void RealJSFormatter::PrintAdditionalDebug(string& strDebugOutput)
strDebugOutput.append(buf);
}

int RealJSFormatter::GetFormattedLine(unsigned int originalLine)
int RealJSFormatter::GetFormattedLine(int originalLine)
{
if(originalLine <= 0 || m_lineFormattedVec.size() <= originalLine)
if(originalLine <= 0 || m_lineFormattedVec.size() <= (size_t)originalLine)
return -1;

for(int l = originalLine; l > 0; --l)
Expand Down Expand Up @@ -223,9 +223,9 @@ void RealJSFormatter::PutString(const Token& token)
else
{
m_lineBuffer += token.code[i];
int tokenLine = token.line;
int tokenLine = (int)token.line;
if(tokenLine != -1)
m_lineWaitVec.push_back(token.line);
m_lineWaitVec.push_back(tokenLine);
}
}
}
Expand Down Expand Up @@ -253,8 +253,8 @@ void RealJSFormatter::PutLineBuffer()
break;
}

unsigned int oldLine = m_lineWaitVec[i];
if(oldLine >= m_lineFormattedVec.size())
int oldLine = m_lineWaitVec[i];
if((size_t)oldLine >= m_lineFormattedVec.size())
{
m_lineFormattedVec.resize(m_lineFormattedVec.size()*2, -1);
continue;
Expand Down Expand Up @@ -830,12 +830,17 @@ void RealJSFormatter::ProcessString(bool bHaveNewLine, char tokenAFirst, char to
bool bTokenAPropName = false;
if(m_tokenPreA.code == ".")
bTokenAPropName = true;

if(!bTokenAPropName &&
(m_tokenA.code == "case" || m_tokenA.code == "default"))
{
// case, default 往里面缩一格
--m_nIndents;
string rightDeco = m_tokenA.code != "default" ? string(" ") : string();
string rightDeco = string(" ");
if(m_tokenA.code == "default" && m_tokenB.code == ":")
{
rightDeco = string("");
}
PutToken(m_tokenA, string(""), rightDeco);
++m_nIndents;
m_blockStack.push(m_blockMap[m_tokenA.code]);
Expand Down
2 changes: 1 addition & 1 deletion FormatterLib/JsFormatterLib/realjsformatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class RealJSFormatter: public JSParser

virtual void Go();

int GetFormattedLine(unsigned int originalLine);
int GetFormattedLine(int originalLine);

private:
void Init();
Expand Down

0 comments on commit 1781dd1

Please sign in to comment.