fix(ngcc): do not add trailing commas in UMD imports (#34545)

Previously, if `UmdRenderingFormatter#addImports()` was called with an
empty list of imports to add (i.e. no new imports were needed), it would
add trailing commas in several locations (arrays, function arguments,
function parameters), thus making the code imcompatible with legacy
browsers such as IE11.

This commit fixes it by ensuring that no trailing commas are added if
`addImports()` is called with an empty list of imports.
This is a follow-up to #34353.

Fixes #34525

PR Close #34545
This commit is contained in:
George Kalpakas
2019-12-23 16:07:34 +02:00
committed by Alex Rickabaugh
parent 4f42de9704
commit 10e29355db
8 changed files with 74 additions and 1 deletions

View File

@ -187,6 +187,17 @@ var core = require('@angular/core');
var i0 = require('@angular/core');
var i1 = require('@angular/common');`);
});
it('should leave the file unchanged if there are no imports to add', () => {
const {renderer, sourceFile} = setup(PROGRAM);
const output = new MagicString(PROGRAM.contents);
const contentsBefore = output.toString();
renderer.addImports(output, [], sourceFile);
const contentsAfter = output.toString();
expect(contentsAfter).toBe(contentsBefore);
});
});
describe('addExports', () => {

View File

@ -195,6 +195,17 @@ import {Directive} from '@angular/core';
import * as i0 from '@angular/core';
import * as i1 from '@angular/common';`);
});
it('should leave the file unchanged if there are no imports to add', () => {
const {renderer, sourceFile} = setup(PROGRAM);
const output = new MagicString(PROGRAM.contents);
const contentsBefore = output.toString();
renderer.addImports(output, [], sourceFile);
const contentsAfter = output.toString();
expect(contentsAfter).toBe(contentsBefore);
});
});
describe('addExports', () => {

View File

@ -112,6 +112,17 @@ import {Directive} from '@angular/core';
import * as i0 from '@angular/core';
import * as i1 from '@angular/common';`);
});
it('should leave the file unchanged if there are no imports to add', () => {
const {renderer, sourceFile} = setup([PROGRAM]);
const output = new MagicString(PROGRAM.contents);
const contentsBefore = output.toString();
renderer.addImports(output, [], sourceFile);
const contentsAfter = output.toString();
expect(contentsAfter).toBe(contentsBefore);
});
});
describe('addExports', () => {

View File

@ -325,6 +325,18 @@ typeof define === 'function' && define.amd ? define('file', ['exports','/tslib',
expect(outputSrc).toContain(`(factory(global.ng.core,global.ng.common));`);
expect(outputSrc).toContain(`(function (i0,i1) {'use strict';`);
});
it('should leave the file unchanged if there are no imports to add', () => {
const {renderer, program} = setup(PROGRAM);
const file = getSourceFileOrError(program, _('/node_modules/test-package/some/file.js'));
const output = new MagicString(PROGRAM.contents);
const contentsBefore = output.toString();
renderer.addImports(output, [], file);
const contentsAfter = output.toString();
expect(contentsAfter).toBe(contentsBefore);
});
});
describe('addExports', () => {