-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a fixer for classes-constants lint rule (#2969)
* Create a fixer for classes-constants lint rule 1. The lint rule will now fail the entire node, instead of just the pt-* string 2. The lint rule now has a fixer that will replace the pt-* string with the blueprint import * PR comments * Add DOM library to make compile work without skipLibCheck * Undo regression to tslint error reporting
- Loading branch information
1 parent
839c14b
commit 35327b7
Showing
7 changed files
with
199 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2018 Palantir Technologies, Inc. All rights reserved. | ||
* | ||
* Licensed under the terms of the LICENSE file distributed with this project. | ||
*/ | ||
|
||
import { Replacement } from "tslint"; | ||
import * as utils from "tsutils"; | ||
import * as ts from "typescript"; | ||
|
||
export function addImportToFile(file: ts.SourceFile, imports: string[], packageName: string) { | ||
const packageToModify = file.statements.find( | ||
statement => utils.isImportDeclaration(statement) && statement.moduleSpecifier.getText() === `"${packageName}"`, | ||
) as ts.ImportDeclaration; | ||
if ( | ||
packageToModify && | ||
packageToModify.importClause && | ||
packageToModify.importClause.namedBindings && | ||
utils.isNamedImports(packageToModify.importClause.namedBindings) | ||
) { | ||
const existingImports = packageToModify.importClause.namedBindings.elements.map(el => el.name.getText()); | ||
// Poor man's lodash.uniq without the dep. | ||
const newImports = Array.from(new Set(existingImports.concat(imports))).sort((a, b) => | ||
a.toLowerCase().localeCompare(b.toLowerCase()), | ||
); | ||
const importString = `{ ${newImports.join(", ")} }`; | ||
return Replacement.replaceNode(packageToModify.importClause.namedBindings, importString); | ||
} else { | ||
// we always place the import in alphabetical order. If imports are already alpha-ordered, this will act nicely | ||
// with existing lint rules. If imports are not alpha-ordered, this may appear weird. | ||
const allImports = file.statements.filter(utils.isImportDeclaration); | ||
const newImportIndex = allImports.findIndex(imp => { | ||
// slice the quotes off each module specifier | ||
return compare(imp.moduleSpecifier.getText().slice(1, -1), packageName) === 1; | ||
}); | ||
const startIndex = newImportIndex === -1 ? 0 : allImports[newImportIndex].getStart(); | ||
return Replacement.appendText(startIndex, `import { ${imports.join(", ")} } from "${packageName}";\n`); | ||
} | ||
} | ||
|
||
function isLow(value: string) { | ||
return value[0] === "." || value[0] === "/"; | ||
} | ||
|
||
// taken from tslint orderedImportRules | ||
function compare(a: string, b: string): 0 | 1 | -1 { | ||
if (isLow(a) && !isLow(b)) { | ||
return 1; | ||
} else if (!isLow(a) && isLow(b)) { | ||
return -1; | ||
} else if (a > b) { | ||
return 1; | ||
} else if (a < b) { | ||
return -1; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/tslint-config/test/rules/blueprint-classes-constants/true/test-icons.tsx.fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Make sure that this rule doesn't modify icon classes, that's handled elsewhere. | ||
<span className"pt-icon pt-icon-folder-open" /> |
2 changes: 2 additions & 0 deletions
2
packages/tslint-config/test/rules/blueprint-classes-constants/true/test-icons.tsx.lint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Make sure that this rule doesn't modify icon classes, that's handled elsewhere. | ||
<span className"pt-icon pt-icon-folder-open" /> |
13 changes: 13 additions & 0 deletions
13
packages/tslint-config/test/rules/blueprint-classes-constants/true/test.tsx.fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Classes } from "@blueprintjs/core"; | ||
apt-get | ||
at 4pt-size | ||
`${Classes.LARGE} script-source apt-get` | ||
`script-source ${Classes.LARGE} apt-get` | ||
`${template} ${Classes.LARGE}` | ||
|
||
<Button className=`${Classes.LARGE} my-class` /> | ||
|
||
classNames(Classes.BUTTON, Classes.INTENT_PRIMARY) | ||
|
||
<pt-popover> // angular directive | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35327b7
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.
Create a fixer for classes-constants lint rule (#2969)
Previews: documentation | landing | table