-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #2642.
- Loading branch information
Showing
3 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const SourceCode = require('eslint').SourceCode; | ||
const espree = require('espree'); | ||
|
||
const getFromContext = require('../../lib/util/pragma').getFromContext; | ||
|
||
const DEFAULT_CONFIG = { | ||
ecmaVersion: 6, | ||
comment: true, | ||
tokens: true, | ||
range: true, | ||
loc: true | ||
}; | ||
|
||
const DEFAULT_SETTINGS = { | ||
react: { | ||
pragma: 'React' | ||
} | ||
}; | ||
|
||
const fakeContext = (code) => { | ||
const ast = espree.parse(code, DEFAULT_CONFIG); | ||
return { | ||
getSourceCode: () => new SourceCode(code, ast), | ||
settings: DEFAULT_SETTINGS | ||
}; | ||
}; | ||
|
||
describe('pragma', () => { | ||
describe('getFromContext', () => { | ||
it('finds the pragma in a block comment', () => { | ||
const code = '/* @jsx jsx */'; | ||
assert.strictEqual(getFromContext(fakeContext(code)), 'jsx'); | ||
}); | ||
|
||
it('finds the pragma in a docstring comment', () => { | ||
const code = '/** @jsx jsx */'; | ||
assert.strictEqual(getFromContext(fakeContext(code)), 'jsx'); | ||
}); | ||
|
||
it('finds the pragma in a line comment', () => { | ||
const code = '// @jsx jsx'; | ||
assert.strictEqual( | ||
getFromContext(fakeContext(code)), | ||
'jsx' | ||
); | ||
}); | ||
|
||
it('defaults to the value of settings.react.pragma', () => { | ||
const code = ''; | ||
assert.strictEqual( | ||
getFromContext(fakeContext(code)), | ||
DEFAULT_SETTINGS.react.pragma | ||
); | ||
}); | ||
|
||
it('throws an error if the pragma is invalid', () => { | ||
const code = '/* @jsx invalid-jsx-pragma */'; | ||
assert.throws(() => getFromContext(fakeContext(code))); | ||
}); | ||
}); | ||
}); |