fix(@schematics/angular): migrate TS module type to esnext

This commit is contained in:
Charles Lyding 2019-04-19 14:57:39 -04:00 committed by Alex Eagle
parent 96fe7686aa
commit 458ba75e37
2 changed files with 33 additions and 8 deletions

View File

@ -53,19 +53,26 @@ export function updateES5Projects(): Rule {
return host;
}
const scriptTarget = findPropertyInAstObject(compilerOptions, 'target');
if (scriptTarget && scriptTarget.value === 'es2015') {
return host;
}
const recorder = host.beginUpdate(tsConfigPath);
if (scriptTarget) {
const scriptTarget = findPropertyInAstObject(compilerOptions, 'target');
if (!scriptTarget) {
insertPropertyInAstObjectInOrder(recorder, compilerOptions, 'target', 'es2015', 4);
} else if (scriptTarget.value !== 'es2015') {
const { start, end } = scriptTarget;
recorder.remove(start.offset, end.offset - start.offset);
recorder.insertLeft(start.offset, '"es2015"');
} else {
insertPropertyInAstObjectInOrder(recorder, compilerOptions, 'target', 'es2015', 4);
}
const scriptModule = findPropertyInAstObject(compilerOptions, 'module');
if (!scriptModule) {
insertPropertyInAstObjectInOrder(recorder, compilerOptions, 'module', 'esnext', 4);
} else if (scriptModule.value !== 'esnext') {
const { start, end } = scriptModule;
recorder.remove(start.offset, end.offset - start.offset);
recorder.insertLeft(start.offset, '"esnext"');
}
host.commitUpdate(recorder);
return updateBrowserlist;

View File

@ -62,6 +62,24 @@ describe('Migration to version 8', () => {
expect(target).toBe('es2015');
});
it(`should update 'module' to esnext when property exists`, () => {
const tree2 = schematicRunner.runSchematic('migration-07', {}, tree.branch());
const { module } = JSON.parse(tree2.readContent(tsConfigPath)).compilerOptions;
expect(module).toBe('esnext');
});
it(`should create 'module' property when doesn't exists`, () => {
const compilerOptions = {
...oldTsConfig.compilerOptions,
module: undefined,
};
tree.overwrite(tsConfigPath, JSON.stringify({ compilerOptions }, null, 2));
const tree2 = schematicRunner.runSchematic('migration-07', {}, tree.branch());
const { module } = JSON.parse(tree2.readContent(tsConfigPath)).compilerOptions;
expect(module).toBe('esnext');
});
it(`should update browserslist file to add an non evergreen browser`, () => {
const tree2 = schematicRunner.runSchematic('migration-07', {}, tree.branch());
expect(tree2.readContent('/browserslist')).toContain('Chrome 41');