Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Add import-spacing rule #1935

Merged
merged 16 commits into from Jan 1, 2017
Merged

Add import-spacing rule #1935

merged 16 commits into from Jan 1, 2017

Conversation

mohsen1
Copy link
Contributor

@mohsen1 mohsen1 commented Dec 25, 2016

This PR adds import-spacing rule which enforces proper spacing in import declarations.

This is implementation of #1584

PR checklist

  • New feature, bugfix, or enhancement
    • Includes tests
  • Documentation update

@mohsen1 mohsen1 changed the title WIP: Add import-spacing rule Add import-spacing rule Dec 28, 2016
@mohsen1
Copy link
Contributor Author

mohsen1 commented Dec 28, 2016

I don't see any failures in the output but my tests are failing. Can you help?

@IllusionMH
Copy link
Contributor

@mohsen1 I'm unable to create PR to your fork.

You can find changes that allowed me to fix test in commit f37c74d

@mohsen1
Copy link
Contributor Author

mohsen1 commented Dec 29, 2016

@IllusionMH thanks for looking into it.
What nil means in there?

@jkillian
Copy link
Contributor

~nil means that the error starts there but has zero width. In this case, it probably means that the error indicates that it starts after the last character of the line (where the line break character would be I suppose). In general it's nicer to avoid outputting errors like this if possible.

@IllusionMH
Copy link
Contributor

@mohsen1 ~nil is described in lines.ts
In this case error starts on next character after } (in second case) and this character is line break - which doesn't have length and can't be marked with regular ~.

@mohsen1
Copy link
Contributor Author

mohsen1 commented Dec 29, 2016

@jkillian I think this is a valid use case

typescriptOnly: false,
};

public static ADD_SPACE_AFTER_IMPORT = "Add space after import";
Copy link
Contributor Author

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.

Copy link
Contributor

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

Copy link
Contributor

@nchen63 nchen63 left a 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.
Copy link
Contributor

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";
Copy link
Contributor

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";
Copy link
Contributor

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 *";
Copy link
Contributor

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;
Copy link
Contributor

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

Copy link
Contributor Author

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?

Copy link
Contributor

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) {
Copy link
Contributor

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)) {
Copy link
Contributor

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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else if

@nchen63 nchen63 merged commit 7d9f7e7 into palantir:master Jan 1, 2017
@nchen63
Copy link
Contributor

nchen63 commented Jan 1, 2017

@mohsen1 thanks!

@adidahiya
Copy link
Contributor

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";

@mohsen1
Copy link
Contributor Author

mohsen1 commented Jan 3, 2017

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.

@adidahiya
Copy link
Contributor

Ah, cool, sorry I missed that. This implementation looks reasonable then.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants