@@ -8,6 +8,7 @@ import * as ts from 'typescript/lib/tsserverlibrary';
88import { loadConfiguration } from './configuration' ;
99import { LanguageServiceLogger } from './logger' ;
1010import StyledTemplateLanguageService from './styled-template-language-service' ;
11+ import { getSubstitution } from './substituter' ;
1112
1213export = ( mod : { typescript : typeof ts } ) => {
1314 return {
@@ -25,57 +26,9 @@ export = (mod: { typescript: typeof ts }) => {
2526 start : number ,
2627 end : number
2728 ) : string {
28- const placeholder = templateString . slice ( start , end ) ;
29-
30- // check to see if it's an in-property interplation, or a mixin,
31- // and determine which character to use in either case
32- // if in-property, replace with "xxxxxx"
33- // if a mixin, replace with " "
34- const pre = templateString . slice ( 0 , start ) ;
35- const post = templateString . slice ( end ) ;
36- const replacementChar = getReplacementCharacter ( pre , post , logger ) ;
37- const result = placeholder . replace ( / ./ gm, c => c === '\n' ? '\n' : replacementChar ) ;
38-
39- // If followed by a semicolon, we may have to eat the semi colon using a false property
40- if ( replacementChar === ' ' && post . match ( / ^ \s * ; / ) ) {
41- // Handle case where we need to eat the semi colon:
42- //
43- // styled.x`
44- // ${'color: red'};
45- // `
46- //
47- // vs. the other case where we do not:
48- //
49- // styled.x`
50- // color: ${'red'};
51- // `
52- if ( pre . match ( / ( ; | ^ | \} ) [ \s | \n ] * $ / ) ) {
53- // Mixin, replace with a dummy variable declaration, so scss server doesn't complain about rogue semicolon
54- return '$a:0' + result . slice ( 4 ) ; // replace(/./gm, c => c === '\n' ? '\n' : ' ');
55- }
56- return placeholder . replace ( / ./ gm, c => c === '\n' ? '\n' : 'x' ) ;
57- }
58-
59- return result ;
29+ return getSubstitution ( templateString , start , end , logger ) ;
6030 } ,
6131 } , { logger } ) ;
6232 } ,
6333 } ;
6434} ;
65-
66- function getReplacementCharacter ( pre : string , post : string , logger : LanguageServiceLogger ) {
67- if ( pre . match ( / ( ^ | \n ) \s * $ / g) ) {
68- if ( ! post . match ( / ^ \s * \{ / ) ) { // ${'button'} {
69- return ' ' ;
70- }
71- }
72-
73- // If the placeholder looks like a unit that would not work when replaced with an identifier,
74- // try replaceing with a number.
75- if ( post . match ( / ^ ( % ) / ) ) {
76- return '0' ;
77- }
78-
79- // Otherwise replace with an identifier
80- return 'x' ;
81- }
0 commit comments