Repository: prettier/prettier
File: src/language-markdown/mdx.js (JavaScript)

Code before:
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

const IMPORT_REGEX = /^import\s/u;
const EXPORT_REGEX = /^export\s/u;
const BLOCKS_REGEX = String.raw`[a-z][a-z0-9]*(\.[a-z][a-z0-9]*)*|`;
const COMMENT_REGEX = /<!---->|<!---?[^>-](?:-?[^-])*-->/u;
const ES_COMMENT_REGEX = /^\{\s*\/\*(.*)\*\/\s*\}/u;
const EMPTY_NEWLINE = "\n\n";

const isImport = (text) => IMPORT_REGEX.test(text);
const isExport = (text) => EXPORT_REGEX.test(text);
const isImportOrExport = (text) => isImport(text) || isExport(text);

const tokenizeEsSyntax = (eat, value) => {
  const index = value.indexOf(EMPTY_NEWLINE);
  const subvalue = index === -1 ? value : value.slice(0, index);

  if (isImportOrExport(subvalue)) {
    return eat(subvalue)({
      type: isExport(subvalue) ? "export" : "import",
      value: subvalue,
    });
  }
};

const tokenizeEsComment = (eat, value) => {
  const match = ES_COMMENT_REGEX.exec(value);

  if (match) {
    return eat(match[0])({
      type: "esComment",
      value: match[1].trim(),
    });
  }
};

/**
 * Don't parse ES syntax in open blocks
 * https://github.com/mdx-js/mdx/pull/763
 */
tokenizeEsSyntax.notInBlock = true;

Review comment (suggestion):
This is the cause of https://github.com/prettier/prettier/pull/17996#issuecomment-3345627650, and it's fixed in upstream.

Code after:
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

const IMPORT_REGEX = /^import\s/u;
const EXPORT_REGEX = /^export\s/u;
const BLOCKS_REGEX = String.raw`[a-z][a-z0-9]*(\.[a-z][a-z0-9]*)*|`;
const COMMENT_REGEX = /<!---->|<!---?[^>-](?:-?[^-])*-->/u;
const ES_COMMENT_REGEX = /^\{\s*\/\*(.*)\*\/\s*\}/u;
const EMPTY_NEWLINE = "\n\n";

const isImport = (text) => IMPORT_REGEX.test(text);
const isExport = (text) => EXPORT_REGEX.test(text);
const isImportOrExport = (text) => isImport(text) || isExport(text);

const tokenizeEsSyntax = (eat, value) => {
  const index = value.indexOf(EMPTY_NEWLINE);
  const subvalue = index === -1 ? value : value.slice(0, index);

  if (isImportOrExport(subvalue)) {
    return eat(subvalue)({
      type: isExport(subvalue) ? "export" : "import",
      value: subvalue,
    });
  }
};

tokenizeEsSyntax.notInBlock = true;

tokenizeEsSyntax.locator = (value /* , fromIndex*/) =>
  isImportOrExport(value) ? -1 : 1;

const tokenizeEsComment = (eat, value) => {
  const match = ES_COMMENT_REGEX.exec(value);

  if (match) {
    return eat(match[0])({
      type: "esComment",
      value: match[1].trim(),
    });
  }
};