Skip to content
Permalink
Browse files

use on tslint-config-prettier pkg instead of custom rules

fixes #11
  • Loading branch information...
schuchard committed Mar 9, 2019
1 parent 8466968 commit fed2fe0fd8b10ac3f612208caa8d9479f47b3fe7
Showing with 16 additions and 37 deletions.
  1. +16 −4 src/prettier/index.ts
  2. +0 −33 src/utility/prettier-util.ts
@@ -17,7 +17,6 @@ import {
PrettierOptions,
getDefaultOptions,
PrettierSettings,
removeConflictingTsLintRules,
} from '../utility/prettier-util';
import { addPackageJsonDependency, NodeDependencyType } from '../utility/dependencies';
import {
@@ -26,6 +25,7 @@ import {
getFileAsJson,
addPropertyToPackageJson,
} from '../utility/util';
import { JsonObject } from '@angular-devkit/core';

export default function(options: PrettierOptions): Rule {
return (tree: Tree, context: SchematicContext) => {
@@ -44,12 +44,13 @@ export default function(options: PrettierOptions): Rule {
}

const prettierCommand = 'prettier --write';
const tslintConfigPackage = 'tslint-config-prettier';

function addDependencies(options: PrettierOptions): Rule {
return (tree: Tree): Observable<Tree> => {
const lintStagedDep = ['lint-staged', 'husky'];

return of('prettier', 'lint-staged', 'husky').pipe(
return of('prettier', 'lint-staged', 'husky', tslintConfigPackage).pipe(
filter((pkg) => {
if (options.lintStaged === false) {
// remove lint-staged deps
@@ -92,9 +93,20 @@ function modifyTsLint(): Rule {
return (tree: Tree, context: SchematicContext) => {
const tslintPath = 'tslint.json';
if (tree.exists(tslintPath)) {
const prettierSafeTsLint = removeConflictingTsLintRules(getFileAsJson(tree, tslintPath));
const tslint = getFileAsJson(tree, tslintPath) as JsonObject;

tree.overwrite(tslintPath, JSON.stringify(prettierSafeTsLint, null, 2));
if (!tslint) return tree;

if (Array.isArray(tslint.extends)) {
// should be added last https://github.com/prettier/tslint-config-prettier
tslint.extends.push(tslintConfigPackage);
} else if (typeof tslint.extends === 'string') {
tslint.extends = [tslint.extends, tslintConfigPackage];
} else {
tslint.extends = tslintConfigPackage;
}

tree.overwrite(tslintPath, JSON.stringify(tslint, null, 2));
} else {
context.logger.info(
`unable to locate tslint file at ${tslintPath}, conflicting styles may exists`
@@ -70,36 +70,3 @@ function wrapAsString(key: any, value: any): boolean {
);
return hasStringValue ? `"${value}"` : value;
}

export function removeConflictingTsLintRules(tsLint: any) {
if (!tsLint.rules) {
return tsLint;
}

tsLint.rules = Object.entries(tsLint.rules).reduce((acc: any, [tsKey, tsValue]) => {
if (ruleIsIncompatible(tsKey)) {
return acc;
}
acc[tsKey] = tsValue;
return acc;
}, {});

return tsLint;
}

function ruleIsIncompatible(rule: string) {
return [
'comment-format',
'curly',
'eofline',
'import-spacing',
'indent',
'max-line-length',
'no-trailing-whitespace',
'one-line',
'quotemark',
'semicolon',
'typedef-whitespace',
'angular-whitespace',
].some((removeable) => rule == removeable);
}

0 comments on commit fed2fe0

Please sign in to comment.
You can’t perform that action at this time.