Skip to content

Commit

Permalink
refactor(lint): Don't allow console.log
Browse files Browse the repository at this point in the history
Enable tslint check for `console.log` as a follow-up to
angular#13018
  • Loading branch information
Bowen Ni authored and vsavkin committed Nov 23, 2016
1 parent 6c2d931 commit 2c02d34
Show file tree
Hide file tree
Showing 32 changed files with 38 additions and 31 deletions.
4 changes: 1 addition & 3 deletions modules/@angular/benchpress/test/firefox_extension/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ describe('firefox extension', function() {

browser.driver.get(TEST_URL);

browser.executeScript('window.startProfiler()').then(function() {
console.log('started measuring perf');
});
browser.executeScript('window.startProfiler()');

browser.executeAsyncScript('setTimeout(arguments[0], 1000);');
browser.executeScript('window.forceGC()');
Expand Down
2 changes: 1 addition & 1 deletion modules/@angular/compiler-cli/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class CodeGenerator {
let root = this.options.basePath;
for (const eachRootDir of this.options.rootDirs || []) {
if (this.options.trace) {
console.log(`Check if ${filePath} is under rootDirs element ${eachRootDir}`);
console.error(`Check if ${filePath} is under rootDirs element ${eachRootDir}`);
}
if (path.relative(eachRootDir, filePath).indexOf('.') !== 0) {
root = eachRootDir;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class PathMappedCompilerHost extends CompilerHost {
ts.resolveModuleName(m, rootedContainingFile, this.options, this.context).resolvedModule;
if (resolved) {
if (this.options.traceResolution) {
console.log('resolve', m, containingFile, '=>', resolved.resolvedFileName);
console.error('resolve', m, containingFile, '=>', resolved.resolvedFileName);
}
return this.getCanonicalFileName(resolved.resolvedFileName);
}
Expand All @@ -71,7 +71,7 @@ export class PathMappedCompilerHost extends CompilerHost {
*/
fileNameToModuleName(importedFile: string, containingFile: string): string {
if (this.options.traceResolution) {
console.log(
console.error(
'getImportPath from containingFile', containingFile, 'to importedFile', importedFile);
}

Expand Down
2 changes: 1 addition & 1 deletion modules/@angular/compiler/src/aot/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export function extractProgramSymbols(
files.filter(fileName => _filterFileByPatterns(fileName, options)).forEach(sourceFile => {
const moduleMetadata = staticReflector.getModuleMetadata(sourceFile);
if (!moduleMetadata) {
console.log(`WARNING: no metadata found for ${sourceFile}`);
console.error(`WARNING: no metadata found for ${sourceFile}`);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion modules/@angular/compiler/src/aot/static_reflector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class StaticReflector implements ReflectorReader {
}
return parameters;
} catch (e) {
console.log(`Failed on type ${JSON.stringify(type)} with error ${e}`);
console.error(`Failed on type ${JSON.stringify(type)} with error ${e}`);
throw e;
}
}
Expand Down
1 change: 0 additions & 1 deletion modules/@angular/examples/_common/e2e_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export function verifyNoBrowserErrors() {
const errors: any[] = [];
browserLog.filter(logEntry => {
const msg = logEntry.message;
console.log('>> ' + msg);
if (logEntry.level.value >= webdriver.logging.Level.INFO.value) {
errors.push(msg);
}
Expand Down
1 change: 1 addition & 0 deletions modules/@angular/examples/facade/ts/async/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

/* tslint:disable:no-console */
// #docregion Observable
import {Observable} from 'rxjs/Observable';
import {Subscriber} from 'rxjs/Subscriber';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

/* tslint:disable:no-console */
// #docregion Observable
import 'rxjs/add/operator/map';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

/* tslint:disable:no-console */
// #docregion Observable
import {Observable} from 'rxjs/Observable';
import {Subscriber} from 'rxjs/Subscriber';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

/* tslint:disable:no-console */
// #docregion Component
import {Component} from '@angular/core';
import {FormArray, FormControl, FormGroup} from '@angular/forms';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

/* tslint:disable:no-console */
// #docregion Component
import {Component} from '@angular/core';
import {FormControl, FormGroup, Validators} from '@angular/forms';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

/* tslint:disable:no-console */
// #docregion Component
import {Component} from '@angular/core';
import {NgForm} from '@angular/forms';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

/* tslint:disable:no-console */
// #docregion Component
import {Component} from '@angular/core';
import {NgForm} from '@angular/forms';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

/* tslint:disable:no-console */
// #docregion Component
import {Component} from '@angular/core';
import {FormControl, FormGroup, Validators} from '@angular/forms';
Expand Down
1 change: 1 addition & 0 deletions modules/@angular/facade/src/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export function isJsObject(o: any): boolean {
}

export function print(obj: Error | Object) {
// tslint:disable-next-line:no-console
console.log(obj);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {

log(error: string): void {
if (window.console) {
// tslint:disable-next-line:no-console
window.console.log && window.console.log(error);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ export class AngularProfiler {
(<any>window.console.profileEnd)(profileName);
}
const msPerTick = (end - start) / numTicks;
// tslint:disable-next-line:no-console
window.console.log(`ran ${numTicks} change detection cycles`);
// tslint:disable-next-line:no-console
window.console.log(`${msPerTick.toFixed(2)} ms per check`);

return new ChangeDetectionPerfRecord(msPerTick, numTicks);
Expand Down
1 change: 1 addition & 0 deletions modules/@angular/platform-server/src/parse5_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class Parse5DomAdapter extends DomAdapter {

logError(error: string) { console.error(error); }

// tslint:disable-next-line:no-console
log(error: string) { console.log(error); }

logGroup(error: string) { console.error(error); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ export class WorkerDomAdapter extends DomAdapter {
if (console.error) {
console.error(error);
} else {
// tslint:disable-next-line:no-console
console.log(error);
}
}

// tslint:disable-next-line:no-console
log(error: any /** TODO #9100 */) { console.log(error); }

logGroup(error: any /** TODO #9100 */) {
if (console.group) {
console.group(error);
this.logError(error);
} else {
// tslint:disable-next-line:no-console
console.log(error);
}
}
Expand Down
2 changes: 1 addition & 1 deletion modules/@angular/upgrade/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function stringify(obj: any): string {

export function onError(e: any) {
// TODO: (misko): We seem to not have a stack trace here!
console.log(e, e.stack);
console.error(e, e.stack);
throw e;
}

Expand Down
10 changes: 5 additions & 5 deletions modules/@angular/upgrade/test/upgrade_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function main() {

it('should throw an uncaught error', fakeAsync(() => {
const resolveSpy = jasmine.createSpy('resolveSpy');
spyOn(console, 'log');
spyOn(console, 'error');

expect(() => {
adapter.bootstrap(html('<ng2></ng2>'), ['ng1']).ready(resolveSpy);
Expand All @@ -50,14 +50,14 @@ export function main() {
expect(resolveSpy).not.toHaveBeenCalled();
}));

it('should properly log to the console and re-throw', fakeAsync(() => {
spyOn(console, 'log');
it('should output an error message to the console and re-throw', fakeAsync(() => {
spyOn(console, 'error');
expect(() => {
adapter.bootstrap(html('<ng2></ng2>'), ['ng1']);
flushMicrotasks();
}).toThrowError();
expect(console.log).toHaveBeenCalled();
expect(console.log).toHaveBeenCalledWith(jasmine.any(Error), jasmine.any(String));
expect(console.error).toHaveBeenCalled();
expect(console.error).toHaveBeenCalledWith(jasmine.any(Error), jasmine.any(String));
}));
});

Expand Down
1 change: 0 additions & 1 deletion modules/benchmarks/e2e_test/old/largetable_perf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe('ng2 largetable benchmark', function() {

['interpolation', 'interpolationAttr', 'interpolationFn'].forEach(function(benchmarkType) {
it('should log the ng stats with: ' + benchmarkType, function(done) {
console.log('executing for type', benchmarkType);
runClickBenchmark({
url: URL,
buttons: ['#ng2DestroyDom', '#ng2CreateDom'],
Expand Down
2 changes: 0 additions & 2 deletions modules/benchmarks/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export function profile(create: () => void, destroy: () => void, name: string) {
destroy();
}
window.console.profileEnd();
window.console.log(`Iterations: ${count}; time: ${duration / count} ms / iteration`);

window.console.profile(name + ' w/o GC');
duration = 0;
Expand All @@ -63,7 +62,6 @@ export function profile(create: () => void, destroy: () => void, name: string) {
destroy();
}
window.console.profileEnd();
window.console.log(`Iterations: ${count}; time: ${duration / count} ms / iteration`);
};
}

Expand Down
2 changes: 1 addition & 1 deletion modules/e2e_util/e2e_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function verifyNoBrowserErrors() {
browser.manage().logs().get('browser').then(function(browserLog: any) {
const filteredLog = browserLog.filter(function(logEntry: any) {
if (logEntry.level.value >= webdriver.logging.Level.INFO.value) {
console.log('>> ' + logEntry.message);
console.error('>> ' + logEntry.message);
}
return logEntry.level.value > webdriver.logging.Level.WARNING.value;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('WebWorkers Animations', function() {
browser.wait(protractor.until.elementLocated(by.css(selector + ' .box')), 5000)
.then(() => {}, () => {
// jasmine will timeout if this gets called too many times
console.log('>> unexpected timeout -> browser.refresh()');
console.error('>> unexpected timeout -> browser.refresh()');
browser.refresh();
waitForBootstrap();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('WebWorkers Input', function() {
},
() => {
// jasmine will timeout if this gets called too many times
console.log('>> unexpected timeout -> browser.refresh()');
console.error('>> unexpected timeout -> browser.refresh()');
browser.refresh();
waitForBootstrap();
});
Expand Down
1 change: 1 addition & 0 deletions modules/playground/src/model_driven_forms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

/* tslint:disable:no-console */
import {Component, Host, NgModule} from '@angular/core';
import {AbstractControl, FormBuilder, FormGroup, FormGroupDirective, ReactiveFormsModule, Validators} from '@angular/forms';
import {BrowserModule} from '@angular/platform-browser';
Expand Down
1 change: 1 addition & 0 deletions modules/playground/src/template_driven_forms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

/* tslint:disable:no-console */

import {Component, Directive, Host, NgModule} from '@angular/core';
import {FormControl, FormGroup, FormsModule, NG_VALIDATORS, NgForm} from '@angular/forms';
Expand Down
1 change: 1 addition & 0 deletions tools/@angular/tsc-wrapped/src/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface CompilerInterface {
const DEBUG = false;

function debug(msg: string, ...o: any[]) {
// tslint:disable-next-line:no-console
if (DEBUG) console.log(msg, ...o);
}

Expand Down
3 changes: 0 additions & 3 deletions tools/tsc-watch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ function processOutputEmitterCodeGen(): Promise<number> {
return new Promise((resolve, reject) => {
const outDir = 'dist/all/@angular/compiler/test/';
const promises: Promise<any>[] = [];
console.log('Processing codegen...');
OFFLINE_COMPILE.forEach((file: string) => {
const codegen = require('../../all/@angular/compiler/test/' + file + '.js');
if (codegen.emit) {
console.log(` ${file} has changed, regenerating...`);
promises.push(Promise.resolve(codegen.emit()).then((code) => {
writeFileSync(outDir + file + '.ts', code);
}));
Expand All @@ -36,7 +34,6 @@ function processOutputEmitterCodeGen(): Promise<number> {
.then(() => {
const args =
['--project', 'tools/cjs-jasmine/tsconfig-output_emitter_codegen.json'];
console.log(' compiling changes: tsc ' + args.join(' '));
const tsc = spawn(TSC, args, {stdio: 'pipe'});
tsc.stdout.on('data', (data: any) => process.stdout.write(data));
tsc.stderr.on('data', (data: any) => process.stderr.write(data));
Expand Down
10 changes: 3 additions & 7 deletions tools/tsc-watch/tsc_watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export class TscWatch {
start: string,
complete: string, onStartCmds?: Array<string[]|Command>, onChangeCmds?: Array<string[]|Command>
}) {
console.log('Watching:', tsconfig, 'in', process.cwd());
this.tsconfig = tsconfig;
this.start = start;
this.error = error;
Expand Down Expand Up @@ -67,14 +66,12 @@ export class TscWatch {
const args = argsOrCmd as Array<string>;
return <any>new Promise((resolve, reject) => {
const [cmd, ...options] = args;
console.log('=====>', cmd, options.join(' '));
const childProcess = spawn(cmd, options, {stdio: 'pipe'});
childProcess.stdout.on('data', stdOut);
childProcess.stderr.on('data', stdErr);
const onExit = () => childProcess.kill();
childProcess.on('close', (code: number) => {
process.removeListener('exit', onExit);
console.log('EXIT:', code, '<=====', args.join(' '));
code ? reject(code) : resolve(code);
});
process.on('exit', onExit);
Expand All @@ -98,23 +95,22 @@ export class TscWatch {
consumeLine(buffer: Buffer, isStdError: boolean) {
const line = '' + buffer;
if (contains(line, this.start)) {
console.log('==============================================================================');
stdOut(buffer, isStdError);
this.state = State.waiting;
} else if (contains(line, this.error)) {
stdOut(buffer, isStdError);
this.state = State.error;
} else if (contains(line, this.complete)) {
stdOut(buffer, isStdError);
console.log('------------------------------------------------------------------------------');
if (this.state == State.error) {
console.log('Errors found.... (response not triggered)');
console.error('Errors found.... (response not triggered)');
if (this.runOnce) process.exit(1);
this.state = State.idle;
} else {
if (this.triggered) {
this.triggered.then(
() => this.triggerCmds(), (e) => console.log('Error while running commands....', e));
() => this.triggerCmds(),
(e) => console.error('Error while running commands....', e));
} else {
this.triggerCmds();
}
Expand Down
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
],
"rules": {
"file-header": [true, "Copyright Google Inc\\."],
"no-console": [true, "log"],
"no-duplicate-imports": true,
"no-duplicate-variable": true,
"no-jasmine-focus": true,
Expand Down

0 comments on commit 2c02d34

Please sign in to comment.