我找到了粘贴的代码,下面 checkin 了我们的 React/Typescript - JSX/TSX 项目。它包含尚未解决的 Git merge 冲突。
代码构建(通过 Fuse-box)并在浏览器中运行!
“转译”代码导致顶部元素被解析为组件的根元素,而第二个元素(以“.app-container”开头)被忽略。
看来,在某些情况下,可以通过 TSX/JSX 解释/解析/转译来忽略 git merge 冲突。
我的问题是:这是如何工作的?这种行为是故意的吗?
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
);
最佳答案
我知道这是一个很晚的答案(几乎一年后),但以防万一有人在谷歌上搜索这个并想知道同样的事情。是的,这种行为是故意在 typescript 词法分析器上有代码来处理 git merge 冲突的!注意:在解析器上跳过琐事默认为 true。
if (isConflictMarkerTrivia(text, pos)) {
pos = scanConflictMarkerTrivia(text, pos, error);
if (skipTrivia) {
continue;
}
else {
return token = SyntaxKind.ConflictMarkerTrivia;
}
}
下面是处理 merge 冲突的代码:
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/0ef0b7adea643a4a28cf9ada1476ff5650a1a9f2/src/compiler/scanner.ts#L565
关于javascript - React、Typescript 项目中 merge 冲突如何有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46299089/