Skip to content

Commit

Permalink
fix: Support for browsers that don't support object spread literals (#…
Browse files Browse the repository at this point in the history
…397)

closes #184
closes #393
  • Loading branch information
kazupon committed Mar 15, 2021
1 parent de0bee7 commit 0181351
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 20 deletions.
9 changes: 6 additions & 3 deletions packages/message-compiler/src/compiler.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 0181351

Please sign in to comment.