Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make "prepend" behaviour add the template at byte zero instead of before the node #7

Merged
merged 1 commit into from
Mar 21, 2018

Conversation

IvanSanchez
Copy link
Contributor

This is related to #3.

My use case is wanting to have no blank lines at the beginning of the file before the notice template.

In order to achieve this, I create a RegExp starting with ^, like:

var copyrightTemplate = fs.readFileSync('bsd-license.txt').toString();
var copyrightMatch = new RegExp( "^" + escapeStringRegexp(copyrightTemplate) );

// ...
	"rules":{
		"notice/notice":["error",{
			"mustMatch": copyrightMatch,
			"template": copyrightTemplate,
		}]
	}

If my files start with a few blank lines and then a block comment, then the fix will prepend the template to the first node in the AST. Unfortunately, the first node does not start at byte 0, so it leaves the blank lines at the beginning intact.

The fix uses insertTextBeforeRange([0, 0] to add the template at the very beginning of the file, instead of before the first node.

@nickdeis
Copy link
Owner

Thank you @IvanSanchez for your pull request. This fix does make more sense!

@nickdeis nickdeis merged commit a3db51d into nickdeis:master Mar 21, 2018
nickdeis added a commit that referenced this pull request Mar 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants