refactor(): use const and let instead of var

This commit is contained in:
Joao Dias
2016-11-12 14:08:58 +01:00
committed by Victor Berchet
parent 73593d4bf3
commit 77ee27c59e
435 changed files with 4637 additions and 4663 deletions

View File

@ -74,7 +74,7 @@ export class MetadataCollector {
}
function classMetadataOf(classDeclaration: ts.ClassDeclaration): ClassMetadata {
let result: ClassMetadata = {__symbolic: 'class'};
const result: ClassMetadata = {__symbolic: 'class'};
function getDecorators(decorators: ts.Decorator[]): MetadataSymbolicExpression[] {
if (decorators && decorators.length)
@ -102,7 +102,7 @@ export class MetadataCollector {
let members: MetadataMap = null;
function recordMember(name: string, metadata: MemberMetadata) {
if (!members) members = {};
let data = members.hasOwnProperty(name) ? members[name] : [];
const data = members.hasOwnProperty(name) ? members[name] : [];
data.push(metadata);
members[name] = data;
}
@ -279,7 +279,7 @@ export class MetadataCollector {
case ts.SyntaxKind.EnumDeclaration:
if (node.flags & ts.NodeFlags.Export) {
const enumDeclaration = <ts.EnumDeclaration>node;
let enumValueHolder: {[name: string]: MetadataValue} = {};
const enumValueHolder: {[name: string]: MetadataValue} = {};
const enumName = enumDeclaration.name.text;
let nextDefaultValue: MetadataValue = 0;
let writtenMembers = 0;
@ -321,9 +321,9 @@ export class MetadataCollector {
break;
case ts.SyntaxKind.VariableStatement:
const variableStatement = <ts.VariableStatement>node;
for (let variableDeclaration of variableStatement.declarationList.declarations) {
for (const variableDeclaration of variableStatement.declarationList.declarations) {
if (variableDeclaration.name.kind == ts.SyntaxKind.Identifier) {
let nameNode = <ts.Identifier>variableDeclaration.name;
const nameNode = <ts.Identifier>variableDeclaration.name;
let varValue: MetadataValue;
if (variableDeclaration.initializer) {
varValue = evaluator.evaluateNode(variableDeclaration.initializer);
@ -530,7 +530,7 @@ function validateMetadata(
const node = nodeMap.get(entry);
if (shouldReportNode(node)) {
if (node) {
let {line, character} = sourceFile.getLineAndCharacterOfPosition(node.getStart());
const {line, character} = sourceFile.getLineAndCharacterOfPosition(node.getStart());
throw new Error(
`${sourceFile.fileName}:${line + 1}:${character + 1}: Error encountered in metadata generated for exported symbol '${name}': \n ${e.message}`);
}
@ -543,7 +543,7 @@ function validateMetadata(
// Collect parameter names from a function.
function namesOf(parameters: ts.NodeArray<ts.ParameterDeclaration>): string[] {
let result: string[] = [];
const result: string[] = [];
function addNamesOf(name: ts.Identifier | ts.BindingPattern) {
if (name.kind == ts.SyntaxKind.Identifier) {
@ -551,13 +551,13 @@ function namesOf(parameters: ts.NodeArray<ts.ParameterDeclaration>): string[] {
result.push(identifier.text);
} else {
const bindingPattern = <ts.BindingPattern>name;
for (let element of bindingPattern.elements) {
for (const element of bindingPattern.elements) {
addNamesOf(element.name);
}
}
}
for (let parameter of parameters) {
for (const parameter of parameters) {
addNamesOf(parameter.name);
}

View File

@ -78,7 +78,7 @@ export function errorSymbol(
if (node) {
sourceFile = sourceFile || getSourceFileOfNode(node);
if (sourceFile) {
let {line, character} =
const {line, character} =
ts.getLineAndCharacterOfPosition(sourceFile, node.getStart(sourceFile));
result = {__symbolic: 'error', message, line, character};
};
@ -260,7 +260,7 @@ export class Evaluator {
// Handle spread expressions
if (isMetadataSymbolicSpreadExpression(value)) {
if (Array.isArray(value.expression)) {
for (let spreadValue of value.expression) {
for (const spreadValue of value.expression) {
arr.push(spreadValue);
}
return;
@ -389,7 +389,7 @@ export class Evaluator {
return {__symbolic: 'select', expression: left, member: qualifiedName.right.text};
} else {
const identifier = <ts.Identifier>typeNameNode;
let symbol = this.symbols.resolve(identifier.text);
const symbol = this.symbols.resolve(identifier.text);
if (isMetadataError(symbol) || isMetadataSymbolicReferenceExpression(symbol)) {
return recordEntry(symbol, node);
}

View File

@ -32,7 +32,7 @@ export class Symbols {
}
private buildImports(): void {
let symbols = this._symbols;
const symbols = this._symbols;
// Collect the imported symbols into this.symbols
const stripQuotes = (s: string) => s.replace(/^['"]|['"]$/g, '');
const visit = (node: ts.Node) => {
@ -84,7 +84,7 @@ export class Symbols {
switch (bindings.kind) {
case ts.SyntaxKind.NamedImports:
// An `import { [<identifier> [, <identifier>] } from <module-specifier>` clause
for (let binding of (<ts.NamedImports>bindings).elements) {
for (const binding of (<ts.NamedImports>bindings).elements) {
symbols.set(binding.name.text, {
__symbolic: 'reference',
module: from,

View File

@ -80,7 +80,7 @@ export class Tsc implements CompilerInterface {
// The issue is that old typescript only has `readDirectory` while
// the newer TypeScript has additional `useCaseSensitiveFileNames` and
// `fileExists`. Inlining will trigger an error of extra parameters.
let host = {
const host = {
useCaseSensitiveFileNames: true,
fileExists: existsSync,
readDirectory: this.readDirectory
@ -103,10 +103,10 @@ export class Tsc implements CompilerInterface {
debug('Checking global diagnostics...');
check(program.getGlobalDiagnostics());
let diagnostics: ts.Diagnostic[] = [];
const diagnostics: ts.Diagnostic[] = [];
debug('Type checking...');
for (let sf of program.getSourceFiles()) {
for (const sf of program.getSourceFiles()) {
diagnostics.push(...ts.getPreEmitDiagnostics(program, sf));
}
check(diagnostics);
@ -117,7 +117,7 @@ export class Tsc implements CompilerInterface {
const program = ts.createProgram(this.parsed.fileNames, this.parsed.options, compilerHost);
debug('Emitting outputs...');
const emitResult = program.emit();
let diagnostics: ts.Diagnostic[] = [];
const diagnostics: ts.Diagnostic[] = [];
diagnostics.push(...emitResult.diagnostics);
check(compilerHost.diagnostics);

View File

@ -14,7 +14,7 @@ import {ClassMetadata, ConstructorMetadata, ModuleMetadata} from '../src/schema'
import {Directory, Host, expectValidSources} from './typescript.mocks';
describe('Collector', () => {
let documentRegistry = ts.createDocumentRegistry();
const documentRegistry = ts.createDocumentRegistry();
let host: Host;
let service: ts.LanguageService;
let program: ts.Program;
@ -234,8 +234,8 @@ describe('Collector', () => {
});
it('should report errors for destructured imports', () => {
let unsupported1 = program.getSourceFile('/unsupported-1.ts');
let metadata = collector.getMetadata(unsupported1);
const unsupported1 = program.getSourceFile('/unsupported-1.ts');
const metadata = collector.getMetadata(unsupported1);
expect(metadata).toEqual({
__symbolic: 'module',
version: 1,
@ -250,11 +250,11 @@ describe('Collector', () => {
});
it('should report an error for references to unexpected types', () => {
let unsupported1 = program.getSourceFile('/unsupported-2.ts');
let metadata = collector.getMetadata(unsupported1);
let barClass = <ClassMetadata>metadata.metadata['Bar'];
let ctor = <ConstructorMetadata>barClass.members['__ctor__'][0];
let parameter = ctor.parameters[0];
const unsupported1 = program.getSourceFile('/unsupported-2.ts');
const metadata = collector.getMetadata(unsupported1);
const barClass = <ClassMetadata>metadata.metadata['Bar'];
const ctor = <ConstructorMetadata>barClass.members['__ctor__'][0];
const parameter = ctor.parameters[0];
expect(parameter).toEqual({
__symbolic: 'error',
message: 'Reference to non-exported class',
@ -265,19 +265,19 @@ describe('Collector', () => {
});
it('should be able to handle import star type references', () => {
let importStar = program.getSourceFile('/import-star.ts');
let metadata = collector.getMetadata(importStar);
let someClass = <ClassMetadata>metadata.metadata['SomeClass'];
let ctor = <ConstructorMetadata>someClass.members['__ctor__'][0];
let parameters = ctor.parameters;
const importStar = program.getSourceFile('/import-star.ts');
const metadata = collector.getMetadata(importStar);
const someClass = <ClassMetadata>metadata.metadata['SomeClass'];
const ctor = <ConstructorMetadata>someClass.members['__ctor__'][0];
const parameters = ctor.parameters;
expect(parameters).toEqual([
{__symbolic: 'reference', module: 'angular2/common', name: 'NgFor'}
]);
});
it('should be able to record functions', () => {
let exportedFunctions = program.getSourceFile('/exported-functions.ts');
let metadata = collector.getMetadata(exportedFunctions);
const exportedFunctions = program.getSourceFile('/exported-functions.ts');
const metadata = collector.getMetadata(exportedFunctions);
expect(metadata).toEqual({
__symbolic: 'module',
version: 1,
@ -334,36 +334,36 @@ describe('Collector', () => {
});
it('should be able to handle import star type references', () => {
let importStar = program.getSourceFile('/import-star.ts');
let metadata = collector.getMetadata(importStar);
let someClass = <ClassMetadata>metadata.metadata['SomeClass'];
let ctor = <ConstructorMetadata>someClass.members['__ctor__'][0];
let parameters = ctor.parameters;
const importStar = program.getSourceFile('/import-star.ts');
const metadata = collector.getMetadata(importStar);
const someClass = <ClassMetadata>metadata.metadata['SomeClass'];
const ctor = <ConstructorMetadata>someClass.members['__ctor__'][0];
const parameters = ctor.parameters;
expect(parameters).toEqual([
{__symbolic: 'reference', module: 'angular2/common', name: 'NgFor'}
]);
});
it('should be able to collect the value of an enum', () => {
let enumSource = program.getSourceFile('/exported-enum.ts');
let metadata = collector.getMetadata(enumSource);
let someEnum: any = metadata.metadata['SomeEnum'];
const enumSource = program.getSourceFile('/exported-enum.ts');
const metadata = collector.getMetadata(enumSource);
const someEnum: any = metadata.metadata['SomeEnum'];
expect(someEnum).toEqual({A: 0, B: 1, C: 100, D: 101});
});
it('should ignore a non-export enum', () => {
let enumSource = program.getSourceFile('/private-enum.ts');
let metadata = collector.getMetadata(enumSource);
let publicEnum: any = metadata.metadata['PublicEnum'];
let privateEnum: any = metadata.metadata['PrivateEnum'];
const enumSource = program.getSourceFile('/private-enum.ts');
const metadata = collector.getMetadata(enumSource);
const publicEnum: any = metadata.metadata['PublicEnum'];
const privateEnum: any = metadata.metadata['PrivateEnum'];
expect(publicEnum).toEqual({a: 0, b: 1, c: 2});
expect(privateEnum).toBeUndefined();
});
it('should be able to collect enums initialized from consts', () => {
let enumSource = program.getSourceFile('/exported-enum.ts');
let metadata = collector.getMetadata(enumSource);
let complexEnum: any = metadata.metadata['ComplexEnum'];
const enumSource = program.getSourceFile('/exported-enum.ts');
const metadata = collector.getMetadata(enumSource);
const complexEnum: any = metadata.metadata['ComplexEnum'];
expect(complexEnum).toEqual({
A: 0,
B: 1,
@ -374,10 +374,10 @@ describe('Collector', () => {
});
it('should be able to collect a simple static method', () => {
let staticSource = program.getSourceFile('/static-method.ts');
let metadata = collector.getMetadata(staticSource);
const staticSource = program.getSourceFile('/static-method.ts');
const metadata = collector.getMetadata(staticSource);
expect(metadata).toBeDefined();
let classData = <ClassMetadata>metadata.metadata['MyModule'];
const classData = <ClassMetadata>metadata.metadata['MyModule'];
expect(classData).toBeDefined();
expect(classData.statics).toEqual({
with: {
@ -392,10 +392,10 @@ describe('Collector', () => {
});
it('should be able to collect a call to a static method', () => {
let staticSource = program.getSourceFile('/static-method-call.ts');
let metadata = collector.getMetadata(staticSource);
const staticSource = program.getSourceFile('/static-method-call.ts');
const metadata = collector.getMetadata(staticSource);
expect(metadata).toBeDefined();
let classData = <ClassMetadata>metadata.metadata['Foo'];
const classData = <ClassMetadata>metadata.metadata['Foo'];
expect(classData).toBeDefined();
expect(classData.decorators).toEqual([{
__symbolic: 'call',
@ -415,19 +415,19 @@ describe('Collector', () => {
});
it('should be able to collect a static field', () => {
let staticSource = program.getSourceFile('/static-field.ts');
let metadata = collector.getMetadata(staticSource);
const staticSource = program.getSourceFile('/static-field.ts');
const metadata = collector.getMetadata(staticSource);
expect(metadata).toBeDefined();
let classData = <ClassMetadata>metadata.metadata['MyModule'];
const classData = <ClassMetadata>metadata.metadata['MyModule'];
expect(classData).toBeDefined();
expect(classData.statics).toEqual({VALUE: 'Some string'});
});
it('should be able to collect a reference to a static field', () => {
let staticSource = program.getSourceFile('/static-field-reference.ts');
let metadata = collector.getMetadata(staticSource);
const staticSource = program.getSourceFile('/static-field-reference.ts');
const metadata = collector.getMetadata(staticSource);
expect(metadata).toBeDefined();
let classData = <ClassMetadata>metadata.metadata['Foo'];
const classData = <ClassMetadata>metadata.metadata['Foo'];
expect(classData).toBeDefined();
expect(classData.decorators).toEqual([{
__symbolic: 'call',
@ -446,10 +446,10 @@ describe('Collector', () => {
});
it('should be able to collect a method with a conditional expression', () => {
let source = program.getSourceFile('/static-method-with-if.ts');
let metadata = collector.getMetadata(source);
const source = program.getSourceFile('/static-method-with-if.ts');
const metadata = collector.getMetadata(source);
expect(metadata).toBeDefined();
let classData = <ClassMetadata>metadata.metadata['MyModule'];
const classData = <ClassMetadata>metadata.metadata['MyModule'];
expect(classData).toBeDefined();
expect(classData.statics).toEqual({
with: {
@ -471,10 +471,10 @@ describe('Collector', () => {
});
it('should be able to collect a method with a default parameter', () => {
let source = program.getSourceFile('/static-method-with-default.ts');
let metadata = collector.getMetadata(source);
const source = program.getSourceFile('/static-method-with-default.ts');
const metadata = collector.getMetadata(source);
expect(metadata).toBeDefined();
let classData = <ClassMetadata>metadata.metadata['MyModule'];
const classData = <ClassMetadata>metadata.metadata['MyModule'];
expect(classData).toBeDefined();
expect(classData.statics).toEqual({
with: {
@ -500,8 +500,8 @@ describe('Collector', () => {
});
it('should be able to collect re-exported symbols', () => {
let source = program.getSourceFile('/re-exports.ts');
let metadata = collector.getMetadata(source);
const source = program.getSourceFile('/re-exports.ts');
const metadata = collector.getMetadata(source);
expect(metadata.exports).toEqual([
{from: './static-field', export: ['MyModule']},
{from: './static-field-reference', export: [{name: 'Foo', as: 'OtherModule'}]},
@ -510,8 +510,8 @@ describe('Collector', () => {
});
it('should collect an error symbol if collecting a reference to a non-exported symbol', () => {
let source = program.getSourceFile('/local-symbol-ref.ts');
let metadata = collector.getMetadata(source);
const source = program.getSourceFile('/local-symbol-ref.ts');
const metadata = collector.getMetadata(source);
expect(metadata.metadata).toEqual({
REQUIRED_VALIDATOR: {
__symbolic: 'error',
@ -532,8 +532,8 @@ describe('Collector', () => {
});
it('should collect an error symbol if collecting a reference to a non-exported function', () => {
let source = program.getSourceFile('/local-function-ref.ts');
let metadata = collector.getMetadata(source);
const source = program.getSourceFile('/local-function-ref.ts');
const metadata = collector.getMetadata(source);
expect(metadata.metadata).toEqual({
REQUIRED_VALIDATOR: {
__symbolic: 'error',
@ -554,8 +554,8 @@ describe('Collector', () => {
});
it('should collect an error for a simple function that references a local variable', () => {
let source = program.getSourceFile('/local-symbol-ref-func.ts');
let metadata = collector.getMetadata(source);
const source = program.getSourceFile('/local-symbol-ref-func.ts');
const metadata = collector.getMetadata(source);
expect(metadata.metadata).toEqual({
foo: {
__symbolic: 'function',
@ -573,18 +573,18 @@ describe('Collector', () => {
describe('in strict mode', () => {
it('should throw if an error symbol is collecting a reference to a non-exported symbol', () => {
let source = program.getSourceFile('/local-symbol-ref.ts');
const source = program.getSourceFile('/local-symbol-ref.ts');
expect(() => collector.getMetadata(source, true)).toThrowError(/Reference to a local symbol/);
});
it('should throw if an error if collecting a reference to a non-exported function', () => {
let source = program.getSourceFile('/local-function-ref.ts');
const source = program.getSourceFile('/local-function-ref.ts');
expect(() => collector.getMetadata(source, true))
.toThrowError(/Reference to a non-exported function/);
});
it('should throw for references to unexpected types', () => {
let unsupported1 = program.getSourceFile('/unsupported-2.ts');
const unsupported1 = program.getSourceFile('/unsupported-2.ts');
expect(() => collector.getMetadata(unsupported1, true))
.toThrowError(/Reference to non-exported class/);
});
@ -594,14 +594,14 @@ describe('Collector', () => {
it('should not throw with a class with no name', () => {
const fileName = '/invalid-class.ts';
override(fileName, 'export class');
let invalidClass = program.getSourceFile(fileName);
const invalidClass = program.getSourceFile(fileName);
expect(() => collector.getMetadata(invalidClass)).not.toThrow();
});
it('should not throw with a function with no name', () => {
const fileName = '/invalid-function.ts';
override(fileName, 'export function');
let invalidFunction = program.getSourceFile(fileName);
const invalidFunction = program.getSourceFile(fileName);
expect(() => collector.getMetadata(invalidFunction)).not.toThrow();
});
});

View File

@ -15,7 +15,7 @@ import {Symbols} from '../src/symbols';
import {Directory, Host, expectNoDiagnostics, findVar} from './typescript.mocks';
describe('Evaluator', () => {
let documentRegistry = ts.createDocumentRegistry();
const documentRegistry = ts.createDocumentRegistry();
let host: ts.LanguageServiceHost;
let service: ts.LanguageService;
let program: ts.Program;

View File

@ -60,13 +60,13 @@ describe('Symbols', () => {
});
it('should be able to create symbols for a source file', () => {
let symbols = new Symbols(expressions);
const symbols = new Symbols(expressions);
expect(symbols).toBeDefined();
});
it('should be able to find symbols in expression', () => {
let symbols = new Symbols(expressions);
const symbols = new Symbols(expressions);
expect(symbols.has('someName')).toBeTruthy();
expect(symbols.resolve('someName'))
.toEqual({__symbolic: 'reference', module: './consts', name: 'someName'});
@ -76,24 +76,24 @@ describe('Symbols', () => {
});
it('should be able to detect a * import', () => {
let symbols = new Symbols(imports);
const symbols = new Symbols(imports);
expect(symbols.resolve('b')).toEqual({__symbolic: 'reference', module: 'b'});
});
it('should be able to detect importing a default export', () => {
let symbols = new Symbols(imports);
const symbols = new Symbols(imports);
expect(symbols.resolve('d')).toEqual({__symbolic: 'reference', module: 'd', default: true});
});
it('should be able to import a renamed symbol', () => {
let symbols = new Symbols(imports);
const symbols = new Symbols(imports);
expect(symbols.resolve('g')).toEqual({__symbolic: 'reference', name: 'f', module: 'f'});
});
it('should be able to resolve any symbol in core global scope', () => {
let core = program.getSourceFiles().find(source => source.fileName.endsWith('lib.d.ts'));
const core = program.getSourceFiles().find(source => source.fileName.endsWith('lib.d.ts'));
expect(core).toBeDefined();
let visit = (node: ts.Node): boolean => {
const visit = (node: ts.Node): boolean => {
switch (node.kind) {
case ts.SyntaxKind.VariableStatement:
case ts.SyntaxKind.VariableDeclarationList:

View File

@ -23,7 +23,7 @@ export class Host implements ts.LanguageServiceHost {
getScriptVersion(fileName: string): string { return this.version.toString(); }
getScriptSnapshot(fileName: string): ts.IScriptSnapshot {
let content = this.getFileContent(fileName);
const content = this.getFileContent(fileName);
if (content) return ts.ScriptSnapshot.fromString(content);
}
@ -125,8 +125,8 @@ export class MockSymbol implements ts.Symbol {
export function expectNoDiagnostics(diagnostics: ts.Diagnostic[]) {
for (const diagnostic of diagnostics) {
let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
let {line, character} = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
const {line, character} = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
}
expect(diagnostics.length).toBe(0);