Conversation
1183703
to
4fc6bd6
Compare
I don't see any failures in the output but my tests are failing. Can you help? |
@IllusionMH thanks for looking into it. |
|
@jkillian I think this is a valid use case |
typescriptOnly: false, | ||
}; | ||
|
||
public static ADD_SPACE_AFTER_IMPORT = "Add space after import"; |
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.
Please feel free to correct my error wordings here.
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.
How about some quotes: Add space after 'import'
, similarly, below
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.
Looks pretty good. Some minor nits. Good tests
@@ -0,0 +1,124 @@ | |||
/** | |||
* @license | |||
* Copyright 2013 Palantir Technologies, Inc. |
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.
2016 (or 2017 if applicable)
typescriptOnly: false, | ||
}; | ||
|
||
public static ADD_SPACE_AFTER_IMPORT = "Add space after import"; |
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.
How about some quotes: Add space after 'import'
, similarly, below
public static ADD_SPACE_AFTER_STAR = "Add space after *"; | ||
public static TOO_MANY_SPACES_AFTER_STAR = "Too many spaces after *"; | ||
public static ADD_SPACE_AFTER_FROM = "Add space after from"; | ||
public static TOO_MANY_SPACES_AFTER_FROM = "Too many spaces after from"; |
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.
Too many space after 'from'
, above and below
public static ADD_SPACE_AFTER_IMPORT = "Add space after import"; | ||
public static TOO_MANY_SPACES_AFTER_IMPORT = "Too many spaces after import"; | ||
public static ADD_SPACE_AFTER_STAR = "Add space after *"; | ||
public static TOO_MANY_SPACES_AFTER_STAR = "Too many spaces after *"; |
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.
Too many spaces after '*'
, and above
} | ||
|
||
class ImportStatementWalker extends Lint.RuleWalker { | ||
private static IMPORT_KEYWORD_LENGTH = 6; // "import".length; |
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.
you can just define it as "import".length
and omit the comment. It is probably even more clear to not define this variable and just just "import".length
everywhere
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.
Isn't it more performance friendly to do this?
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.
The perf difference is not measurable. See the jsperf run https://jsperf.com/str-length
if ((nodeStart + ImportStatementWalker.IMPORT_KEYWORD_LENGTH + 1) < moduleSpecifierStart) { | ||
this.addFailure(this.createFailure(nodeStart, moduleSpecifierStart - nodeStart, Rule.TOO_MANY_SPACES_AFTER_IMPORT)); | ||
} | ||
if ((nodeStart + ImportStatementWalker.IMPORT_KEYWORD_LENGTH) === moduleSpecifierStart) { |
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.
else if
if (/from$/.test(fromString)) { | ||
this.addFailure(this.createFailure(importClauseEnd, fromString.length, Rule.ADD_SPACE_AFTER_FROM)); | ||
} | ||
if (/from\s{2,}$/.test(fromString)) { |
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.
else if
if (/^\s{2,}from/.test(fromString)) { | ||
this.addFailure(this.createFailure(importClauseEnd, fromString.length, Rule.TOO_MANY_SPACES_BEFORE_FROM)); | ||
} | ||
if (/^from/.test(fromString)) { |
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.
else if
@mohsen1 thanks! |
The banning of line breaks in import statements is problematic here. We need to prescribe a way to make this rule play nicely with max-line-length out of the box. Right now we don't have an option for the line length rule to skip import statements (#1872 (comment)). IMO I would prefer to keep the max-line-length rule in effect for import statements and allow multiline import statements in the default TSLint config. import {
RuleWalker,
RuleFailure,
...
} from "tslint"; |
It does allow line breaks in destructuring import clause. although it won't let a very long module specifier like this: import * as veryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName from './very-very-very-very-very-very-very-very-very-long-name'; But a line break might not solve the issue if the specifier itself is longer than max lenght. |
Ah, cool, sorry I missed that. This implementation looks reasonable then. |
This PR adds
import-spacing
rule which enforces proper spacing in import declarations.This is implementation of #1584
PR checklist