Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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