@@ -67,7 +67,8 @@ module.exports = {
6767 TaggedTemplateExpression : ( node ) => {
6868 if ( node . tag . name === 'hbs' ) {
6969 if ( node . quasi . type === 'TemplateLiteral' ) {
70- const hbs = node . quasi . quasis [ 0 ] . value . cooked
70+ let hbs = node . quasi . quasis [ 0 ] . value . cooked
71+ hbs = unindentAndStripSafeNewlines ( hbs )
7172 const results = linter . verify ( { source : hbs . toString ( ) , moduleId : context . id } )
7273 if ( results . length !== 0 ) {
7374 const firstLine = results [ 0 ] . message . split ( '\n' ) [ 0 ]
@@ -80,3 +81,63 @@ module.exports = {
8081 }
8182 }
8283}
84+
85+ /*
86+ This turns:
87+
88+ ```js
89+ `
90+ {{#foo-bar}}
91+ template block text
92+ {{/foo-bar}}
93+ `
94+ ```js
95+
96+ into:
97+
98+ ```js
99+ `{{#foo-bar}}
100+ template block text
101+ {{/foo-bar}}`
102+ ```js
103+
104+ so your indentation, whitespace, and newline rules don't fail on every template.
105+ */
106+ function unindentAndStripSafeNewlines ( template ) {
107+ let lines = template . split ( '\n' )
108+
109+ // you are allowed one initial blank line, we remove it for you
110+ if ( lines [ 0 ] === '' ) {
111+ lines = lines . slice ( 1 )
112+ }
113+
114+ let countSame = - 1
115+ let testChar
116+ do {
117+ countSame ++
118+ testChar = null
119+ for ( let line of lines ) {
120+ if ( ! testChar ) {
121+ testChar = line [ countSame ]
122+ }
123+ let curChar = line [ countSame ]
124+ let isBlankLine = ! curChar
125+ let isWhitespace = curChar === ' ' || curChar === '\t'
126+ if ( ! isBlankLine && ( ! isWhitespace || curChar !== testChar ) ) {
127+ testChar = null
128+ break
129+ }
130+ }
131+ } while ( testChar )
132+
133+ lines = lines . map ( line => {
134+ return line . substr ( countSame )
135+ } )
136+
137+ // after we are done un-indenting, we will remove one final blank line for you
138+ if ( lines [ lines . length - 1 ] === '' ) {
139+ lines = lines . slice ( 0 , lines . length - 1 )
140+ }
141+
142+ return lines . join ( '\n' )
143+ }
0 commit comments