[英]How is a merge conflict valid in a React, Typescript project
我發現下面粘貼的代碼簽入了我們的React / Typescript-JSX / TSX項目。 它包含一個Git合並沖突,尚未解決。
該代碼將構建(通過保險絲盒)並在瀏覽器中運行!
“已編譯”代碼導致將top元素解析為組件的根元素,而忽略第二個元素(“ .app-container”開始)。
看起來,在某些情況下,可以通過TSX / JSX解釋/解析/轉譯來忽略git合並沖突。
我的問題是:這項工作如何進行? 這種行為是故意的嗎?
export const App: SFC = () => (
<<<<<<< HEAD
<Provider store={window.uglySolution}>
<Router history={customHistory}>
<Switch>
<Route exact path='/' component={Welcome}/>
<Route exact path='/advice' component={PageLayout}/>
<Route exact path='/login' component={Login}/>
<Route exact path='/manage' component={ManageLayout}/>
</Switch>
</Router>
</Provider>
=======
<div className='app-container'>
<Provider store={window.uglySolution}>
<Router history={customHistory}>
<Switch>
<Route exact path='/' component={Welcome}/>
<Route exact path='/advice' component={PageLayout}/>
<Route exact path='/login' component={Login}/>
<Route exact path='/manage' component={ManageLayout}/>
</Switch>
</Router>
</Provider>
</div>
>>>>>>> f81b0b1b458e3d41f91faa2e726be6d88a35d9f8
);
我知道這是一個很晚的答案(將近一年后),但萬一有人正在使用谷歌搜索並想知道同一件事。 是的,在打字稿詞法分析器中故意存在代碼的行為來處理git合並沖突! 注意:在解析器上,跳過瑣事默認為true。
if (isConflictMarkerTrivia(text, pos)) {
pos = scanConflictMarkerTrivia(text, pos, error);
if (skipTrivia) {
continue;
}
else {
return token = SyntaxKind.ConflictMarkerTrivia;
}
}
下面是處理合並沖突的代碼:
function scanConflictMarkerTrivia(text: string, pos: number, error?: (diag: DiagnosticMessage, pos?: number, len?: number) => void) {
if (error) {
error(Diagnostics.Merge_conflict_marker_encountered, pos, mergeConflictMarkerLength);
}
const ch = text.charCodeAt(pos);
const len = text.length;
if (ch === CharacterCodes.lessThan || ch === CharacterCodes.greaterThan) {
while (pos < len && !isLineBreak(text.charCodeAt(pos))) {
pos++;
}
}
else {
Debug.assert(ch === CharacterCodes.bar || ch === CharacterCodes.equals);
// Consume everything from the start of a ||||||| or ======= marker to the start
// of the next ======= or >>>>>>> marker.
while (pos < len) {
const currentChar = text.charCodeAt(pos);
if ((currentChar === CharacterCodes.equals || currentChar === CharacterCodes.greaterThan) && currentChar !== ch && isConflictMarkerTrivia(text, pos)) {
break;
}
pos++;
}
}
return pos;
}
資料來源: https : //github.com/Microsoft/TypeScript/blob/0ef0b7adea643a4a28cf9ada1476ff5650a1a9f2/src/compiler/scanner.ts#L1604 https://github.com/Microsoft/TypeScript/blob/0ef0b7adea643a4a28cf9ada1476ff5650a1com9er #L565
看起來,在某些情況下,可以通過Tax / JSX解釋/解析/轉譯來解決git合並沖突。
給定的代碼是編譯錯誤 。 那就是說編譯錯誤並不意味着您不會生成任何JS 。 有一個選項noEmitOnError
可以防止這種情況的發生,但這表示即使沒有此選項,您也應該看到編譯錯誤並進行修復。
https://basarat.gitbooks.io/typescript/content/docs/why-typescript.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.