typescript-strictly-typed
Advanced tools
Comparing version 2.9.0 to 2.10.0
@@ -25,8 +25,5 @@ "use strict"; | ||
} | ||
if (!config.angularCompilerOptions) { | ||
config.angularCompilerOptions = {}; | ||
} | ||
config.angularCompilerOptions.strictInjectionParameters = true; | ||
config.angularCompilerOptions.strictTemplates = true; | ||
config.angularCompilerOptions.strictInputAccessModifiers = true; | ||
config.raw = config_utils_1.modifyJSON(config.raw, ['angularCompilerOptions', 'strictInjectionParameters'], true); | ||
config.raw = config_utils_1.modifyJSON(config.raw, ['angularCompilerOptions', 'strictTemplates'], true); | ||
config.raw = config_utils_1.modifyJSON(config.raw, ['angularCompilerOptions', 'strictInputAccessModifiers'], true); | ||
return config_utils_1.saveConfig(cwd, file, config); | ||
@@ -44,7 +41,4 @@ } | ||
const config = config_utils_1.getConfig(cwd, file); | ||
if (config && isCodelyzer(config.rulesDirectory)) { | ||
if (!config.rules) { | ||
config.rules = {}; | ||
} | ||
config.rules['template-no-any'] = true; | ||
if (config && isCodelyzer(config.json.rulesDirectory)) { | ||
config.raw = config_utils_1.modifyJSON(config.raw, ['rules', 'template-no-any'], true); | ||
config_utils_1.saveConfig(cwd, file, config); | ||
@@ -51,0 +45,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.checkDependencyVersion = exports.saveConfig = exports.getConfig = exports.findConfig = void 0; | ||
exports.checkDependencyVersion = exports.modifyJSON = exports.saveConfig = exports.getConfig = exports.findConfig = void 0; | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
const json5 = require("json5"); | ||
const jsonc_parser_1 = require("jsonc-parser"); | ||
const yaml = require("js-yaml"); | ||
@@ -33,3 +33,3 @@ const pkg_up_1 = require("pkg-up"); | ||
/** | ||
* Get and parse the config of a tool | ||
* Get the config of a tool | ||
* | ||
@@ -39,21 +39,35 @@ * @param cwd Working directory path | ||
* | ||
* @returns The parsed config, or `null` | ||
* @returns The config in raw (string) and JSON formats, or `null` | ||
*/ | ||
function getConfig(cwd, file) { | ||
const filePath = path.join(cwd, file); | ||
const configRaw = fs.readFileSync(filePath, { encoding: 'utf8' }); | ||
const raw = fs.readFileSync(filePath, { encoding: 'utf8' }); | ||
const fileType = path.extname(file); | ||
let configParsed = null; | ||
let config = null; | ||
try { | ||
switch (fileType) { | ||
case '.json': | ||
configParsed = json5.parse(configRaw); | ||
case '.json': { | ||
config = { | ||
raw, | ||
json: jsonc_parser_1.parse(raw), | ||
}; | ||
break; | ||
} | ||
case '.yaml': | ||
case '.yml': | ||
configParsed = yaml.load(configRaw); | ||
case '.yml': { | ||
const json = yaml.load(raw); | ||
config = { | ||
raw: JSON.stringify(json), | ||
json, | ||
}; | ||
break; | ||
case '.js': | ||
configParsed = require(filePath); // eslint-disable-line @typescript-eslint/no-var-requires | ||
} | ||
case '.js': { | ||
const json = require(filePath); // eslint-disable-line @typescript-eslint/no-var-requires | ||
config = { | ||
raw: JSON.stringify(json), | ||
json, | ||
}; | ||
break; | ||
} | ||
} | ||
@@ -64,3 +78,3 @@ } | ||
} | ||
return configParsed; | ||
return config; | ||
} | ||
@@ -84,7 +98,7 @@ exports.getConfig = getConfig; | ||
case '.json': | ||
configStringified = JSON.stringify(config, null, 2); | ||
configStringified = config.raw; | ||
break; | ||
case '.yaml': | ||
case '.yml': | ||
configStringified = yaml.dump(config, { indent: 2 }); | ||
configStringified = yaml.dump(config.json, { indent: 2 }); | ||
break; | ||
@@ -111,2 +125,13 @@ } | ||
exports.saveConfig = saveConfig; | ||
function modifyJSON(json, path, value, otherOptions) { | ||
const options = { | ||
formattingOptions: { | ||
insertSpaces: true, | ||
tabSize: 2, | ||
}, | ||
...otherOptions, | ||
}; | ||
return jsonc_parser_1.applyEdits(json, jsonc_parser_1.modify(json.toString(), path, value, options !== null && options !== void 0 ? options : {})); | ||
} | ||
exports.modifyJSON = modifyJSON; | ||
/** | ||
@@ -124,3 +149,3 @@ * Check a dependency version | ||
const packageJsonFile = fs.readFileSync(filePath, { encoding: 'utf8' }); | ||
const packageJsonConfig = json5.parse(packageJsonFile); | ||
const packageJsonConfig = jsonc_parser_1.parse(packageJsonFile); | ||
const prodDependencyVersion = (_a = packageJsonConfig === null || packageJsonConfig === void 0 ? void 0 : packageJsonConfig.dependencies) === null || _a === void 0 ? void 0 : _a[name]; | ||
@@ -127,0 +152,0 @@ const devDependencyVersion = (_b = packageJsonConfig === null || packageJsonConfig === void 0 ? void 0 : packageJsonConfig.devDependencies) === null || _b === void 0 ? void 0 : _b[name]; |
@@ -16,3 +16,3 @@ "use strict"; | ||
function enableESLintStrict(cwd) { | ||
var _a, _b, _c; | ||
var _a, _b, _c, _d, _e, _f, _g; | ||
const possibleConfigFiles = ['.eslintrc.json', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.js', 'package.json']; | ||
@@ -30,6 +30,9 @@ const tsFilesConfig = '*.ts'; | ||
packageJSONConfig = config_utils_1.getConfig(cwd, file); | ||
if (!packageJSONConfig) { | ||
if (!packageJSONConfig || !packageJSONConfig.json.eslintConfig) { | ||
return false; | ||
} | ||
config = (_a = packageJSONConfig.eslintConfig) !== null && _a !== void 0 ? _a : null; | ||
config = { | ||
raw: JSON.stringify(packageJSONConfig.json.eslintConfig), | ||
json: packageJSONConfig.json.eslintConfig, | ||
}; | ||
} | ||
@@ -42,9 +45,10 @@ else { | ||
} | ||
checkConfig(config); | ||
checkConfig(config.json); | ||
let tsConfigAdded = false; | ||
/* If there is an override, rules must be set inside it, or they won't be checked */ | ||
for (const override of (_b = config.overrides) !== null && _b !== void 0 ? _b : []) { | ||
for (const [index, override] of Object.entries((_a = config.json.overrides) !== null && _a !== void 0 ? _a : [])) { | ||
const indexNumber = Number.parseInt(index, 10); | ||
const files = normalizeConfigToArray(override.files); | ||
if (files.some((file) => file.includes(tsFilesConfig))) { | ||
addTSConfig(override); | ||
addTSConfig(config, ['overrides', indexNumber], (_c = (_b = config.json.overrides) === null || _b === void 0 ? void 0 : _b[indexNumber]) === null || _c === void 0 ? void 0 : _c.rules); | ||
tsConfigAdded = true; | ||
@@ -54,5 +58,5 @@ } | ||
const extendsConfig = normalizeConfigToArray(override.extends); | ||
if (((_c = override.plugins) === null || _c === void 0 ? void 0 : _c.includes(eslintAngularTemplatePlugin)) | ||
if (((_d = override.plugins) === null || _d === void 0 ? void 0 : _d.includes(eslintAngularTemplatePlugin)) | ||
|| extendsConfig.some((extendConfig) => extendConfig.includes(eslintAngularTemplatePlugin))) | ||
addAngularHTMLConfig(override); | ||
addAngularHTMLConfig(config, ['overrides', indexNumber], (_f = (_e = config.json.overrides) === null || _e === void 0 ? void 0 : _e[indexNumber]) === null || _f === void 0 ? void 0 : _f.rules); | ||
} | ||
@@ -62,6 +66,6 @@ } | ||
if (!tsConfigAdded) { | ||
addTSConfig(config); | ||
addTSConfig(config, [], (_g = config.json) === null || _g === void 0 ? void 0 : _g.rules); | ||
} | ||
if (packageJSONConfig) { | ||
packageJSONConfig.eslintConfig = config; | ||
config.raw = config_utils_1.modifyJSON(packageJSONConfig.raw, ['eslintConfig'], config.json); | ||
return config_utils_1.saveConfig(cwd, file, packageJSONConfig); | ||
@@ -109,28 +113,22 @@ } | ||
} | ||
function addTSConfig(config) { | ||
if (!config.rules) { | ||
config.rules = {}; | ||
function addTSConfig(config, path, rules) { | ||
if (Array.isArray(rules === null || rules === void 0 ? void 0 : rules['@typescript-eslint/no-explicit-any'])) { | ||
config.raw = config_utils_1.modifyJSON(config.raw, [...path, 'rules', '@typescript-eslint/no-explicit-any', 0], 'error'); | ||
} | ||
if (Array.isArray(config.rules['@typescript-eslint/no-explicit-any'])) { | ||
config.rules['@typescript-eslint/no-explicit-any'][0] = 'error'; | ||
} | ||
else { | ||
config.rules['@typescript-eslint/no-explicit-any'] = 'error'; | ||
config.raw = config_utils_1.modifyJSON(config.raw, [...path, 'rules', '@typescript-eslint/no-explicit-any'], 'error'); | ||
} | ||
if (Array.isArray(config.rules['@typescript-eslint/explicit-module-boundary-types'])) { | ||
config.rules['@typescript-eslint/explicit-module-boundary-types'][0] = 'error'; | ||
if (Array.isArray(rules === null || rules === void 0 ? void 0 : rules['@typescript-eslint/explicit-module-boundary-types'])) { | ||
config.raw = config_utils_1.modifyJSON(config.raw, [...path, 'rules', '@typescript-eslint/explicit-module-boundary-types', 0], 'error'); | ||
} | ||
else { | ||
config.rules['@typescript-eslint/explicit-module-boundary-types'] = 'error'; | ||
config.raw = config_utils_1.modifyJSON(config.raw, [...path, 'rules', '@typescript-eslint/explicit-module-boundary-types'], 'error'); | ||
} | ||
} | ||
function addAngularHTMLConfig(config) { | ||
if (!config.rules) { | ||
config.rules = {}; | ||
function addAngularHTMLConfig(config, path, rules) { | ||
if (Array.isArray(rules === null || rules === void 0 ? void 0 : rules['@angular-eslint/template/no-any'])) { | ||
config.raw = config_utils_1.modifyJSON(config.raw, [...path, 'rules', '@angular-eslint/template/no-any', 0], 'error'); | ||
} | ||
if (Array.isArray(config.rules['@angular-eslint/template/no-any'])) { | ||
config.rules['@angular-eslint/template/no-any'][0] = 'error'; | ||
} | ||
else { | ||
config.rules['@angular-eslint/template/no-any'] = 'error'; | ||
config.raw = config_utils_1.modifyJSON(config.raw, [...path, 'rules', '@angular-eslint/template/no-any'], 'error'); | ||
} | ||
@@ -137,0 +135,0 @@ } |
{ | ||
"name": "typescript-strictly-typed", | ||
"version": "2.9.0", | ||
"version": "2.10.0", | ||
"description": "Enable configurations for strictly typed TypeScript, ESLint or TSLint, and optionally Angular.", | ||
@@ -50,5 +50,5 @@ "funding": { | ||
"dependencies": { | ||
"chalk": "4.1.1", | ||
"chalk": "4.1.2", | ||
"js-yaml": "4.1.0", | ||
"json5": "2.2.0", | ||
"jsonc-parser": "3.0.0", | ||
"pkg-up": "3.1.0", | ||
@@ -58,12 +58,12 @@ "semver": "7.3.5" | ||
"devDependencies": { | ||
"@types/js-yaml": "4.0.1", | ||
"@types/js-yaml": "4.0.3", | ||
"@types/node": "12.12.70", | ||
"@types/semver": "7.3.6", | ||
"@typescript-eslint/eslint-plugin": "4.26.0", | ||
"@typescript-eslint/parser": "4.26.0", | ||
"@types/semver": "7.3.8", | ||
"@typescript-eslint/eslint-plugin": "4.29.2", | ||
"@typescript-eslint/parser": "4.29.2", | ||
"copyfiles": "2.4.1", | ||
"eslint": "7.27.0", | ||
"eslint": "7.32.0", | ||
"rimraf": "3.0.2", | ||
"typescript": "4.3.2" | ||
"typescript": "4.3.5" | ||
} | ||
} |
@@ -17,2 +17,6 @@ # TypeScript Strictly Typed | ||
On the other hand, it's recommended to redo the command when doing major updates of your tools | ||
(TypeScript, ESLint or Angular) to add newly introduced strict options, | ||
to stay up to date on best practices. | ||
## Getting started | ||
@@ -19,0 +23,0 @@ |
@@ -16,2 +16,3 @@ "use strict"; | ||
function enableTSLintStrict(cwd) { | ||
var _a; | ||
const file = config_utils_1.findConfig(cwd, ['tslint.json', 'tslint.yaml', 'tslint.yml']); | ||
@@ -25,14 +26,12 @@ if (!file) { | ||
} | ||
if (!config.rules) { | ||
config.rules = {}; | ||
} | ||
config.rules['no-any'] = true; | ||
config.raw = config_utils_1.modifyJSON(config.raw, ['rules', 'no-any'], true); | ||
/* `typedef` has multiple options, existing ones must not be deleted */ | ||
if (config.rules.typedef && Array.isArray(config.rules.typedef)) { | ||
if (!config.rules.typedef.includes('call-signature')) { | ||
config.rules.typedef.push('call-signature'); | ||
const rules = (_a = config.json.rules) !== null && _a !== void 0 ? _a : {}; | ||
if (rules.typedef && Array.isArray(rules.typedef)) { | ||
if (!rules.typedef.includes('call-signature')) { | ||
config.raw = config_utils_1.modifyJSON(config.raw, ['rules', 'typedef'], 'call-signature', { isArrayInsertion: true }); | ||
} | ||
} | ||
else { | ||
config.rules.typedef = [true, 'call-signature']; | ||
config.raw = config_utils_1.modifyJSON(config.raw, ['rules', 'typedef'], [true, 'call-signature']); | ||
} | ||
@@ -39,0 +38,0 @@ return config_utils_1.saveConfig(cwd, file, config); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const config_utils_1 = require("./config-utils"); | ||
const config_utils_2 = require("./config-utils"); | ||
/** | ||
@@ -26,9 +27,6 @@ * Enable the following TypeScript compiler options: | ||
} | ||
if (!config.compilerOptions) { | ||
config.compilerOptions = {}; | ||
} | ||
config.compilerOptions.strict = true; | ||
config.compilerOptions.noFallthroughCasesInSwitch = true; | ||
config.compilerOptions.noImplicitReturns = true; | ||
config.compilerOptions.forceConsistentCasingInFileNames = true; | ||
config.raw = config_utils_2.modifyJSON(config.raw, ['compilerOptions', 'strict'], true); | ||
config.raw = config_utils_2.modifyJSON(config.raw, ['compilerOptions', 'noFallthroughCasesInSwitch'], true); | ||
config.raw = config_utils_2.modifyJSON(config.raw, ['compilerOptions', 'noImplicitReturns'], true); | ||
config.raw = config_utils_2.modifyJSON(config.raw, ['compilerOptions', 'forceConsistentCasingInFileNames'], true); | ||
/* | ||
@@ -40,31 +38,17 @@ if (checkDependencyVersion(cwd, 'typescript', '>=4.1.0')) { | ||
if (config_utils_1.checkDependencyVersion(cwd, 'typescript', '>=4.2.0')) { | ||
config.compilerOptions.noPropertyAccessFromIndexSignature = true; | ||
config.raw = config_utils_2.modifyJSON(config.raw, ['compilerOptions', 'noPropertyAccessFromIndexSignature'], true); | ||
} | ||
if (config_utils_1.checkDependencyVersion(cwd, 'typescript', '>=4.3.0')) { | ||
config.compilerOptions.noImplicitOverride = true; | ||
config.raw = config_utils_2.modifyJSON(config.raw, ['compilerOptions', 'noImplicitOverride'], true); | ||
} | ||
/* Clean up options included in strict mode */ | ||
if (config.compilerOptions.alwaysStrict) { | ||
delete config.compilerOptions.alwaysStrict; | ||
} | ||
if (config.compilerOptions.noImplicitAny) { | ||
delete config.compilerOptions.noImplicitAny; | ||
} | ||
if (config.compilerOptions.noImplicitThis) { | ||
delete config.compilerOptions.noImplicitThis; | ||
} | ||
if (config.compilerOptions.strictBindCallApply) { | ||
delete config.compilerOptions.strictBindCallApply; | ||
} | ||
if (config.compilerOptions.strictFunctionTypes) { | ||
delete config.compilerOptions.strictFunctionTypes; | ||
} | ||
if (config.compilerOptions.strictNullChecks) { | ||
delete config.compilerOptions.strictNullChecks; | ||
} | ||
if (config.compilerOptions.strictPropertyInitialization) { | ||
delete config.compilerOptions.strictPropertyInitialization; | ||
} | ||
config.raw = config_utils_2.modifyJSON(config.raw, ['compilerOptions', 'alwaysStrict'], undefined); | ||
config.raw = config_utils_2.modifyJSON(config.raw, ['compilerOptions', 'noImplicitAny'], undefined); | ||
config.raw = config_utils_2.modifyJSON(config.raw, ['compilerOptions', 'noImplicitThis'], undefined); | ||
config.raw = config_utils_2.modifyJSON(config.raw, ['compilerOptions', 'strictBindCallApply'], undefined); | ||
config.raw = config_utils_2.modifyJSON(config.raw, ['compilerOptions', 'strictFunctionTypes'], undefined); | ||
config.raw = config_utils_2.modifyJSON(config.raw, ['compilerOptions', 'strictNullChecks'], undefined); | ||
config.raw = config_utils_2.modifyJSON(config.raw, ['compilerOptions', 'strictPropertyInitialization'], undefined); | ||
return config_utils_1.saveConfig(cwd, file, config); | ||
} | ||
exports.default = enableTypescriptStrict; |
29431
139
+ Addedjsonc-parser@3.0.0
+ Addedchalk@4.1.2(transitive)
+ Addedjsonc-parser@3.0.0(transitive)
- Removedjson5@2.2.0
- Removedchalk@4.1.1(transitive)
- Removedjson5@2.2.0(transitive)
- Removedminimist@1.2.8(transitive)
Updatedchalk@4.1.2