@@ -26,85 +26,48 @@ const RuleTester = require('eslint').RuleTester;
26
26
27
27
const ruleTester = new RuleTester ( ) ;
28
28
29
- let graphqlEslintParserPath ;
30
-
31
- try {
32
- graphqlEslintParserPath = require . resolve ( '@graphql-eslint/eslint-plugin' ) ;
33
- } catch ( e ) {
34
- // ignore
35
- }
36
-
37
29
ruleTester . run ( 'prettier' , rule , {
38
30
valid : [
39
- // Correct style.
31
+ // Correct style. Also proves that the plugin works if no filename is provided
40
32
{ code : `'';\n` } ,
41
33
// Double quote from .prettierrc.
42
34
{ code : '"";\n' , filename : getPrettierRcJsFilename ( 'double-quote' ) } ,
43
35
// Override .prettierrc from object option.
44
36
{
45
37
code : `var foo = {bar: 0};\n` ,
46
38
filename : getPrettierRcJsFilename ( 'bracket-spacing' ) ,
47
- options : [ { bracketSpacing : false } ]
39
+ options : [ { bracketSpacing : false } ] ,
48
40
} ,
49
41
// Only use options from plugin, skipping .prettierrc
50
42
{
51
43
code : `var foo = {bar: 0};\n` ,
52
44
filename : getPrettierRcJsFilename ( 'bracket-spacing' ) ,
53
- options : [ { bracketSpacing : false } , { usePrettierrc : false } ]
45
+ options : [ { bracketSpacing : false } , { usePrettierrc : false } ] ,
54
46
} ,
55
47
// Ignores filenames in .prettierignore
56
48
{
57
49
code : `("");\n` ,
58
- filename : getPrettierRcJsFilename ( 'single-quote' , 'ignore-me.js' )
50
+ filename : getPrettierRcJsFilename ( 'single-quote' , 'ignore-me.js' ) ,
59
51
} ,
60
52
// Sets a default parser when it can't be inferred from the file extensions
61
53
{
62
54
code : `('');\n` ,
63
- filename : getPrettierRcJsFilename ( 'single-quote' , 'dummy.qqq' )
55
+ filename : getPrettierRcJsFilename ( 'single-quote' , 'dummy.qqq' ) ,
64
56
} ,
65
57
// Overwrites the parser for file extensions prettier would try to format
66
58
// with not the babylon parser
67
59
// In the real world, eslint-plugin-markdown would transform file contents
68
60
// into JS snippets that would get passed to ESLint
69
61
{
70
62
code : `('');\n` ,
71
- filename : getPrettierRcJsFilename ( 'single-quote' , 'dummy.md' )
63
+ filename : getPrettierRcJsFilename ( 'single-quote' , 'dummy.md' ) ,
72
64
} ,
73
65
// Should ignore files from node_modules
74
66
{
75
67
code : 'a();;;;;;\n' ,
76
- filename : 'node_modules/dummy.js'
68
+ filename : 'node_modules/dummy.js' ,
77
69
} ,
78
- // ESLint processors can provide virtual filenames. E.g. fenced code blocks
79
- // in a markdown file may be processed with the filenames
80
- // `a-markdown-file.md/1.js` / `a-markdown-file.md/2.js`
81
- // If we try and pass those filenames into prettier's `resolveConfig` and
82
- // `getFileInfo` methods they throw up because the it doesn't like treating
83
- // `markdown-file.md` as a directory.
84
- // Make sure we handle that case internally so this does not crash
85
- {
86
- code : `('');\n` ,
87
- filename : path . join ( __filename , '0_fake_virtual_name.js' )
88
- } ,
89
- {
90
- code : 'ESLintPluginGraphQLFile`type Query {\n foo: String!\n}`\n' ,
91
- filename : getPrettierRcJsFilename ( 'no-semi' , 'dummy.graphql' ) ,
92
- parserOptions : {
93
- ecmaVersion : 2015
94
- }
95
- }
96
- ] . concat (
97
- graphqlEslintParserPath
98
- ? {
99
- code : `type Query {
100
- foo: String!
101
- }
102
- ` ,
103
- filename : 'valid.graphql' ,
104
- parser : graphqlEslintParserPath
105
- }
106
- : [ ]
107
- ) ,
70
+ ] ,
108
71
invalid : [
109
72
'01' ,
110
73
'02' ,
@@ -124,29 +87,71 @@ ruleTester.run('prettier', rule, {
124
87
'15' ,
125
88
'16' ,
126
89
'17' ,
127
- '18'
128
- ] . map ( loadInvalidFixture )
90
+ '18' ,
91
+ ] . map ( loadInvalidFixture ) ,
129
92
} ) ;
130
93
131
94
const vueRuleTester = new RuleTester ( {
132
- parser : require . resolve ( 'vue-eslint-parser' )
95
+ parser : require . resolve ( 'vue-eslint-parser' ) ,
133
96
} ) ;
134
97
135
- vueRuleTester . run ( 'prettier ' , rule , {
98
+ vueRuleTester . run ( 'vue ' , rule , {
136
99
valid : [
137
100
{
138
101
code : `<template>\n <div>HI</div>\n</template>\n<script>\n3;\n</script>\n` ,
139
- filename : 'valid.vue'
140
- }
102
+ filename : 'valid.vue' ,
103
+ } ,
141
104
] ,
142
105
invalid : [
143
106
Object . assign ( loadInvalidFixture ( 'vue' ) , {
144
- filename : 'invalid.vue'
107
+ filename : 'invalid.vue' ,
145
108
} ) ,
146
109
Object . assign ( loadInvalidFixture ( 'vue-syntax-error' ) , {
147
- filename : 'syntax-error.vue'
148
- } )
149
- ]
110
+ filename : 'syntax-error.vue' ,
111
+ } ) ,
112
+ ] ,
113
+ } ) ;
114
+
115
+ const atGraphqlEslintRuleTester = new RuleTester ( {
116
+ parser : require . resolve ( '@graphql-eslint/eslint-plugin' ) ,
117
+ } ) ;
118
+
119
+ atGraphqlEslintRuleTester . run ( '@graphql-eslint/eslint-plugin' , rule , {
120
+ valid : [
121
+ {
122
+ code : `type Query {\n foo: String!\n}\n` ,
123
+ filename : 'valid.graphql' ,
124
+ } ,
125
+ ] ,
126
+ invalid : [
127
+ Object . assign ( loadInvalidFixture ( 'graphql' ) , {
128
+ filename : 'invalid.graphql' ,
129
+ } ) ,
130
+ ] ,
131
+ } ) ;
132
+
133
+ // eslint-plugin-graphql handles literal graphql files by tranforming graphql
134
+ // code with a processor, instead of using a parser. Unfortunatly we cant
135
+ // specify custom processors in a RuleTester, so instead we have write test code
136
+ // that is the result of eslint-plugin-graphql's processing - this is the
137
+ // ESLintPluginGraphQLFile tagged template literal. See
138
+ // https://github.com/apollographql/eslint-plugin-graphql/blob/c465fedc8fea239ee1731ad4ec3ee1183a3cdddf/src/index.js#L404
139
+ // In the future if ESLint supports processors (https://github.com/eslint/rfcs/pull/31)
140
+ // we should be define a RuleTester like
141
+ // `newRuleTester({processor: require('eslint-plugin-graphql').processor['.graphql']})
142
+ // and then pass in pure graphql into the code value.
143
+ const eslintPluginGraphqlRuleTester = new RuleTester ( {
144
+ parserOptions : { ecmaVersion : 2015 } ,
145
+ } ) ;
146
+
147
+ eslintPluginGraphqlRuleTester . run ( 'eslint-plugin-graphql' , rule , {
148
+ valid : [
149
+ {
150
+ code : 'ESLintPluginGraphQLFile`type Query {\n foo: String!\n}`\n' ,
151
+ filename : getPrettierRcJsFilename ( 'no-semi' , 'dummy.graphql' ) ,
152
+ } ,
153
+ ] ,
154
+ invalid : [ ] ,
150
155
} ) ;
151
156
152
157
// ------------------------------------------------------------------------------
@@ -166,13 +171,13 @@ function loadInvalidFixture(name) {
166
171
const src = fs . readFileSync ( filename , 'utf8' ) ;
167
172
const sections = src
168
173
. split ( / ^ [ A - Z ] + : \n / m)
169
- . map ( x => x . replace ( / (? = \n ) \n $ / , '' ) ) ;
174
+ . map ( ( x ) => x . replace ( / (? = \n ) \n $ / , '' ) ) ;
170
175
const item = {
171
176
code : sections [ 1 ] ,
172
177
output : sections [ 2 ] ,
173
178
options : eval ( sections [ 3 ] ) , // eslint-disable-line no-eval
174
179
errors : eval ( sections [ 4 ] ) , // eslint-disable-line no-eval
175
- filename : getPrettierRcJsFilename ( 'double-quote' , name + '.txt' )
180
+ filename : getPrettierRcJsFilename ( 'double-quote' , name + '.txt' ) ,
176
181
} ;
177
182
if ( sections . length >= 6 ) {
178
183
item . filename = sections [ 5 ] ;
0 commit comments