-
Notifications
You must be signed in to change notification settings - Fork 872
Update dependencies and lint for no-unsafe-any #2544
Changes from 2 commits
d9faf67
c422969
cb8b9da
8b28da7
cb1614d
dd04557
416951a
0dca004
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,8 @@ | |
| * limitations under the License. | ||
| */ | ||
|
|
||
| // tslint:disable no-unsafe-any (TODO) | ||
|
|
||
| import findup = require("findup-sync"); | ||
| import * as fs from "fs"; | ||
| import * as path from "path"; | ||
|
|
@@ -157,8 +159,8 @@ export function loadConfigurationFromPath(configFilePath?: string): IConfigurati | |
| let rawConfigFile: any; | ||
| if (path.extname(resolvedConfigFilePath) === ".json") { | ||
| const fileContent = stripComments(fs.readFileSync(resolvedConfigFilePath) | ||
| .toString() | ||
| .replace(/^\uFEFF/, "")); | ||
| .toString() | ||
| .replace(/^\uFEFF/, "")); | ||
| rawConfigFile = JSON.parse(fileContent); | ||
| } else { | ||
| rawConfigFile = require(resolvedConfigFilePath); | ||
|
|
@@ -252,7 +254,7 @@ export function extendConfigurationFile(targetConfig: IConfigurationFile, | |
| }; | ||
| } | ||
|
|
||
| function getHomeDir() { | ||
| function getHomeDir(): string | undefined { | ||
| const environment = global.process.env; | ||
| const paths = [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prefer type annotation on |
||
| environment.USERPROFILE, | ||
|
|
@@ -266,6 +268,8 @@ function getHomeDir() { | |
| return homePath; | ||
| } | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| // returns the absolute path (contrary to what the name implies) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,13 +17,13 @@ | |
|
|
||
| import * as fs from "fs"; | ||
| import * as path from "path"; | ||
| import {FormatterFunction} from "./index"; | ||
| import {FormatterConstructor} from "./index"; | ||
| import {camelize} from "./utils"; | ||
|
|
||
| const moduleDirectory = path.dirname(module.filename); | ||
| const CORE_FORMATTERS_DIRECTORY = path.resolve(moduleDirectory, ".", "formatters"); | ||
|
|
||
| export function findFormatter(name: string | FormatterFunction, formattersDirectory?: string) { | ||
| export function findFormatter(name: string | FormatterConstructor, formattersDirectory?: string): FormatterConstructor | undefined { | ||
| if (typeof name === "function") { | ||
| return name; | ||
| } else if (typeof name === "string") { | ||
|
|
@@ -52,24 +52,24 @@ export function findFormatter(name: string | FormatterFunction, formattersDirect | |
| } | ||
| } | ||
|
|
||
| function loadFormatter(...paths: string[]) { | ||
| function loadFormatter(...paths: string[]): FormatterConstructor | undefined { | ||
| const formatterPath = paths.reduce((p, c) => path.join(p, c), ""); | ||
| const fullPath = path.resolve(moduleDirectory, formatterPath); | ||
|
|
||
| if (fs.existsSync(`${fullPath}.js`)) { | ||
| const formatterModule = require(fullPath); | ||
| return formatterModule.Formatter; | ||
| return formatterModule.Formatter; // tslint:disable-line no-unsafe-any | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of disabling rule, cast the |
||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| function loadFormatterModule(name: string) { | ||
| function loadFormatterModule(name: string): FormatterConstructor | undefined { | ||
| let src: string; | ||
| try { | ||
| src = require.resolve(name); | ||
| } catch (e) { | ||
| return undefined; | ||
| } | ||
| return require(src).Formatter; | ||
| return require(src).Formatter; // tslint:disable-line no-unsafe-any | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above comment |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,13 +20,12 @@ import * as path from "path"; | |
|
|
||
| import { getRelativePath } from "./configuration"; | ||
| import { showWarningOnce } from "./error"; | ||
| import { AbstractRule } from "./language/rule/abstractRule"; | ||
| import { IDisabledInterval, IOptions, IRule } from "./language/rule/rule"; | ||
| import { IDisabledInterval, IOptions, IRule, RuleConstructor } from "./language/rule/rule"; | ||
| import { arrayify, camelize, dedent } from "./utils"; | ||
|
|
||
| const moduleDirectory = path.dirname(module.filename); | ||
| const CORE_RULES_DIRECTORY = path.resolve(moduleDirectory, ".", "rules"); | ||
| const cachedRules = new Map<string, typeof AbstractRule | null>(); // null indicates that the rule was not found | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually I found this to be clearer
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But rules aren't actually guaranteed to be implemented by
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that it's renamed to |
||
| const cachedRules = new Map<string, RuleConstructor | null>(); // null indicates that the rule was not found | ||
|
|
||
| export interface IEnableDisablePosition { | ||
| isEnabled: boolean; | ||
|
|
@@ -45,7 +44,7 @@ export function loadRules(ruleOptionsList: IOptions[], | |
| const ruleName = ruleOptions.ruleName; | ||
| const enableDisableRules = enableDisableRuleMap.get(ruleName); | ||
| if (ruleOptions.ruleSeverity !== "off" || enableDisableRuleMap) { | ||
| const Rule: (typeof AbstractRule) | null = findRule(ruleName, rulesDirectories); | ||
| const Rule = findRule(ruleName, rulesDirectories); | ||
| if (Rule == null) { | ||
| notFoundRules.push(ruleName); | ||
| } else { | ||
|
|
@@ -54,7 +53,7 @@ export function loadRules(ruleOptionsList: IOptions[], | |
| } else { | ||
| const ruleSpecificList = enableDisableRules || []; | ||
| ruleOptions.disabledIntervals = buildDisabledIntervalsFromSwitches(ruleSpecificList); | ||
| rules.push(new (Rule as any)(ruleOptions)); | ||
| rules.push(new (Rule as any)(ruleOptions) as IRule); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't need either cast |
||
|
|
||
| if (Rule.metadata && Rule.metadata.deprecationMessage) { | ||
| showWarningOnce(`${Rule.metadata.ruleName} is deprecated. ${Rule.metadata.deprecationMessage}`); | ||
|
|
@@ -89,9 +88,9 @@ export function loadRules(ruleOptionsList: IOptions[], | |
| return rules; | ||
| } | ||
|
|
||
| export function findRule(name: string, rulesDirectories?: string | string[]) { | ||
| export function findRule(name: string, rulesDirectories?: string | string[]): RuleConstructor | null { | ||
| const camelizedName = transformName(name); | ||
| let Rule: typeof AbstractRule | null; | ||
| let Rule: RuleConstructor | null; | ||
|
|
||
| // first check for core rules | ||
| Rule = loadCachedRule(CORE_RULES_DIRECTORY, camelizedName); | ||
|
|
@@ -109,7 +108,7 @@ export function findRule(name: string, rulesDirectories?: string | string[]) { | |
| return Rule; | ||
| } | ||
|
|
||
| function transformName(name: string) { | ||
| function transformName(name: string): string { | ||
| // camelize strips out leading and trailing underscores and dashes, so make sure they aren't passed to camelize | ||
| // the regex matches the groups (leading underscores and dashes)(other characters)(trailing underscores and dashes) | ||
| const nameMatch = name.match(/^([-_]*)(.*?)([-_]*)$/); | ||
|
|
@@ -123,18 +122,18 @@ function transformName(name: string) { | |
| * @param directory - An absolute path to a directory of rules | ||
| * @param ruleName - A name of a rule in filename format. ex) "someLintRule" | ||
| */ | ||
| function loadRule(directory: string, ruleName: string) { | ||
| function loadRule(directory: string, ruleName: string): RuleConstructor | null { | ||
| const fullPath = path.join(directory, ruleName); | ||
| if (fs.existsSync(fullPath + ".js")) { | ||
| const ruleModule = require(fullPath); | ||
| if (ruleModule && ruleModule.Rule) { | ||
| const ruleModule = require(fullPath) as { Rule: RuleConstructor } | undefined; | ||
| if (ruleModule !== undefined) { | ||
| return ruleModule.Rule; | ||
| } | ||
| } | ||
| return undefined; | ||
| return null; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the switch to nulls? can't we use undefined?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we should use a string literal like "not-found" or "load-error" instead of |
||
| } | ||
|
|
||
| function loadCachedRule(directory: string, ruleName: string, isCustomPath = false) { | ||
| function loadCachedRule(directory: string, ruleName: string, isCustomPath = false): RuleConstructor | null { | ||
| // use cached value if available | ||
| const fullPath = path.join(directory, ruleName); | ||
| const cachedRule = cachedRules.get(fullPath); | ||
|
|
@@ -153,9 +152,9 @@ function loadCachedRule(directory: string, ruleName: string, isCustomPath = fals | |
| } | ||
| } | ||
|
|
||
| let Rule: typeof AbstractRule | null = null; | ||
| if (absolutePath != null) { | ||
| Rule = loadRule(absolutePath, ruleName); | ||
| let Rule: RuleConstructor | null = null; | ||
| if (absolutePath !== undefined) { | ||
| Rule = loadRule(absolutePath, ruleName); // tslint:disable-line no-unsafe-any | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need to disable the rule here |
||
| } | ||
| cachedRules.set(fullPath, Rule); | ||
| return Rule; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this needed?