Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Enable strictNullChecks #462

Merged
merged 2 commits into from
Nov 9, 2016
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
2 changes: 1 addition & 1 deletion src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const globalArguments: ArgDescriptor[] = [
name: 'env',
description: 'The environment to use to specialize certain commands, '
+ 'like build',
type(value: string): Environment {
type(value: string): Environment|undefined {
return buildEnvironment(value);
},
group: 'global',
Expand Down
2 changes: 1 addition & 1 deletion src/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export async function build(options: BuildOptions, config: ProjectConfig): Promi
.once('data', () => { logger.info('Generating build/unbundled...'); })
.pipe(
gulpif(
options.insertDependencyLinks,
options.insertDependencyLinks || false,
new PrefetchTransform(polymerProject)
)
)
Expand Down
27 changes: 16 additions & 11 deletions src/build/prefetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ export class PrefetchTransform extends Transform {
type: 'import' | 'prefetch'
) {
let contents = file.contents.toString();
let ast = parse5.parse(contents);
let head = dom5.query(ast, dom5.predicates.hasTagName('head'));
const ast = parse5.parse(contents);
// parse5 always produces a <head> element
const head = dom5.query(ast, dom5.predicates.hasTagName('head'))!;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to constifying. Looks like typescript finds more and more places where they're able to do better type inference with const over let. (yes, const-like behavior can ~always be inferred but the convenience of not having to do that does seem to be paying out for the tsc team) microsoft/TypeScript#10676

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to only touch declarations near other edits for this PR. We can do this incrementally or do a constification PR to follow up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, agree and affirm.

for (let dep of deps) {
if (dep.startsWith(this.config.root)) {
dep = path.relative(file.dirname, dep);
Expand All @@ -52,7 +53,7 @@ export class PrefetchTransform extends Transform {
if (type === 'prefetch') {
dep = path.join('/', dep);
}
let link = dom5.constructors.element('link');
const link = dom5.constructors.element('link');
dom5.setAttribute(link, 'rel', type);
dom5.setAttribute(link, 'href', dep);
dom5.append(head, link);
Expand All @@ -65,7 +66,7 @@ export class PrefetchTransform extends Transform {
if (this.isImportantFile(file)) {
// hold on to the file for safe keeping
this.fileMap.set(file.path, file);
callback(null, null);
callback(null);
} else {
callback(null, file);
}
Expand All @@ -81,27 +82,31 @@ export class PrefetchTransform extends Transform {
return done();
}
this.analyzer.analyzeDependencies.then((depsIndex: DepsIndex) => {
let fragmentToDeps = new Map(depsIndex.fragmentToDeps);
const fragmentToDeps = new Map(depsIndex.fragmentToDeps);

if (this.config.entrypoint && this.config.shell) {
let file = this.fileMap.get(this.config.entrypoint);
const file = this.fileMap.get(this.config.entrypoint);
if (file == null) throw new TypeError('file is null');

// forward shell's dependencies to main to be prefetched
let deps = fragmentToDeps.get(this.config.shell);
const deps = fragmentToDeps.get(this.config.shell);
if (deps) {
this.pullUpDeps(file, deps, 'prefetch');
}
this.push(file);
this.fileMap.delete(this.config.entrypoint);
}

for (let im of this.config.allFragments) {
let file = this.fileMap.get(im);
let deps = fragmentToDeps.get(im);
for (const importUrl of this.config.allFragments) {
const file = this.fileMap.get(importUrl);
if (file == null) throw new TypeError('file is null');

const deps = fragmentToDeps.get(importUrl);
if (deps) {
this.pullUpDeps(file, deps, 'import');
}
this.push(file);
this.fileMap.delete(im);
this.fileMap.delete(importUrl);
}

for (let leftover of this.fileMap.keys()) {
Expand Down
6 changes: 3 additions & 3 deletions src/build/sw-precache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import {SWConfig} from 'polymer-build';

let logger = logging.getLogger('cli.build.sw-precache');

export function parsePreCacheConfig(configFile: string): Promise<SWConfig> {
return new Promise<SWConfig>((resolve, _reject) => {
export function parsePreCacheConfig(configFile: string): Promise<SWConfig|null> {
return new Promise<SWConfig|null>((resolve, _reject) => {
fs.stat(configFile, (statError) => {
let config: SWConfig;
let config: SWConfig|null = null;
// only log if the config file exists at all
if (!statError) {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class BuildCommand implements Command {
js: {},
};
if (options['html.collapseWhitespace']) {
buildOptions.html.collapseWhitespace = true;
buildOptions.html!.collapseWhitespace = true;
}
logger.debug('building with options', buildOptions);

Expand Down
2 changes: 1 addition & 1 deletion src/commands/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,6 @@ export class LintCommand implements Command {
'config-field': options['config-field'],
// NOTE: `no-recursion` has the opposite behavior of `follow-dependencies`
'no-recursion': !followDependencies,
}).then(() => null as void);
}).then(() => undefined);
}
}
2 changes: 1 addition & 1 deletion src/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class ServeCommand implements Command {
// Defer dependency loading until this specific command is run
const polyserve = require('polyserve');

let openPath: string;
let openPath: string|undefined;
if (config.entrypoint && config.shell) {
openPath = config.entrypoint.substring(config.root.length);
if (openPath === 'index.html' || openPath === '/index.html') {
Expand Down
5 changes: 3 additions & 2 deletions src/environments/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const environments = new EnvironmentMap();
/**
* Builds an environment with the given name.
*/
export function buildEnvironment(name: string): Environment {
return environments.has(name) && new (environments.get(name.toLowerCase()))();
export function buildEnvironment(name: string): Environment|undefined {
const E = environments.get(name.toLowerCase());
if (E) return new E();
}
10 changes: 5 additions & 5 deletions src/github/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export type RequestAPI = request.RequestAPI<request.Request, request.CoreOptions

class GithubResponseError extends Error {
name = 'GithubResponseError';
statusCode: number;
statusMessage: string;
statusCode?: number;
statusMessage?: string;

constructor(statusCode: number, statusMessage: string) {
constructor(statusCode?: number, statusMessage?: string) {
super('unexpected response: ' + statusCode + ' ' + statusMessage);
this.statusCode = statusCode;
this.statusMessage = statusMessage;
Expand All @@ -44,13 +44,13 @@ export interface GithubOpts {
}

export class Github {
private _token: string;
private _token: string|null;
private _github: GitHubApi;
private _request: request.RequestAPI<request.Request, request.CoreOptions, request.RequiredUriUrl>;
private _owner: string;
private _repo: string;

static tokenFromFile(filename: string): string {
static tokenFromFile(filename: string): string|null {
try {
return fs.readFileSync(filename, 'utf8').trim();
} catch (error) {
Expand Down
6 changes: 4 additions & 2 deletions src/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function checkIsMinGW(): boolean {
*/
function getGeneratorDescription(generator: YeomanEnvironment.GeneratorMeta, generatorName: string): GeneratorDescription {
const name = getDisplayName(generatorName);
let description: string;
let description: string = '';

if (templateDescriptions.hasOwnProperty(name)) {
description = templateDescriptions[name];
Expand All @@ -95,7 +95,9 @@ function getGeneratorDescription(generator: YeomanEnvironment.GeneratorMeta, gen
}
}
// If a description exists, format it properly for the command-line
description = (description) ? chalk.dim(' - ' + description) : '';
if (description.length > 0) {
description = chalk.dim(` - ${description}`);
}

return {
name: `${name}${description}`,
Expand Down
22 changes: 12 additions & 10 deletions src/polymer-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class PolymerCli {
globals: ArgDescriptor[]
): ArgDescriptor[] {
const mergedArgs = new Map<string, ArgDescriptor>();
let defaultOption: string = null;
let defaultOption: string|null = null;

const addAll = (args: ArgDescriptor[]) => {
for (let definition of args) {
Expand Down Expand Up @@ -154,7 +154,7 @@ export class PolymerCli {
}

run(): Promise<any> {
const helpCommand = this.commands.get('help');
const helpCommand = this.commands.get('help')!;
const commandNames = Array.from(this.commands.keys());
let parsedArgs: ParsedCommand;
logger.debug('running...');
Expand Down Expand Up @@ -182,20 +182,22 @@ export class PolymerCli {
throw error;
}

let commandName = parsedArgs.command;
let commandArgs = parsedArgs.argv;
let command = this.commands.get(commandName);
const commandName = parsedArgs.command;
const commandArgs = parsedArgs.argv;
const command = this.commands.get(commandName)!;
if (command == null) throw new TypeError('command is null');

logger.debug(`command '${commandName}' found, parsing command args:`, {args: commandArgs});

let commandDefinitions = this.mergeDefinitions(command, globalArguments);
let commandOptionsRaw = commandLineArgs(commandDefinitions, commandArgs);
let commandOptions = parseCLIArgs(commandOptionsRaw);
const commandDefinitions = this.mergeDefinitions(command, globalArguments);
const commandOptionsRaw = commandLineArgs(commandDefinitions, commandArgs);
const commandOptions = parseCLIArgs(commandOptionsRaw);
logger.debug(`command options parsed from args:`, commandOptions);

let mergedConfigOptions = Object.assign(
const mergedConfigOptions = Object.assign(
{}, this.defaultConfigOptions, commandOptions);

let config = new ProjectConfig(mergedConfigOptions);
const config = new ProjectConfig(mergedConfigOptions);
logger.debug(`final project configuration generated:`, config);

// Help is a special argument for displaying help for the given command.
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"moduleResolution": "node",
"isolatedModules": false,
"declaration": false,
"strictNullChecks": true,
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
Expand Down