简体   繁体   English

合并冲突在React Typescript项目中如何有效

[英]How is a merge conflict valid in a React, Typescript project

I found the code, pasted, below checked in to our React/Typescript - JSX/TSX project. 我发现下面粘贴的代码签入了我们的React / Typescript-JSX / TSX项目。 It contains a Git merge conflict that hasn't been resolved. 它包含一个Git合并冲突,尚未解决。

The code builds (through Fuse-box) and runs in the browser! 该代码将构建(通过保险丝盒)并在浏览器中运行!

The "transpiled" code results in the top element being resolved as the root element of our component and the second element (beginning ".app-container") being ignored. “已编译”代码导致将top元素解析为组件的根元素,而忽略第二个元素(“ .app-container”开始)。

It appears, that in some situations, git merge conflicts can be ignored through TSX/JSX interpretation / parsing / transpilation. 看起来,在某些情况下,可以通过TSX / JSX解释/解析/转译来忽略git合并冲突。

My questions are: How does this work? 我的问题是:这项工作如何进行? Is this behaviour intentional? 这种行为是故意的吗?

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
);

I know this is quite a late answer (almost a year later) but in case somebody is googling this and wondering about the same thing. 我知道这是一个很晚的答案(将近一年后),但万一有人正在使用谷歌搜索并想知道同一件事。 Yes, the behavior intentional there is code right at the typescript lexer to handle git merge conflicts! 是的,在打字稿词法分析器中故意存在代码的行为来处理git合并冲突! Note: skip trivia defaults to true on the parser. 注意:在解析器上,跳过琐事默认为true。

if (isConflictMarkerTrivia(text, pos)) {
  pos = scanConflictMarkerTrivia(text, pos, error);
  if (skipTrivia) {
    continue;
}
else {
    return token = SyntaxKind.ConflictMarkerTrivia;
  }
}

And below is the code that handles merge conflicts: 下面是处理合并冲突的代码:

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;
}

sources: https://github.com/Microsoft/TypeScript/blob/0ef0b7adea643a4a28cf9ada1476ff5650a1a9f2/src/compiler/scanner.ts#L1604 https://github.com/Microsoft/TypeScript/blob/0ef0b7adea643a4a28cf9ada1476ff5650a1a9f2/src/compiler/scanner.ts#L565 资料来源: https : //github.com/Microsoft/TypeScript/blob/0ef0b7adea643a4a28cf9ada1476ff5650a1a9f2/src/compiler/scanner.ts#L1604 https://github.com/Microsoft/TypeScript/blob/0ef0b7adea643a4a28cf9ada1476ff5650a1com9er #L565

It appears, that in some situations git merge conflicts can be resolved though Tax/JSX interpretation / parsing / transpilation. 看起来,在某些情况下,可以通过Tax / JSX解释/解析/转译来解决git合并冲突。

The given code is a compile error . 给定的代码是编译错误 That said compile errors don't mean you don't get any JS generated . 那就是说编译错误并不意味着您不会生成任何JS There is an option noEmitOnError that will prevent this, but that said you should see a compile error and fix it even without this option. 有一个选项noEmitOnError可以防止这种情况的发生,但这表示即使没有此选项,您也应该看到编译错误并进行修复。

More 更多

https://basarat.gitbooks.io/typescript/content/docs/why-typescript.html https://basarat.gitbooks.io/typescript/content/docs/why-typescript.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM