mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-06-05 21:04:28 +08:00
fix(client): ensure source is added to files (#56601)
This commit is contained in:
parent
20a8c1d59b
commit
0a64f4bd53
@ -14,7 +14,8 @@ import {
|
||||
transformContents,
|
||||
transformHeadTailAndContents,
|
||||
setExt,
|
||||
compileHeadTail
|
||||
compileHeadTail,
|
||||
createSource
|
||||
} from '../../../../../shared/utils/polyvinyl';
|
||||
import { WorkerExecutor } from '../utils/worker-executor';
|
||||
|
||||
@ -275,6 +276,7 @@ const htmlTransformer = cond([
|
||||
]);
|
||||
|
||||
export const getTransformers = loopProtectOptions => [
|
||||
createSource,
|
||||
replaceNBSP,
|
||||
babelTransformer(loopProtectOptions),
|
||||
partial(compileHeadTail, ''),
|
||||
@ -282,6 +284,7 @@ export const getTransformers = loopProtectOptions => [
|
||||
];
|
||||
|
||||
export const getPythonTransformers = () => [
|
||||
createSource,
|
||||
replaceNBSP,
|
||||
partial(compileHeadTail, '')
|
||||
];
|
||||
|
||||
@ -20,7 +20,7 @@ const invariant = require('invariant');
|
||||
// contents: String,
|
||||
// history?: [...String],
|
||||
// }) => PolyVinyl, throws
|
||||
function createPoly({ name, ext, contents, history, ...rest } = {}) {
|
||||
function createPoly({ name, ext, contents, history, ...rest }) {
|
||||
invariant(typeof name === 'string', 'name must be a string but got %s', name);
|
||||
|
||||
invariant(typeof ext === 'string', 'ext must be a string, but was %s', ext);
|
||||
@ -147,11 +147,20 @@ function transformHeadTailAndContents(wrap, poly) {
|
||||
};
|
||||
}
|
||||
|
||||
// createSource(poly: PolyVinyl) => PolyVinyl
|
||||
function createSource(poly) {
|
||||
return {
|
||||
...poly,
|
||||
source: poly.source || poly.contents
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createPoly,
|
||||
isPoly,
|
||||
setContent,
|
||||
setExt,
|
||||
createSource,
|
||||
compileHeadTail,
|
||||
regeneratePathAndHistory,
|
||||
transformContents,
|
||||
|
||||
29
shared/utils/polyvinyl.test.js
Normal file
29
shared/utils/polyvinyl.test.js
Normal file
@ -0,0 +1,29 @@
|
||||
import polyvinyl from './polyvinyl';
|
||||
|
||||
const polyData = {
|
||||
name: 'test',
|
||||
ext: 'js',
|
||||
contents: 'var hello = world;',
|
||||
history: ['test.js']
|
||||
};
|
||||
|
||||
describe('createSource', () => {
|
||||
it('should return a vinyl object with a source matching the contents', () => {
|
||||
const original = polyvinyl.createPoly(polyData);
|
||||
|
||||
const updated = polyvinyl.createSource(original);
|
||||
expect(original).not.toHaveProperty('source');
|
||||
expect(updated).toHaveProperty('source', 'var hello = world;');
|
||||
expect(updated).toMatchObject(original);
|
||||
});
|
||||
|
||||
it('should not update the source if it already exists', () => {
|
||||
const original = polyvinyl.createPoly({
|
||||
...polyData,
|
||||
source: 'const hello = world;'
|
||||
});
|
||||
|
||||
const updated = polyvinyl.createSource(original);
|
||||
expect(updated).toStrictEqual(original);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user