Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support for browsers that don't support object spread literals #397

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/message-compiler/src/compiler.ts
Expand Up @@ -2,18 +2,21 @@ import { CompileOptions } from './options'
import { createParser } from './parser'
import { transform } from './transformer'
import { generate, CodeGenResult } from './generator'
import { assign } from '@intlify/shared'

export function baseCompile(
source: string,
options: CompileOptions = {}
): CodeGenResult {
const assignedOptions = assign({}, options)

// parse source codes
const parser = createParser({ ...options })
const parser = createParser(assignedOptions)
const ast = parser.parse(source)

// transform ASTs
transform(ast, { ...options })
transform(ast, assignedOptions)

// generate javascript codes
return generate(ast, { ...options })
return generate(ast, assignedOptions)
}
3 changes: 2 additions & 1 deletion packages/message-compiler/src/parser.ts
Expand Up @@ -2,6 +2,7 @@ import { Position, createLocation, SourceLocation } from './location'
import { ParserOptions } from './options'
import { createCompileError, CompileErrorCodes } from './errors'
import { Tokenizer, createTokenizer, TokenTypes, Token } from './tokenizer'
import { assign } from '@intlify/shared'

export const enum NodeTypes {
Resource, // 0
Expand Down Expand Up @@ -530,7 +531,7 @@ export function createParser(options: ParserOptions = {}): Parser {
}

function parse(source: string): ResourceNode {
const tokenizer = createTokenizer(source, { ...options })
const tokenizer = createTokenizer(source, assign({}, options))
const context = tokenizer.context()

const node = startNode(
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/index.ts
Expand Up @@ -87,6 +87,8 @@ export function warn(msg: string, err?: Error): void {
}
}

export const assign = Object.assign

let _globalThis: any
export const getGlobalThis = (): any => {
// prettier-ignore
Expand Down
6 changes: 3 additions & 3 deletions packages/vue-i18n/src/components/DatetimeFormat.ts
Expand Up @@ -2,6 +2,7 @@ import { useI18n } from '../i18n'
import { DatetimePartsSymbol } from '../composer'
import { renderFormatter } from './formatRenderer'
import { baseFormatProps } from './base'
import { assign } from '@intlify/shared'

import type { RenderFunction, SetupContext } from 'vue'
import type { DateTimeOptions } from '@intlify/core-base'
Expand Down Expand Up @@ -61,16 +62,15 @@ const DATETIME_FORMAT_KEYS = [
export const DatetimeFormat = {
/* eslint-disable */
name: 'i18n-d',
props: {
...baseFormatProps,
props: assign({
value: {
type: [Number, Date],
required: true
},
format: {
type: [String, Object]
}
},
}, baseFormatProps),
/* eslint-enable */
setup(props: DatetimeFormatProps, context: SetupContext): RenderFunction {
const i18n = useI18n({ useScope: 'parent' }) as Composer & ComposerInternal
Expand Down
6 changes: 3 additions & 3 deletions packages/vue-i18n/src/components/NumberFormat.ts
Expand Up @@ -2,6 +2,7 @@ import { useI18n } from '../i18n'
import { NumberPartsSymbol } from '../composer'
import { renderFormatter } from './formatRenderer'
import { baseFormatProps } from './base'
import { assign } from '@intlify/shared'

import type { SetupContext, RenderFunction } from 'vue'
import type { NumberOptions } from '@intlify/core-base'
Expand Down Expand Up @@ -56,16 +57,15 @@ const NUMBER_FORMAT_KEYS = [
export const NumberFormat = {
/* eslint-disable */
name: 'i18n-n',
props: {
...baseFormatProps,
props: assign({
value: {
type: Number,
required: true
},
format: {
type: [String, Object]
}
},
}, baseFormatProps),
/* eslint-enable */
setup(props: NumberFormatProps, context: SetupContext): RenderFunction {
const i18n = useI18n({ useScope: 'parent' }) as Composer & ComposerInternal
Expand Down
13 changes: 7 additions & 6 deletions packages/vue-i18n/src/components/Translation.ts
Expand Up @@ -3,6 +3,7 @@ import { isNumber, isString, isObject } from '@intlify/shared'
import { TransrateVNodeSymbol } from '../composer'
import { useI18n } from '../i18n'
import { baseFormatProps } from './base'
import { assign } from '@intlify/shared'

import type { SetupContext, VNodeChild, RenderFunction } from 'vue'
import type { Composer, ComposerInternal } from '../composer'
Expand Down Expand Up @@ -80,8 +81,7 @@ export interface TranslationProps extends BaseFormatProps {
export const Translation = {
/* eslint-disable */
name: 'i18n-t',
props: {
...baseFormatProps,
props: assign({
keypath: {
type: String,
required: true
Expand All @@ -91,7 +91,7 @@ export const Translation = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
validator: (val: any): boolean => isNumber(val) || !isNaN(val)
}
},
}, baseFormatProps),
/* eslint-enable */
setup(props: TranslationProps, context: SetupContext): RenderFunction {
const { slots, attrs } = context
Expand All @@ -114,12 +114,13 @@ export const Translation = {
arg,
options
)
const assignedAttrs = assign({}, attrs)
// prettier-ignore
return isString(props.tag)
? h(props.tag, { ...attrs }, children)
? h(props.tag, assignedAttrs, children)
: isObject(props.tag)
? h(props.tag, { ...attrs }, children)
: h(Fragment, { ...attrs }, children)
? h(props.tag, assignedAttrs, children)
: h(Fragment, assignedAttrs, children)
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions packages/vue-i18n/src/components/formatRenderer.ts
@@ -1,5 +1,5 @@
import { h, Fragment } from 'vue'
import { isString, isObject, isArray } from '@intlify/shared'
import { isString, isObject, isArray, assign } from '@intlify/shared'

import type {
RenderFunction,
Expand Down Expand Up @@ -90,11 +90,12 @@ export function renderFormatter<
children = [parts]
}

const assignedAttrs = assign({}, attrs)
// prettier-ignore
return isString(props.tag)
? h(props.tag, { ...attrs }, children)
? h(props.tag, assignedAttrs, children)
: isObject(props.tag)
? h(props.tag, { ...attrs }, children)
: h(Fragment, { ...attrs }, children)
? h(props.tag, assignedAttrs, children)
: h(Fragment, assignedAttrs, children)
}
}