From 383652163b5729859f9721edb0faf08fea297b9d Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Mon, 12 Feb 2018 13:01:32 -0800 Subject: [PATCH] fix: dasherize after leading underscore --- packages/gatsby/src/schema/__tests__/create-key.js | 12 +++++++++--- packages/gatsby/src/schema/create-key.js | 4 ++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/gatsby/src/schema/__tests__/create-key.js b/packages/gatsby/src/schema/__tests__/create-key.js index eb4a28578422d..da6484dda1888 100644 --- a/packages/gatsby/src/schema/__tests__/create-key.js +++ b/packages/gatsby/src/schema/__tests__/create-key.js @@ -10,11 +10,17 @@ describe(`createKey`, () => { it(`replaces invalid characters`, () => { ;[ [`/hello`, `_hello`], - [`~/path/to/some/module`, `_path_to_some_module`], - [`/*`, `_`], - [`/*.js`, `_js`], + [`~/path/to/some/module`, `_-path-to-some-module`], + [`/*`, `_-`], + [`/*.js`, `_--js`], ].forEach(([input, output]) => { expect(createKey(input)).toBe(output) }) }) + + it(`does not generate same key for different input`, () => { + ;[[`/*.js`, `*js`]].forEach(([one, two]) => { + expect(createKey(one)).not.toBe(createKey(two)) + }) + }) }) diff --git a/packages/gatsby/src/schema/create-key.js b/packages/gatsby/src/schema/create-key.js index bc6b1bd3a57ca..4cb78499f5552 100644 --- a/packages/gatsby/src/schema/create-key.js +++ b/packages/gatsby/src/schema/create-key.js @@ -16,9 +16,9 @@ module.exports = (key: string): string => { const replaced = key.replace(nonAlphaNumericExpr, `_`) - // TODO: figure out what to replace this with _OR_ change the expr + // key is invalid; normalize with a leading underscore and dasherize rest if (replaced.match(/^__/)) { - return replaced.replace(/^_{2,}/, `_`) + return replaced.replace(/_/g, (char, index) => (index === 0 ? `_` : `-`)) } return replaced