Skip to content

Commit

Permalink
fix(@schematics/angular): migrate TS module type to esnext
Browse files Browse the repository at this point in the history
  • Loading branch information
clydin authored and alexeagle committed Apr 22, 2019
1 parent 96fe768 commit 458ba75
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 458ba75

Please sign in to comment.