-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from marp-team/unknown-theme-diagnostics
Add `unknown-theme` diagnostic
- Loading branch information
Showing
7 changed files
with
173 additions
and
6 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
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,84 @@ | ||
import dedent from 'dedent' | ||
import { | ||
Diagnostic, | ||
DiagnosticSeverity, | ||
Position, | ||
Range, | ||
TextDocument, | ||
Uri, | ||
} from 'vscode' | ||
import { DirectiveParser } from '../directive-parser' | ||
import { Themes } from '../themes' | ||
import * as rule from './unknown-theme' | ||
|
||
jest.mock('vscode') | ||
|
||
const doc = (text: string): TextDocument => | ||
({ | ||
getText: () => text, | ||
positionAt: (offset: number) => { | ||
const lines = text.slice(0, offset).split('\n') | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return new Position(lines.length - 1, lines.pop()!.length) | ||
}, | ||
uri: Uri.parse('/test/document'), | ||
fileName: '/test/document', | ||
} as any) | ||
|
||
describe('[Diagnostics rule] Unknown theme', () => { | ||
const register = (doc: TextDocument): Diagnostic[] => { | ||
const parser = new DirectiveParser() | ||
const diagnostics: Diagnostic[] = [] | ||
|
||
rule.register(doc, parser, diagnostics) | ||
|
||
parser.parse(doc) | ||
return diagnostics | ||
} | ||
|
||
describe('#register', () => { | ||
it('adds diagnostics when passed theme directive with not recognized theme name', () => { | ||
const diagnostics = register( | ||
doc(dedent` | ||
--- | ||
marp: true | ||
theme: unknown | ||
test: test | ||
--- | ||
`) | ||
) | ||
expect(diagnostics).toHaveLength(1) | ||
|
||
const [$theme] = diagnostics | ||
expect($theme).toBeInstanceOf(Diagnostic) | ||
expect($theme.code).toBe(rule.code) | ||
expect($theme.source).toBe('marp-vscode') | ||
expect($theme.severity).toBe(DiagnosticSeverity.Warning) | ||
expect($theme.range).toStrictEqual( | ||
new Range(new Position(2, 7), new Position(2, 14)) | ||
) | ||
}) | ||
|
||
it('does not add diagnostics when theme directive is not defined', () => | ||
expect(register(doc(''))).toHaveLength(0)) | ||
|
||
it('does not add diagnostics when the specified theme is recognized', () => { | ||
expect(register(doc('<!-- theme: default -->'))).toHaveLength(0) | ||
expect(register(doc('<!-- theme: gaia -->'))).toHaveLength(0) | ||
expect(register(doc('<!-- theme: uncover -->'))).toHaveLength(0) | ||
}) | ||
|
||
describe('when registered custom theme', () => { | ||
beforeEach(() => { | ||
jest | ||
.spyOn(Themes.prototype, 'getRegisteredStyles') | ||
.mockReturnValue([{ css: '/* @theme custom-theme */' } as any]) | ||
}) | ||
|
||
it('does not add diagnostics when specified the name of custom theme', () => { | ||
expect(register(doc('<!-- theme: custom-theme -->'))).toHaveLength(0) | ||
}) | ||
}) | ||
}) | ||
}) |
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,55 @@ | ||
import { Diagnostic, DiagnosticSeverity, Range, TextDocument } from 'vscode' | ||
import { DirectiveParser } from '../directive-parser' | ||
import themes from '../themes' | ||
|
||
interface ParsedThemeValue { | ||
value: string | ||
range: Range | ||
} | ||
|
||
export const code = 'unknown-theme' | ||
|
||
export function register( | ||
doc: TextDocument, | ||
directiveParser: DirectiveParser, | ||
diagnostics: Diagnostic[] | ||
) { | ||
let parsed: ParsedThemeValue | undefined | ||
|
||
directiveParser.on('startParse', () => { | ||
parsed = undefined | ||
}) | ||
|
||
directiveParser.on('directive', ({ item, offset, info }) => { | ||
if (info?.name === 'theme') { | ||
const [start, end] = item.value.range | ||
|
||
parsed = { | ||
value: item.value.value, | ||
range: new Range( | ||
doc.positionAt(start + offset), | ||
doc.positionAt(end + offset) | ||
), | ||
} | ||
} | ||
}) | ||
|
||
directiveParser.on('endParse', () => { | ||
if (parsed) { | ||
const themeSet = themes.getMarpThemeSetFor(doc) | ||
|
||
if (!themeSet.has(parsed.value)) { | ||
const diagnostic = new Diagnostic( | ||
parsed.range, | ||
`The specified theme "${parsed.value}" is not recognized by Marp for VS Code.`, | ||
DiagnosticSeverity.Warning | ||
) | ||
|
||
diagnostic.source = 'marp-vscode' | ||
diagnostic.code = code | ||
|
||
diagnostics.push(diagnostic) | ||
} | ||
} | ||
}) | ||
} |
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