refactor(bazel): Fix warning about overridden tsconfig options (#28674)
Under Bazel, some compilerOptions in tsconfig.json are controlled by downstream rules. The default tsconfig.json causes Bazel to print out warnings about overriden settings. This commit makes a backup of the original tsconfig.json and removes tsconfig settings that are controlled by Bazel. As part of this fix, JsonAst utils are refactored into separate package and unit tests are added. PR closes https://github.com/angular/angular/issues/28034 PR Close #28674
This commit is contained in:

committed by
Misko Hevery

parent
841a1d32e1
commit
65c2deacbb
30
packages/bazel/src/schematics/utility/BUILD.bazel
Normal file
30
packages/bazel/src/schematics/utility/BUILD.bazel
Normal file
@ -0,0 +1,30 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load("//tools:defaults.bzl", "ts_library")
|
||||
|
||||
ts_library(
|
||||
name = "utility",
|
||||
srcs = [
|
||||
"json-utils.ts",
|
||||
],
|
||||
module_name = "@angular/bazel/src/schematics/utility",
|
||||
deps = [
|
||||
"@ngdeps//@angular-devkit/core",
|
||||
"@ngdeps//@angular-devkit/schematics",
|
||||
"@ngdeps//@schematics/angular",
|
||||
"@ngdeps//typescript",
|
||||
],
|
||||
)
|
||||
|
||||
ts_library(
|
||||
name = "test",
|
||||
testonly = True,
|
||||
srcs = [
|
||||
"json-utils_spec.ts",
|
||||
],
|
||||
deps = [
|
||||
":utility",
|
||||
"@ngdeps//@angular-devkit/core",
|
||||
"@ngdeps//@angular-devkit/schematics",
|
||||
],
|
||||
)
|
65
packages/bazel/src/schematics/utility/json-utils.ts
Normal file
65
packages/bazel/src/schematics/utility/json-utils.ts
Normal file
@ -0,0 +1,65 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {JsonAstNode, JsonAstObject, JsonValue} from '@angular-devkit/core';
|
||||
import {UpdateRecorder} from '@angular-devkit/schematics';
|
||||
import {findPropertyInAstObject} from '@schematics/angular/utility/json-utils';
|
||||
|
||||
/**
|
||||
* Replace the value of the key-value pair in the 'node' object with a different
|
||||
* 'value' and record the update using the specified 'recorder'.
|
||||
*/
|
||||
export function replacePropertyInAstObject(
|
||||
recorder: UpdateRecorder, node: JsonAstObject, propertyName: string, value: JsonValue,
|
||||
indent: number = 0) {
|
||||
const property = findPropertyInAstObject(node, propertyName);
|
||||
if (property === null) {
|
||||
throw new Error(`Property '${propertyName}' does not exist in JSON object`);
|
||||
}
|
||||
const {start, text} = property;
|
||||
recorder.remove(start.offset, text.length);
|
||||
const indentStr = '\n' +
|
||||
' '.repeat(indent);
|
||||
const content = JSON.stringify(value, null, ' ').replace(/\n/g, indentStr);
|
||||
recorder.insertLeft(start.offset, content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the key-value pair with the specified 'key' in the specified 'node'
|
||||
* object and record the update using the specified 'recorder'.
|
||||
*/
|
||||
export function removeKeyValueInAstObject(
|
||||
recorder: UpdateRecorder, content: string, node: JsonAstObject, key: string) {
|
||||
for (const [i, prop] of node.properties.entries()) {
|
||||
if (prop.key.value === key) {
|
||||
const start = prop.start.offset;
|
||||
const end = prop.end.offset;
|
||||
let length = end - start;
|
||||
const match = content.slice(end).match(/[,\s]+/);
|
||||
if (match) {
|
||||
length += match.pop() !.length;
|
||||
}
|
||||
recorder.remove(start, length);
|
||||
if (i === node.properties.length - 1) { // last property
|
||||
let offset = 0;
|
||||
while (/(,|\s)/.test(content.charAt(start - offset - 1))) {
|
||||
offset++;
|
||||
}
|
||||
recorder.remove(start - offset, offset);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the specified 'node' is a JsonAstObject, false otherwise.
|
||||
*/
|
||||
export function isJsonAstObject(node: JsonAstNode | null): node is JsonAstObject {
|
||||
return !!node && node.kind === 'object';
|
||||
}
|
109
packages/bazel/src/schematics/utility/json-utils_spec.ts
Normal file
109
packages/bazel/src/schematics/utility/json-utils_spec.ts
Normal file
@ -0,0 +1,109 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {JsonAstObject, parseJsonAst} from '@angular-devkit/core';
|
||||
import {HostTree} from '@angular-devkit/schematics';
|
||||
import {UnitTestTree} from '@angular-devkit/schematics/testing';
|
||||
import {isJsonAstObject, removeKeyValueInAstObject, replacePropertyInAstObject} from './json-utils';
|
||||
|
||||
describe('JsonUtils', () => {
|
||||
|
||||
let tree: UnitTestTree;
|
||||
beforeEach(() => { tree = new UnitTestTree(new HostTree()); });
|
||||
|
||||
describe('replacePropertyInAstObject', () => {
|
||||
it('should replace property', () => {
|
||||
const content = JSON.stringify({foo: {bar: 'baz'}});
|
||||
tree.create('tmp', content);
|
||||
const ast = parseJsonAst(content) as JsonAstObject;
|
||||
const recorder = tree.beginUpdate('tmp');
|
||||
replacePropertyInAstObject(recorder, ast, 'foo', [1, 2, 3]);
|
||||
tree.commitUpdate(recorder);
|
||||
const value = tree.readContent('tmp');
|
||||
expect(JSON.parse(value)).toEqual({
|
||||
foo: [1, 2, 3],
|
||||
});
|
||||
expect(value).toBe(`{"foo":[
|
||||
1,
|
||||
2,
|
||||
3
|
||||
]}`);
|
||||
});
|
||||
|
||||
it('should respect the indent parameter', () => {
|
||||
const content = JSON.stringify({hello: 'world'}, null, 2);
|
||||
tree.create('tmp', content);
|
||||
const ast = parseJsonAst(content) as JsonAstObject;
|
||||
const recorder = tree.beginUpdate('tmp');
|
||||
replacePropertyInAstObject(recorder, ast, 'hello', 'world!', 2);
|
||||
tree.commitUpdate(recorder);
|
||||
const value = tree.readContent('tmp');
|
||||
expect(JSON.parse(value)).toEqual({
|
||||
hello: 'world!',
|
||||
});
|
||||
expect(value).toBe(`{
|
||||
"hello": "world!"
|
||||
}`);
|
||||
});
|
||||
|
||||
it('should throw error if property is not found', () => {
|
||||
const content = JSON.stringify({});
|
||||
tree.create('tmp', content);
|
||||
const ast = parseJsonAst(content) as JsonAstObject;
|
||||
const recorder = tree.beginUpdate('tmp');
|
||||
expect(() => replacePropertyInAstObject(recorder, ast, 'foo', 'bar'))
|
||||
.toThrowError(`Property 'foo' does not exist in JSON object`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('removeKeyValueInAstObject', () => {
|
||||
it('should remove key-value pair', () => {
|
||||
const content = JSON.stringify({hello: 'world', foo: 'bar'});
|
||||
tree.create('tmp', content);
|
||||
const ast = parseJsonAst(content) as JsonAstObject;
|
||||
let recorder = tree.beginUpdate('tmp');
|
||||
removeKeyValueInAstObject(recorder, content, ast, 'foo');
|
||||
tree.commitUpdate(recorder);
|
||||
const tmp = tree.readContent('tmp');
|
||||
expect(JSON.parse(tmp)).toEqual({
|
||||
hello: 'world',
|
||||
});
|
||||
expect(tmp).toBe('{"hello":"world"}');
|
||||
recorder = tree.beginUpdate('tmp');
|
||||
const newContent = tree.readContent('tmp');
|
||||
removeKeyValueInAstObject(recorder, newContent, ast, 'hello');
|
||||
tree.commitUpdate(recorder);
|
||||
const value = tree.readContent('tmp');
|
||||
expect(JSON.parse(value)).toEqual({});
|
||||
expect(value).toBe('{}');
|
||||
});
|
||||
|
||||
it('should be a noop if key is not found', () => {
|
||||
const content = JSON.stringify({foo: 'bar'});
|
||||
tree.create('tmp', content);
|
||||
const ast = parseJsonAst(content) as JsonAstObject;
|
||||
let recorder = tree.beginUpdate('tmp');
|
||||
expect(() => removeKeyValueInAstObject(recorder, content, ast, 'hello')).not.toThrow();
|
||||
tree.commitUpdate(recorder);
|
||||
const value = tree.readContent('tmp');
|
||||
expect(JSON.parse(value)).toEqual({foo: 'bar'});
|
||||
expect(value).toBe('{"foo":"bar"}');
|
||||
});
|
||||
});
|
||||
|
||||
describe('isJsonAstObject', () => {
|
||||
it('should return true for an object', () => {
|
||||
const ast = parseJsonAst(JSON.stringify({}));
|
||||
expect(isJsonAstObject(ast)).toBe(true);
|
||||
});
|
||||
it('should return false for a non-object', () => {
|
||||
const ast = parseJsonAst(JSON.stringify([]));
|
||||
expect(isJsonAstObject(ast)).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user