mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-06 12:01:40 +08:00
whatChanged: front_end/entrypoints/formatter_worker/JSONFormatter.ts created a JSONFormatter file to parse minified JSON files. front_end/core/common/ResourceType.ts : line 298 : isDocumentOrScriptOrStyleSheet() Added the resourceType of ‘xhr’ to allow a pretty print button to be set on .json files. sample video of working pp: Video: https://www.loom.com/share/2cbe6414caab4db1bdfd054cb3958119 Reproducible steps: visit: https://tv.youtube.com/welcome/ open devtools select network tab filter only json network responses Reload page and click on any json response. attempt to pretty print minifed json R=tvanderlippe@chromium.org Fixed: 998674 Change-Id: Idcc69b2f43d2e46882488c3bd6d4d933605fca6d Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2909881 Reviewed-by: Tim van der Lippe <tvanderlippe@chromium.org> Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org>
70 lines
2.1 KiB
TypeScript
70 lines
2.1 KiB
TypeScript
// Copyright 2021 The Chromium Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import type {FormattedContentBuilder} from './FormattedContentBuilder.js';
|
|
import {createTokenizer} from './FormatterWorker.js';
|
|
|
|
export class JSONFormatter {
|
|
builder: FormattedContentBuilder;
|
|
toOffset!: number;
|
|
fromOffset!: number;
|
|
lineEndings!: number[];
|
|
lastLine: number;
|
|
text!: string;
|
|
constructor(builder: FormattedContentBuilder) {
|
|
this.builder = builder;
|
|
this.lastLine = -1;
|
|
}
|
|
|
|
format(text: string, lineEndings: number[], fromOffset: number, toOffset: number): void {
|
|
this.lineEndings = lineEndings;
|
|
this.fromOffset = fromOffset;
|
|
this.toOffset = toOffset;
|
|
this.lastLine = -1;
|
|
this.text = text;
|
|
const tokenize = createTokenizer('application/json');
|
|
tokenize(text.substring(this.fromOffset, this.toOffset), this.tokenCallback.bind(this));
|
|
}
|
|
|
|
tokenCallback(token: string, type: string|null, startPosition: number): void {
|
|
switch (token.charAt(0)) {
|
|
case '{':
|
|
case '[':
|
|
if (this.text[startPosition + 1] === '}' || this.text[startPosition + 1] === ']') {
|
|
this.builder.addToken(token, startPosition);
|
|
} else {
|
|
this.builder.addToken(token, startPosition);
|
|
this.builder.addNewLine();
|
|
this.builder.increaseNestingLevel();
|
|
}
|
|
break;
|
|
case '}':
|
|
case ']':
|
|
if (this.text[startPosition - 1] === '{' || this.text[startPosition - 1] === '[') {
|
|
this.builder.addToken(token, startPosition);
|
|
} else {
|
|
this.builder.decreaseNestingLevel();
|
|
this.builder.addNewLine();
|
|
this.builder.addToken(token, startPosition);
|
|
}
|
|
break;
|
|
case ':':
|
|
this.builder.addToken(token, startPosition);
|
|
this.builder.addSoftSpace();
|
|
break;
|
|
case ',':
|
|
this.builder.addToken(token, startPosition);
|
|
this.builder.addNewLine();
|
|
break;
|
|
case '':
|
|
case ' ':
|
|
case '\n':
|
|
break;
|
|
default:
|
|
this.builder.addToken(token, startPosition);
|
|
break;
|
|
}
|
|
}
|
|
}
|