@@ -17,146 +17,145 @@ import { createService } from 'typescript-service';
1717//------------------------------------------------------------------------------
1818
1919type RawRuleConfig =
20- | null
21- | undefined
22- | boolean
23- | any [ ]
24- | {
25- severity ?: RuleSeverity | 'warn' | 'none' | 'default' ;
26- options ?: any ;
20+ | null
21+ | undefined
22+ | boolean
23+ | any [ ]
24+ | {
25+ severity ?: RuleSeverity | 'warn' | 'none' | 'default' ;
26+ options ?: any ;
2727 } ;
2828
2929interface RawRulesConfig {
30- [ key : string ] : RawRuleConfig ;
30+ [ key : string ] : RawRuleConfig ;
3131}
3232
3333interface TSLintPluginOptions {
34- rulesDirectory ?: string [ ] ;
35- rules ?: RawRulesConfig ;
34+ rulesDirectory ?: string [ ] ;
35+ rules ?: RawRulesConfig ;
3636}
3737
3838let languageService : ReturnType < typeof createService > ;
3939
4040export const rules = {
41- /**
42- * Expose a single rule called "config", which will be accessed in the user's eslint config files
43- * via "tslint/config"
44- */
45- config : {
46- meta : {
47- docs : {
48- description :
49- 'Wraps a TSLint configuration and lints the whole source using TSLint' ,
50- category : 'TSLint' ,
51- } ,
52- fixable : false ,
53- schema : [
54- {
55- type : 'object' ,
56- properties : {
57- rules : {
58- type : 'object' ,
59- /**
60- * No fixed schema properties for rules, as this would be a permanently moving target
61- */
62- additionalProperties : true ,
41+ /**
42+ * Expose a single rule called "config", which will be accessed in the user's eslint config files
43+ * via "tslint/config"
44+ */
45+ config : {
46+ meta : {
47+ docs : {
48+ description :
49+ 'Wraps a TSLint configuration and lints the whole source using TSLint' ,
50+ category : 'TSLint' ,
6351 } ,
64- rulesDirectory : {
65- type : 'array' ,
66- items : {
67- type : 'string' ,
68- } ,
69- } ,
70- configFile : {
71- type : 'string' ,
72- } ,
73- compilerOptions : {
74- type : 'object' ,
75- additionalProperties : true ,
76- } ,
77- } ,
78- additionalProperties : false ,
52+ schema : [
53+ {
54+ type : 'object' ,
55+ properties : {
56+ rules : {
57+ type : 'object' ,
58+ /**
59+ * No fixed schema properties for rules, as this would be a permanently moving target
60+ */
61+ additionalProperties : true ,
62+ } ,
63+ rulesDirectory : {
64+ type : 'array' ,
65+ items : {
66+ type : 'string' ,
67+ } ,
68+ } ,
69+ configFile : {
70+ type : 'string' ,
71+ } ,
72+ compilerOptions : {
73+ type : 'object' ,
74+ additionalProperties : true ,
75+ } ,
76+ } ,
77+ additionalProperties : false ,
78+ } ,
79+ ] ,
80+ } ,
81+ create : function ( context : Rule . RuleContext ) {
82+ const fileName = context . getFilename ( ) ;
83+ const sourceCode = context . getSourceCode ( ) . text ;
84+
85+ /**
86+ * The TSLint rules configuration passed in by the user
87+ */
88+ const {
89+ rules : tslintRules ,
90+ rulesDirectory : tslintRulesDirectory ,
91+ configFile,
92+ compilerOptions,
93+ } = context . options [ 0 ] ;
94+
95+ const tslintOptions = {
96+ formatter : 'json' ,
97+ fix : false ,
98+ rulesDirectory : tslintRulesDirectory ,
99+ } ;
100+
101+ /**
102+ * Manually construct a configFile for TSLint
103+ */
104+ const rawConfig : TSLintPluginOptions = { } ;
105+ rawConfig . rules = tslintRules || { } ;
106+ rawConfig . rulesDirectory = tslintRulesDirectory || [ ] ;
107+
108+ const tslintConfig = Configuration . parseConfigFile ( rawConfig ) ;
109+
110+ let program : ts . Program | undefined = undefined ;
111+
112+ if ( fileName !== '<input>' && configFile ) {
113+ if ( ! languageService ) {
114+ languageService = createService ( { configFile, compilerOptions } ) ;
115+ }
116+ program = languageService . getProgram ( ) ;
117+ }
118+
119+ /**
120+ * Create an instance of TSLint
121+ */
122+ const tslint = new TSLintLinter ( tslintOptions , program ) ;
123+
124+ /**
125+ * Lint the source code using the configured TSLint instance, and the rules which have been
126+ * passed via the ESLint rule options for this rule (using "tslint/config")
127+ */
128+ tslint . lint ( fileName , sourceCode , tslintConfig ) ;
129+
130+ const result = tslint . getResult ( ) ;
131+
132+ /**
133+ * Format the TSLint results for ESLint
134+ */
135+ if ( result . failures && result . failures . length ) {
136+ result . failures . forEach ( failure => {
137+ const start = failure . getStartPosition ( ) . getLineAndCharacter ( ) ;
138+ const end = failure . getEndPosition ( ) . getLineAndCharacter ( ) ;
139+ context . report ( {
140+ message : `${ failure . getFailure ( ) } (tslint:${ failure . getRuleName ( ) } )` ,
141+ loc : {
142+ start : {
143+ line : start . line + 1 ,
144+ column : start . character ,
145+ } ,
146+ end : {
147+ line : end . line + 1 ,
148+ column : end . character ,
149+ } ,
150+ } ,
151+ } ) ;
152+ } ) ;
153+ }
154+
155+ /**
156+ * Return an empty object for the ESLint rule
157+ */
158+ return { } ;
79159 } ,
80- ] ,
81- } ,
82- create : function ( context : Rule . RuleContext ) {
83- const fileName = context . getFilename ( ) ;
84- const sourceCode = context . getSourceCode ( ) . text ;
85-
86- /**
87- * The TSLint rules configuration passed in by the user
88- */
89- const {
90- rules : tslintRules ,
91- rulesDirectory : tslintRulesDirectory ,
92- configFile,
93- compilerOptions,
94- } = context . options [ 0 ] ;
95-
96- const tslintOptions = {
97- formatter : 'json' ,
98- fix : false ,
99- rulesDirectory : tslintRulesDirectory ,
100- } ;
101-
102- /**
103- * Manually construct a configFile for TSLint
104- */
105- const rawConfig : TSLintPluginOptions = { } ;
106- rawConfig . rules = tslintRules || { } ;
107- rawConfig . rulesDirectory = tslintRulesDirectory || [ ] ;
108-
109- const tslintConfig = Configuration . parseConfigFile ( rawConfig ) ;
110-
111- let program : ts . Program | undefined = undefined ;
112-
113- if ( fileName !== '<input>' && configFile ) {
114- if ( ! languageService ) {
115- languageService = createService ( { configFile, compilerOptions } ) ;
116- }
117- program = languageService . getProgram ( ) ;
118- }
119-
120- /**
121- * Create an instance of TSLint
122- */
123- const tslint = new TSLintLinter ( tslintOptions , program ) ;
124-
125- /**
126- * Lint the source code using the configured TSLint instance, and the rules which have been
127- * passed via the ESLint rule options for this rule (using "tslint/config")
128- */
129- tslint . lint ( fileName , sourceCode , tslintConfig ) ;
130-
131- const result = tslint . getResult ( ) ;
132-
133- /**
134- * Format the TSLint results for ESLint
135- */
136- if ( result . failures && result . failures . length ) {
137- result . failures . forEach ( failure => {
138- const start = failure . getStartPosition ( ) . getLineAndCharacter ( ) ;
139- const end = failure . getEndPosition ( ) . getLineAndCharacter ( ) ;
140- context . report ( {
141- message : `${ failure . getFailure ( ) } (tslint:${ failure . getRuleName ( ) } )` ,
142- loc : {
143- start : {
144- line : start . line + 1 ,
145- column : start . character ,
146- } ,
147- end : {
148- line : end . line + 1 ,
149- column : end . character ,
150- } ,
151- } ,
152- } ) ;
153- } ) ;
154- }
155-
156- /**
157- * Return an empty object for the ESLint rule
158- */
159- return { } ;
160160 } ,
161- } ,
162161} ;
0 commit comments