|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +const assert = require('assert'); |
| 9 | +const fs = require('fs'); |
| 10 | +const path = require('path'); |
| 11 | +const {ESLint} = require('eslint'); |
| 12 | +const plugin = require('..'); |
| 13 | + |
| 14 | +const FIXTURES_DIR = path.join( |
| 15 | + __dirname, |
| 16 | + 'fixtures', |
| 17 | + 'src', |
| 18 | + 'content' |
| 19 | +); |
| 20 | +const PARSER_PATH = path.join(__dirname, '..', 'parser.js'); |
| 21 | + |
| 22 | +function createESLint({fix = false} = {}) { |
| 23 | + return new ESLint({ |
| 24 | + useEslintrc: false, |
| 25 | + fix, |
| 26 | + plugins: { |
| 27 | + 'local-rules': plugin, |
| 28 | + }, |
| 29 | + overrideConfig: { |
| 30 | + parser: PARSER_PATH, |
| 31 | + plugins: ['local-rules'], |
| 32 | + rules: { |
| 33 | + 'local-rules/lint-markdown-code-blocks': 'error', |
| 34 | + }, |
| 35 | + parserOptions: { |
| 36 | + sourceType: 'module', |
| 37 | + }, |
| 38 | + }, |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +function readFixture(name) { |
| 43 | + return fs.readFileSync(path.join(FIXTURES_DIR, name), 'utf8'); |
| 44 | +} |
| 45 | + |
| 46 | +async function lintFixture(name, {fix = false} = {}) { |
| 47 | + const eslint = createESLint({fix}); |
| 48 | + const filePath = path.join(FIXTURES_DIR, name); |
| 49 | + const markdown = readFixture(name); |
| 50 | + const [result] = await eslint.lintText(markdown, {filePath}); |
| 51 | + return result; |
| 52 | +} |
| 53 | + |
| 54 | +async function run() { |
| 55 | + const basicResult = await lintFixture('basic-error.md'); |
| 56 | + assert.strictEqual( |
| 57 | + basicResult.messages.length, |
| 58 | + 1, |
| 59 | + 'expected one diagnostic' |
| 60 | + ); |
| 61 | + assert( |
| 62 | + basicResult.messages[0].message.includes('Calling setState during render'), |
| 63 | + 'expected message to mention setState during render' |
| 64 | + ); |
| 65 | + |
| 66 | + const suppressedResult = await lintFixture('suppressed-error.md'); |
| 67 | + assert.strictEqual( |
| 68 | + suppressedResult.messages.length, |
| 69 | + 0, |
| 70 | + 'expected suppression metadata to silence diagnostic' |
| 71 | + ); |
| 72 | + |
| 73 | + const staleResult = await lintFixture('stale-expected-error.md'); |
| 74 | + assert.strictEqual( |
| 75 | + staleResult.messages.length, |
| 76 | + 1, |
| 77 | + 'expected stale metadata error' |
| 78 | + ); |
| 79 | + assert.strictEqual( |
| 80 | + staleResult.messages[0].message, |
| 81 | + 'React Compiler expected error on line 3 was not triggered' |
| 82 | + ); |
| 83 | + |
| 84 | + const duplicateResult = await lintFixture('duplicate-metadata.md'); |
| 85 | + assert.strictEqual( |
| 86 | + duplicateResult.messages.length, |
| 87 | + 2, |
| 88 | + 'expected duplicate metadata to surface compiler diagnostic and stale metadata notice' |
| 89 | + ); |
| 90 | + const duplicateFixed = await lintFixture('duplicate-metadata.md', { |
| 91 | + fix: true, |
| 92 | + }); |
| 93 | + assert( |
| 94 | + duplicateFixed.output.includes( |
| 95 | + "{expectedErrors: {'react-compiler': [4]}}" |
| 96 | + ), |
| 97 | + 'expected duplicates to be rewritten to a single canonical block' |
| 98 | + ); |
| 99 | + assert( |
| 100 | + !duplicateFixed.output.includes('[99]'), |
| 101 | + 'expected stale line numbers to be removed from metadata' |
| 102 | + ); |
| 103 | + |
| 104 | + const mixedLanguageResult = await lintFixture('mixed-language.md'); |
| 105 | + assert.strictEqual( |
| 106 | + mixedLanguageResult.messages.length, |
| 107 | + 0, |
| 108 | + 'expected non-js code fences to be ignored' |
| 109 | + ); |
| 110 | + |
| 111 | + const malformedResult = await lintFixture('malformed-metadata.md'); |
| 112 | + assert.strictEqual( |
| 113 | + malformedResult.messages.length, |
| 114 | + 1, |
| 115 | + 'expected malformed metadata to fall back to compiler diagnostics' |
| 116 | + ); |
| 117 | + const malformedFixed = await lintFixture('malformed-metadata.md', { |
| 118 | + fix: true, |
| 119 | + }); |
| 120 | + assert( |
| 121 | + malformedFixed.output.includes( |
| 122 | + "{expectedErrors: {'react-compiler': [4]}}" |
| 123 | + ), |
| 124 | + 'expected malformed metadata to be replaced with canonical form' |
| 125 | + ); |
| 126 | +} |
| 127 | + |
| 128 | +run().catch(error => { |
| 129 | + console.error(error); |
| 130 | + process.exitCode = 1; |
| 131 | +}); |
0 commit comments